Skip to content

Commit d00caa3

Browse files
WIP
1 parent 3c170f7 commit d00caa3

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,8 +1637,20 @@ private function processStmtNode(
16371637
} else {
16381638
$this->processExprNode($stmt, $stmt->cond, $bodyScope, $storage, $nodeCallback, ExpressionContext::createDeep());
16391639
}
1640-
foreach ($bodyScopeResult->getExitPointsByType(Break_::class) as $breakExitPoint) {
1641-
$finalScope = $breakExitPoint->getScope()->mergeWith($finalScope);
1640+
1641+
$breakExitPoints = $bodyScopeResult->getExitPointsByType(Break_::class);
1642+
if ($alwaysIterates && count($breakExitPoints) > 0) {
1643+
$breakScope = null;
1644+
foreach ($breakExitPoints as $breakExitPoint) {
1645+
$breakScope = $breakScope === null
1646+
? $breakExitPoint->getScope()
1647+
: $breakScope->mergeWith($breakExitPoint->getScope());
1648+
}
1649+
$finalScope = $breakScope;
1650+
} else {
1651+
foreach ($breakExitPoints as $breakExitPoint) {
1652+
$finalScope = $finalScope->mergeWith($breakExitPoint->getScope());
1653+
}
16421654
}
16431655

16441656
return new InternalStatementResult(
@@ -1749,8 +1761,19 @@ private function processStmtNode(
17491761
$finalScope = $finalScope->filterByFalseyValue($lastCondExpr);
17501762
}
17511763

1752-
foreach ($finalScopeResult->getExitPointsByType(Break_::class) as $breakExitPoint) {
1753-
$finalScope = $breakExitPoint->getScope()->mergeWith($finalScope);
1764+
$breakExitPoints = $finalScopeResult->getExitPointsByType(Break_::class);
1765+
if ($alwaysIterates->yes() && count($breakExitPoints) > 0) {
1766+
$breakScope = null;
1767+
foreach ($breakExitPoints as $breakExitPoint) {
1768+
$breakScope = $breakScope === null
1769+
? $breakExitPoint->getScope()
1770+
: $breakScope->mergeWith($breakExitPoint->getScope());
1771+
}
1772+
$finalScope = $breakScope;
1773+
} else {
1774+
foreach ($breakExitPoints as $breakExitPoint) {
1775+
$finalScope = $finalScope->mergeWith($breakExitPoint->getScope());
1776+
}
17541777
}
17551778

17561779
if ($isIterableAtLeastOnce->no() || $finalScopeResult->isAlwaysTerminating()) {

tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,16 +1284,7 @@ public function testBug11545(): void
12841284
$this->checkMaybeUndefinedVariables = true;
12851285
$this->polluteScopeWithAlwaysIterableForeach = true;
12861286

1287-
$this->analyse([__DIR__ . '/data/bug-11545.php'], [
1288-
[
1289-
'Variable $result might not be defined.',
1290-
24,
1291-
],
1292-
[
1293-
'Variable $result might not be defined.',
1294-
36,
1295-
],
1296-
]);
1287+
$this->analyse([__DIR__ . '/data/bug-11545.php'], []);
12971288
}
12981289

12991290
public function testBug10245(): void

0 commit comments

Comments
 (0)