From 1ffed9b90db23e69b306107db302e3a4b8418b83 Mon Sep 17 00:00:00 2001 From: Yohta Kimura <38206553+rajyan@users.noreply.github.com> Date: Sun, 24 Apr 2022 18:10:22 +0900 Subject: [PATCH] Fix/issue 3601 --- src/Analyser/NodeScopeResolver.php | 2 ++ .../Rules/Variables/DefinedVariableRuleTest.php | 9 +++++++++ tests/PHPStan/Rules/Variables/data/bug-3601.php | 15 +++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 tests/PHPStan/Rules/Variables/data/bug-3601.php diff --git a/src/Analyser/NodeScopeResolver.php b/src/Analyser/NodeScopeResolver.php index 4a7a75af33..685b5edf6c 100644 --- a/src/Analyser/NodeScopeResolver.php +++ b/src/Analyser/NodeScopeResolver.php @@ -2383,6 +2383,8 @@ static function (?Type $offsetType, Type $valueType) use (&$arrayType): void { $hasYield = $hasYield || $result->hasYield(); $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); $nonNullabilityResults[] = $nonNullabilityResult; + } + foreach (array_reverse($expr->vars) as $var) { $scope = $this->lookForUnsetAllowedUndefinedExpressions($scope, $var); } foreach (array_reverse($nonNullabilityResults) as $nonNullabilityResult) { diff --git a/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php b/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php index 6349d31808..eb266e470d 100644 --- a/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php +++ b/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php @@ -862,4 +862,13 @@ public function testBug6112(): void $this->analyse([__DIR__ . '/data/bug-6112.php'], []); } + public function testBug3601(): void + { + $this->cliArgumentsVariablesRegistered = true; + $this->polluteScopeWithLoopInitialAssignments = true; + $this->checkMaybeUndefinedVariables = true; + $this->polluteScopeWithAlwaysIterableForeach = true; + $this->analyse([__DIR__ . '/data/bug-3601.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Variables/data/bug-3601.php b/tests/PHPStan/Rules/Variables/data/bug-3601.php new file mode 100644 index 0000000000..4930ab2f49 --- /dev/null +++ b/tests/PHPStan/Rules/Variables/data/bug-3601.php @@ -0,0 +1,15 @@ + 'everything is fine']; +} + +if (isset($a, $c, $c[$a])) { + echo $c[$a]; +}