diff --git a/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php b/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php index daf1eec4f64..b6cc5f03bc3 100644 --- a/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php +++ b/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php @@ -1517,6 +1517,15 @@ public function testBug11218(): void $this->analyse([__DIR__ . '/data/bug-11218.php'], []); } + public function testBug4352(): void + { + $this->cliArgumentsVariablesRegistered = true; + $this->polluteScopeWithLoopInitialAssignments = false; + $this->checkMaybeUndefinedVariables = true; + $this->polluteScopeWithAlwaysIterableForeach = true; + $this->analyse([__DIR__ . '/data/bug-4352.php'], []); + } + public function testBug6688(): void { $this->cliArgumentsVariablesRegistered = true; diff --git a/tests/PHPStan/Rules/Variables/data/bug-4352.php b/tests/PHPStan/Rules/Variables/data/bug-4352.php new file mode 100644 index 00000000000..8839b32ca0b --- /dev/null +++ b/tests/PHPStan/Rules/Variables/data/bug-4352.php @@ -0,0 +1,37 @@ + 2) { + $a = $b; + $found = true; + } +} + +if ($found) { + /** @var int $a */ + echo $a; +} + +class A { + public function foo(): void { + $foo = [rand()]; + $found = false; + + foreach ($foo as $b) { + if ($b > 2) { + $a = $b; + $found = true; + } + } + + if ($found) { + /** @var int $a */ + echo $a; + } + } +}