@@ -465,8 +465,11 @@ public static function countReturns(File $file, int $functionPosition): array
465
465
}
466
466
467
467
if ($ tokens [$ i ]['code ' ] === T_RETURN && !$ scopeClosers ->count ()) {
468
- PhpcsHelpers::isVoidReturn ($ file , $ i ) ? $ voidReturnCount ++ : $ nonVoidReturnCount ++;
469
- PhpcsHelpers::isNullReturn ($ file , $ i ) and $ nullReturnCount ++;
468
+ $ void = PhpcsHelpers::isVoidReturn ($ file , $ i );
469
+ $ null = PhpcsHelpers::isNullReturn ($ file , $ i );
470
+ $ void and $ voidReturnCount ++;
471
+ $ null and $ nullReturnCount ++;
472
+ (!$ void && !$ null ) and $ nonVoidReturnCount ++;
470
473
}
471
474
}
472
475
@@ -476,9 +479,10 @@ public static function countReturns(File $file, int $functionPosition): array
476
479
/**
477
480
* @param File $file
478
481
* @param int $returnPosition
482
+ * @param bool $includeNull
479
483
* @return bool
480
484
*/
481
- public static function isVoidReturn (File $ file , int $ returnPosition ): bool
485
+ public static function isVoidReturn (File $ file , int $ returnPosition, $ includeNull = false ): bool
482
486
{
483
487
$ tokens = $ file ->getTokens ();
484
488
@@ -487,21 +491,12 @@ public static function isVoidReturn(File $file, int $returnPosition): bool
487
491
}
488
492
489
493
$ returnPosition ++;
490
- $ nextToReturn = $ file ->findNext ([T_WHITESPACE ], $ returnPosition , null , true , null , true );
491
- $ nextToReturnType = $ tokens [$ nextToReturn ]['code ' ] ?? '' ;
492
-
493
- if ($ nextToReturnType === T_SEMICOLON ) {
494
- return true ;
495
- }
496
-
497
- if ($ nextToReturnType !== T_NULL ) {
498
- return false ;
499
- }
494
+ $ exclude = Tokens::$ emptyTokens ;
495
+ $ includeNull and $ exclude [] = T_NULL ;
500
496
501
- $ returnPosition = $ nextToReturn + 1 ;
502
- $ followedBySemicolon = ($ tokens [$ returnPosition ]['code ' ] ?? '' ) === T_SEMICOLON ;
497
+ $ nextToReturn = $ file ->findNext ($ exclude , $ returnPosition , null , true , null , true );
503
498
504
- return $ followedBySemicolon ;
499
+ return ( $ tokens [ $ nextToReturn ][ ' code ' ] ?? '' ) === T_SEMICOLON ;
505
500
}
506
501
507
502
/**
@@ -511,16 +506,9 @@ public static function isVoidReturn(File $file, int $returnPosition): bool
511
506
*/
512
507
public static function isNullReturn (File $ file , int $ returnPosition ): bool
513
508
{
514
- $ tokens = $ file ->getTokens ();
515
-
516
- if (($ tokens [$ returnPosition ]['code ' ] ?? '' ) !== T_RETURN ) {
517
- return false ;
518
- }
519
-
520
- $ returnPosition ++;
521
- $ nextToReturn = $ file ->findNext ([T_WHITESPACE , T_NULL ], $ returnPosition , null , true , null , true );
522
-
523
- return ($ tokens [$ nextToReturn ]['code ' ] ?? '' ) === T_SEMICOLON ;
509
+ return
510
+ !self ::isVoidReturn ($ file , $ returnPosition , false )
511
+ && self ::isVoidReturn ($ file , $ returnPosition , true );
524
512
}
525
513
526
514
/**
0 commit comments