Skip to content

Commit

Permalink
make polluteScopeWithAlwaysIterableForeach stricter for foreach key a…
Browse files Browse the repository at this point in the history
…nd value
  • Loading branch information
rajyan committed Dec 7, 2022
1 parent 2b97645 commit 4077165
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,11 @@ private function processStmtNode(
$finalScope = $finalScope->mergeWith($scope);
}
} elseif (!$this->polluteScopeWithAlwaysIterableForeach) {
$finalScope = $scope->processAlwaysIterableForeachScopeWithoutPollute($finalScope);
$finalScope = $scope->processAlwaysIterableForeachScopeWithoutPollute($finalScope)
->invalidateExpression($stmt->valueVar);
if ($stmt->keyVar !== null) {
$finalScope = $finalScope->invalidateExpression($stmt->keyVar);
}
// get types from finalScope, but don't create new variables
}

Expand Down
10 changes: 5 additions & 5 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -514,23 +514,23 @@ public function dataForeachPolluteScopeWithAlwaysIterableForeach(): array
10,
],
[
'Variable $key might not be defined.',
'Undefined variable: $key',
19,
],
[
'Variable $val might not be defined.',
'Undefined variable: $val',
20,
],
[
'Variable $test might not be defined.',
21,
],
[
'Variable $key might not be defined.',
'Undefined variable: $key',
32,
],
[
'Variable $val might not be defined.',
'Undefined variable: $val',
33,
],
[
Expand All @@ -554,7 +554,7 @@ public function dataForeachPolluteScopeWithAlwaysIterableForeach(): array
62,
],
[
'Variable $key might not be defined.',
'Undefined variable: $key',
75,
],
[
Expand Down

0 comments on commit 4077165

Please sign in to comment.