diff --git a/src/Rules/Variables/DefinedVariableRule.php b/src/Rules/Variables/DefinedVariableRule.php index 8fbb1e5d0b..f33e371fbe 100644 --- a/src/Rules/Variables/DefinedVariableRule.php +++ b/src/Rules/Variables/DefinedVariableRule.php @@ -41,6 +41,11 @@ public function getNodeType(): string public function processNode(Node $node, Scope $scope): array { $errors = []; + + if ($scope->isUndefinedExpressionAllowed($node)) { + return []; + } + if (is_string($node->name)) { $variableNameScopes = [$node->name => $scope]; } else { @@ -78,7 +83,7 @@ private function processSingleVariable(Scope $scope, Variable $node, string $var } } - if ($scope->isInExpressionAssign($node) || $scope->isUndefinedExpressionAllowed($node)) { + if ($scope->isInExpressionAssign($node)) { return []; } diff --git a/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php b/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php index 96b89cf070..c1b910789a 100644 --- a/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php +++ b/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php @@ -1169,4 +1169,19 @@ public function testBug8719(): void $this->analyse([__DIR__ . '/data/bug-8719.php'], []); } + public function testBug13353(): void + { + $this->cliArgumentsVariablesRegistered = true; + $this->polluteScopeWithLoopInitialAssignments = true; + $this->checkMaybeUndefinedVariables = true; + $this->polluteScopeWithAlwaysIterableForeach = true; + + $this->analyse([__DIR__ . '/data/bug-13353.php'], [ + [ + 'Variable $bar might not be defined.', + 11, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/Variables/data/bug-13353.php b/tests/PHPStan/Rules/Variables/data/bug-13353.php new file mode 100644 index 0000000000..ef13d0635f --- /dev/null +++ b/tests/PHPStan/Rules/Variables/data/bug-13353.php @@ -0,0 +1,11 @@ +