@@ -1375,12 +1375,12 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
13751375 $ closureScope = $ this ->enterAnonymousFunctionWithoutReflection ($ node , $ callableParameters );
13761376 $ closureReturnStatements = [];
13771377 $ closureYieldStatements = [];
1378- $ closureExecutionEnds = [] ;
1378+ $ onlyNeverExecutionEnds = null ;
13791379 $ closureImpurePoints = [];
13801380 $ invalidateExpressions = [];
13811381
13821382 try {
1383- $ closureStatementResult = $ this ->nodeScopeResolver ->processStmtNodes ($ node , $ node ->stmts , $ closureScope , static function (Node $ node , Scope $ scope ) use ($ closureScope , &$ closureReturnStatements , &$ closureYieldStatements , &$ closureExecutionEnds , &$ closureImpurePoints , &$ invalidateExpressions ): void {
1383+ $ closureStatementResult = $ this ->nodeScopeResolver ->processStmtNodes ($ node , $ node ->stmts , $ closureScope , static function (Node $ node , Scope $ scope ) use ($ closureScope , &$ closureReturnStatements , &$ closureYieldStatements , &$ onlyNeverExecutionEnds , &$ closureImpurePoints , &$ invalidateExpressions ): void {
13841384 if ($ scope ->getAnonymousFunctionReflection () !== $ closureScope ->getAnonymousFunctionReflection ()) {
13851385 return ;
13861386 }
@@ -1405,16 +1405,24 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
14051405 if ($ node ->getStatementResult ()->isAlwaysTerminating ()) {
14061406 foreach ($ node ->getStatementResult ()->getExitPoints () as $ exitPoint ) {
14071407 if ($ exitPoint ->getStatement () instanceof Node \Stmt \Return_) {
1408+ $ onlyNeverExecutionEnds = false ;
14081409 continue ;
14091410 }
14101411
1411- $ closureExecutionEnds [] = $ node ;
1412+ if ($ onlyNeverExecutionEnds === null ) {
1413+ $ onlyNeverExecutionEnds = true ;
1414+ }
1415+
14121416 break ;
14131417 }
14141418
14151419 if (count ($ node ->getStatementResult ()->getExitPoints ()) === 0 ) {
1416- $ closureExecutionEnds [] = $ node ;
1420+ if ($ onlyNeverExecutionEnds === null ) {
1421+ $ onlyNeverExecutionEnds = true ;
1422+ }
14171423 }
1424+ } else {
1425+ $ onlyNeverExecutionEnds = false ;
14181426 }
14191427
14201428 return ;
@@ -1449,13 +1457,13 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
14491457 }
14501458
14511459 if (count ($ returnTypes ) === 0 ) {
1452- if (count ( $ closureExecutionEnds ) > 0 && !$ hasNull ) {
1460+ if ($ onlyNeverExecutionEnds === true && !$ hasNull ) {
14531461 $ returnType = new NonAcceptingNeverType ();
14541462 } else {
14551463 $ returnType = new VoidType ();
14561464 }
14571465 } else {
1458- if (count ( $ closureExecutionEnds ) > 0 ) {
1466+ if ($ onlyNeverExecutionEnds === true ) {
14591467 $ returnTypes [] = new NonAcceptingNeverType ();
14601468 }
14611469 if ($ hasNull ) {
0 commit comments