diff --git a/src/Analyser/NodeScopeResolver.php b/src/Analyser/NodeScopeResolver.php index 86f616953b..90bb01f3ef 100644 --- a/src/Analyser/NodeScopeResolver.php +++ b/src/Analyser/NodeScopeResolver.php @@ -1520,8 +1520,15 @@ private function processStmtNode( } $breakExitPoints = $finalScopeResult->getExitPointsByType(Break_::class); - foreach ($breakExitPoints as $breakExitPoint) { - $finalScope = $finalScope->mergeWith($breakExitPoint->getScope()); + if ($alwaysIterates && count($breakExitPoints) > 0) { + $finalScope = $breakExitPoints[0]->getScope(); + for ($i = 1, $breakExitPointsCount = count($breakExitPoints); $i < $breakExitPointsCount; $i++) { + $finalScope = $finalScope->mergeWith($breakExitPoints[$i]->getScope()); + } + } else { + foreach ($breakExitPoints as $breakExitPoint) { + $finalScope = $finalScope->mergeWith($breakExitPoint->getScope()); + } } $isIterableAtLeastOnce = $beforeCondBooleanType->isTrue()->yes(); @@ -1627,8 +1634,16 @@ private function processStmtNode( } else { $this->processExprNode($stmt, $stmt->cond, $bodyScope, $storage, $nodeCallback, ExpressionContext::createDeep()); } - foreach ($bodyScopeResult->getExitPointsByType(Break_::class) as $breakExitPoint) { - $finalScope = $breakExitPoint->getScope()->mergeWith($finalScope); + $breakExitPoints = $bodyScopeResult->getExitPointsByType(Break_::class); + if ($alwaysIterates && count($breakExitPoints) > 0) { + $finalScope = $breakExitPoints[0]->getScope(); + for ($i = 1, $breakExitPointsCount = count($breakExitPoints); $i < $breakExitPointsCount; $i++) { + $finalScope = $finalScope->mergeWith($breakExitPoints[$i]->getScope()); + } + } else { + foreach ($breakExitPoints as $breakExitPoint) { + $finalScope = $breakExitPoint->getScope()->mergeWith($finalScope); + } } return new InternalStatementResult( @@ -1739,26 +1754,34 @@ private function processStmtNode( $finalScope = $finalScope->filterByFalseyValue($lastCondExpr); } - foreach ($finalScopeResult->getExitPointsByType(Break_::class) as $breakExitPoint) { - $finalScope = $breakExitPoint->getScope()->mergeWith($finalScope); - } - - if ($isIterableAtLeastOnce->no() || $finalScopeResult->isAlwaysTerminating()) { - if ($this->polluteScopeWithLoopInitialAssignments) { - $finalScope = $initScope; - } else { - $finalScope = $scope; + $breakExitPoints = $finalScopeResult->getExitPointsByType(Break_::class); + if ($alwaysIterates->yes() && count($breakExitPoints) > 0) { + $finalScope = $breakExitPoints[0]->getScope(); + for ($i = 1, $breakExitPointsCount = count($breakExitPoints); $i < $breakExitPointsCount; $i++) { + $finalScope = $finalScope->mergeWith($breakExitPoints[$i]->getScope()); + } + } else { + foreach ($breakExitPoints as $breakExitPoint) { + $finalScope = $breakExitPoint->getScope()->mergeWith($finalScope); } - } elseif ($isIterableAtLeastOnce->maybe()) { - if ($this->polluteScopeWithLoopInitialAssignments) { - $finalScope = $finalScope->mergeWith($initScope); + if ($isIterableAtLeastOnce->no() || $finalScopeResult->isAlwaysTerminating()) { + if ($this->polluteScopeWithLoopInitialAssignments) { + $finalScope = $initScope; + } else { + $finalScope = $scope; + } + + } elseif ($isIterableAtLeastOnce->maybe()) { + if ($this->polluteScopeWithLoopInitialAssignments) { + $finalScope = $finalScope->mergeWith($initScope); + } else { + $finalScope = $finalScope->mergeWith($scope); + } } else { - $finalScope = $finalScope->mergeWith($scope); - } - } else { - if (!$this->polluteScopeWithLoopInitialAssignments) { - $finalScope = $finalScope->mergeWith($scope); + if (!$this->polluteScopeWithLoopInitialAssignments) { + $finalScope = $finalScope->mergeWith($scope); + } } } diff --git a/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php b/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php index 112a57d4cc..3fd88eaa29 100644 --- a/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php +++ b/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php @@ -1257,4 +1257,14 @@ public function testBug12944(): void $this->analyse([__DIR__ . '/data/bug-12944.php'], []); } + public function testBug5919(): void + { + $this->cliArgumentsVariablesRegistered = true; + $this->polluteScopeWithLoopInitialAssignments = true; + $this->checkMaybeUndefinedVariables = true; + $this->polluteScopeWithAlwaysIterableForeach = true; + + $this->analyse([__DIR__ . '/data/bug-5919.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Variables/data/bug-5919.php b/tests/PHPStan/Rules/Variables/data/bug-5919.php new file mode 100644 index 0000000000..11c20eb9cf --- /dev/null +++ b/tests/PHPStan/Rules/Variables/data/bug-5919.php @@ -0,0 +1,59 @@ +