From 451d5fbc7b37650d5a14f92047d8ee9544da57ed Mon Sep 17 00:00:00 2001 From: Yohta Kimura Date: Sun, 24 Apr 2022 17:25:24 +0900 Subject: [PATCH 1/2] add failing test case for multiple isset var undefined variable false positive --- .../Rules/Variables/DefinedVariableRuleTest.php | 9 +++++++++ tests/PHPStan/Rules/Variables/data/bug-3601.php | 15 +++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 tests/PHPStan/Rules/Variables/data/bug-3601.php 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]; +} From 214d4327464a37d926a417ace79c2067a24664fb Mon Sep 17 00:00:00 2001 From: Yohta Kimura Date: Sun, 24 Apr 2022 17:29:42 +0900 Subject: [PATCH 2/2] unset currentlyAllowedUndefinedExpression after isset scope ends --- src/Analyser/NodeScopeResolver.php | 2 ++ 1 file changed, 2 insertions(+) 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) {