@@ -465,8 +465,11 @@ public static function countReturns(File $file, int $functionPosition): array
465465 }
466466
467467 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 ++;
470473 }
471474 }
472475
@@ -476,9 +479,10 @@ public static function countReturns(File $file, int $functionPosition): array
476479 /**
477480 * @param File $file
478481 * @param int $returnPosition
482+ * @param bool $includeNull
479483 * @return bool
480484 */
481- public static function isVoidReturn (File $ file , int $ returnPosition ): bool
485+ public static function isVoidReturn (File $ file , int $ returnPosition, $ includeNull = false ): bool
482486 {
483487 $ tokens = $ file ->getTokens ();
484488
@@ -487,21 +491,12 @@ public static function isVoidReturn(File $file, int $returnPosition): bool
487491 }
488492
489493 $ 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 ;
500496
501- $ returnPosition = $ nextToReturn + 1 ;
502- $ followedBySemicolon = ($ tokens [$ returnPosition ]['code ' ] ?? '' ) === T_SEMICOLON ;
497+ $ nextToReturn = $ file ->findNext ($ exclude , $ returnPosition , null , true , null , true );
503498
504- return $ followedBySemicolon ;
499+ return ( $ tokens [ $ nextToReturn ][ ' code ' ] ?? '' ) === T_SEMICOLON ;
505500 }
506501
507502 /**
@@ -511,16 +506,9 @@ public static function isVoidReturn(File $file, int $returnPosition): bool
511506 */
512507 public static function isNullReturn (File $ file , int $ returnPosition ): bool
513508 {
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 );
524512 }
525513
526514 /**
0 commit comments