@@ -119,13 +119,14 @@ public static function findOopContext(File $file, int $position): int
119
119
$ targetLevel = (int )$ tokens [$ position ]['level ' ] - 1 ;
120
120
121
121
foreach ($ tokens [$ position ]['conditions ' ] as $ condPosition => $ condCode ) {
122
+ assert (is_int ($ condPosition ));
122
123
$ condLevel = (int )($ tokens [$ condPosition ]['level ' ] ?? -1 );
123
124
124
125
if (
125
126
in_array ($ condCode , Tokens::$ ooScopeTokens , true )
126
127
&& ($ condLevel === $ targetLevel )
127
128
) {
128
- return ( int ) $ condPosition ;
129
+ return $ condPosition ;
129
130
}
130
131
}
131
132
@@ -288,7 +289,7 @@ public static function isHookClosure(
288
289
/** @var array<int, array<string, mixed>> $tokens */
289
290
$ tokens = $ file ->getTokens ();
290
291
291
- if (( $ tokens [$ position ]['code ' ] ?? '' ) !== T_CLOSURE ) {
292
+ if (! in_array (( $ tokens [$ position ]['code ' ] ?? '' ), [ T_CLOSURE , T_FN ], true ) ) {
292
293
return false ;
293
294
}
294
295
@@ -341,7 +342,7 @@ public static function functionDocBlockTags(
341
342
342
343
if (
343
344
!array_key_exists ($ position , $ tokens )
344
- || !in_array ($ tokens [$ position ]['code ' ], [T_FUNCTION , T_CLOSURE ], true )
345
+ || !in_array ($ tokens [$ position ]['code ' ], [T_FUNCTION , T_CLOSURE , T_FN ], true )
345
346
) {
346
347
return [];
347
348
}
@@ -387,7 +388,7 @@ public static function functionDocBlockTags(
387
388
$ normalizedTags = [];
388
389
static $ rand ;
389
390
$ rand or $ rand = bin2hex (random_bytes (3 ));
390
- foreach ($ tags as list ( $ tagName , $ tagContent) ) {
391
+ foreach ($ tags as [ $ tagName , $ tagContent] ) {
391
392
empty ($ normalizedTags [$ tagName ]) and $ normalizedTags [$ tagName ] = [];
392
393
if (!$ normalizeContent ) {
393
394
$ normalizedTags [$ tagName ][] = $ tagContent ;
@@ -433,7 +434,7 @@ public static function functionDocBlockParamTypes(File $file, int $functionPosit
433
434
434
435
$ types = [];
435
436
foreach ($ params as $ param ) {
436
- preg_match ('~^([^$]+)\s*(\$(?:[^\s]+) )~ ' , trim ($ param ), $ matches );
437
+ preg_match ('~^([^$]+)\s*(\$\S+ )~ ' , trim ($ param ), $ matches );
437
438
if (empty ($ matches [1 ]) || empty ($ matches [2 ])) {
438
439
continue ;
439
440
}
@@ -461,7 +462,7 @@ public static function isHookFunction(File $file, int $position): bool
461
462
*/
462
463
public static function functionBody (File $ file , int $ position ): string
463
464
{
464
- list ( $ start , $ end) = static ::functionBoundaries ($ file , $ position );
465
+ [ $ start , $ end] = static ::functionBoundaries ($ file , $ position );
465
466
if ($ start < 0 || $ end < 0 ) {
466
467
return '' ;
467
468
}
@@ -470,7 +471,7 @@ public static function functionBody(File $file, int $position): string
470
471
$ tokens = $ file ->getTokens ();
471
472
$ body = '' ;
472
473
for ($ i = $ start + 1 ; $ i < $ end ; $ i ++) {
473
- $ body .= (string )$ tokens [$ i ]['content ' ];
474
+ $ body .= (string )( $ tokens [$ i ]['content ' ] ?? '' ) ;
474
475
}
475
476
476
477
return $ body ;
@@ -479,30 +480,24 @@ public static function functionBody(File $file, int $position): string
479
480
/**
480
481
* @param File $file
481
482
* @param int $position
482
- * @return array {int, int}
483
+ * @return list {int, int}
483
484
*/
484
485
public static function functionBoundaries (File $ file , int $ position ): array
485
486
{
486
487
/** @var array<int, array<string, mixed>> $tokens */
487
488
$ tokens = $ file ->getTokens ();
488
489
489
- if (!in_array (($ tokens [$ position ]['code ' ] ?? null ), [T_FUNCTION , T_CLOSURE ], true )) {
490
+ if (!in_array (($ tokens [$ position ]['code ' ] ?? null ), [T_FUNCTION , T_CLOSURE , T_FN ], true )) {
490
491
return [-1 , -1 ];
491
492
}
492
493
493
- $ functionStart = (int )($ tokens [$ position ]['scope_opener ' ] ?? 0 );
494
- $ functionEnd = (int )($ tokens [$ position ]['scope_closer ' ] ?? 0 );
495
- if ($ functionStart <= 0 || $ functionEnd <= 0 || $ functionStart >= ($ functionEnd - 1 )) {
496
- return [-1 , -1 ];
497
- }
498
-
499
- return [$ functionStart , $ functionEnd ];
494
+ return static ::boundaries ($ tokens , $ position );
500
495
}
501
496
502
497
/**
503
498
* @param File $file
504
499
* @param int $position
505
- * @return array {int, int}
500
+ * @return list {int, int}
506
501
*/
507
502
public static function classBoundaries (File $ file , int $ position ): array
508
503
{
@@ -513,13 +508,7 @@ public static function classBoundaries(File $file, int $position): array
513
508
return [-1 , -1 ];
514
509
}
515
510
516
- $ start = (int )($ tokens [$ position ]['scope_opener ' ] ?? 0 );
517
- $ end = (int )($ tokens [$ position ]['scope_closer ' ] ?? 0 );
518
- if ($ start <= 0 || $ end <= 0 || $ start >= ($ end - 1 )) {
519
- return [-1 , -1 ];
520
- }
521
-
522
- return [$ start , $ end ];
511
+ return static ::boundaries ($ tokens , $ position );
523
512
}
524
513
525
514
/**
@@ -531,18 +520,22 @@ public static function returnsCountInfo(File $file, int $position): array
531
520
{
532
521
$ returnCount = ['nonEmpty ' => 0 , 'void ' => 0 , 'null ' => 0 , 'total ' => 0 ];
533
522
534
- list ( $ start , $ end) = self ::functionBoundaries ($ file , $ position );
523
+ [ $ start , $ end] = self ::functionBoundaries ($ file , $ position );
535
524
if ($ start < 0 || $ end <= 0 ) {
536
525
return $ returnCount ;
537
526
}
538
527
539
528
/** @var array<int, array<string, mixed>> $tokens */
540
529
$ tokens = $ file ->getTokens ();
541
530
531
+ if (T_FN === ($ tokens [$ position ]['code ' ] ?? null )) {
532
+ return ['nonEmpty ' => 1 , 'void ' => 0 , 'null ' => 0 , 'total ' => 1 ];
533
+ }
534
+
542
535
$ pos = $ start + 1 ;
543
536
while ($ pos < $ end ) {
544
- list ( , $ innerFunctionEnd) = self ::functionBoundaries ($ file , $ pos );
545
- list ( , $ innerClassEnd) = self ::classBoundaries ($ file , $ pos );
537
+ [ , $ innerFunctionEnd] = self ::functionBoundaries ($ file , $ pos );
538
+ [ , $ innerClassEnd] = self ::classBoundaries ($ file , $ pos );
546
539
if ($ innerFunctionEnd > 0 || $ innerClassEnd > 0 ) {
547
540
$ pos = ($ innerFunctionEnd > 0 ) ? $ innerFunctionEnd + 1 : $ innerClassEnd + 1 ;
548
541
continue ;
@@ -723,4 +716,20 @@ public static function isUntypedPsrMethod(File $file, int $position): bool
723
716
724
717
return false ;
725
718
}
719
+
720
+ /**
721
+ * @param array<int, array<string, mixed>> $tokens
722
+ * @param int $position
723
+ * @return list{int, int}
724
+ */
725
+ private static function boundaries (array $ tokens , int $ position ): array
726
+ {
727
+ $ start = (int )($ tokens [$ position ]['scope_opener ' ] ?? 0 );
728
+ $ end = (int )($ tokens [$ position ]['scope_closer ' ] ?? 0 );
729
+ if ($ start <= 0 || $ end <= 0 || $ start >= ($ end - 1 )) {
730
+ return [-1 , -1 ];
731
+ }
732
+
733
+ return [$ start , $ end ];
734
+ }
726
735
}
0 commit comments