-
Notifications
You must be signed in to change notification settings - Fork 554
Fix phpstan/phpstan#14117: Incorrect 'Variable $value might not be defined.' #5125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
staabm
merged 7 commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-c1hi1rz
Mar 4, 2026
+338
−2
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7259b57
Fix phpstan/phpstan#14117: Variable might not be defined false positi…
phpstan-bot 9f0b378
regression test
staabm 2b68f49
regression test
staabm 0d3dd3a
added test
staabm 76fd431
added test
staabm 709fbd0
cheap check first
staabm 4fcbc97
more tests
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <?php | ||
|
|
||
| namespace Bug10657; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| public function sayHello(bool $flag): void | ||
| { | ||
| for ($i = 0; $i < 10; $i++) { | ||
| if ($flag) { | ||
| $foo = 'bar'; | ||
| } | ||
| if ($flag) { | ||
| echo $foo; | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug14117; | ||
|
|
||
| function foo(): void { | ||
| $key = rand(0, 2); | ||
|
|
||
| if ($key === 2) { | ||
| $value = 'test'; | ||
| } | ||
|
|
||
| if ($key === 1) { | ||
| $value = 'test'; | ||
| } | ||
|
|
||
| if ($key === 1) { | ||
| echo $value; | ||
| } | ||
| } | ||
|
|
||
| function bar(): void { | ||
| $key = rand(0, 2); | ||
|
|
||
| if ($key === 2) { | ||
| $value = 'two'; | ||
| } | ||
|
|
||
| if ($key === 1) { | ||
| $value = 'one'; | ||
| } | ||
|
|
||
| if ($key === 2) { | ||
| echo $value; | ||
| } | ||
| } | ||
|
|
||
| function baz(): void { | ||
| $key = rand(0, 3); | ||
|
|
||
| if ($key === 1) { | ||
| $value = 'one'; | ||
| } | ||
|
|
||
| if ($key === 2) { | ||
| $value = 'two'; | ||
| } | ||
|
|
||
| if ($key === 3) { | ||
| echo $value; // SHOULD report "is not defined" | ||
| } | ||
| } | ||
|
|
||
| function boo(): void { | ||
| $key = rand(0, 2); | ||
|
|
||
| if ($key === 1) { | ||
| $value = 'test'; | ||
| } | ||
|
|
||
| if ($key === 1) { | ||
| unset($value); | ||
| } | ||
|
|
||
| if ($key === 1) { | ||
| echo $value; | ||
| } | ||
| } | ||
|
|
||
| function loo(): void { | ||
| $key = rand(0, 2); | ||
|
|
||
| if ($key === 1) { | ||
| $value = 'test'; | ||
| } | ||
|
|
||
| if ($key === 1 || $key === 2) { | ||
| unset($value); | ||
| } | ||
|
|
||
| if ($key === 1) { | ||
| echo $value; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Bug6830; | ||
|
|
||
| /** @param array<bool> $bools */ | ||
| function test(array $bools): void | ||
| { | ||
| foreach ($bools as $bool) { | ||
| if ($bool) { | ||
| $foo = 'foo'; | ||
| } | ||
| if ($bool) { | ||
| echo $foo; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Bug8430; | ||
|
|
||
| class B | ||
| { | ||
| public function abc(string $a, bool $b): void | ||
| { | ||
| for ($i = 1; $i <= 5; $i++) { | ||
| if (!$b) { | ||
| $arr = ['a' => 1]; | ||
| } | ||
| if (!$a && !$b) { | ||
| echo $arr['a']; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public function def(string $a, bool $b): void | ||
| { | ||
| if (!$b) { | ||
| $arr = ['a' => 1]; | ||
| } | ||
| if (!$a && !$b) { | ||
| echo $arr['a']; | ||
| } | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Bug8430b; | ||
|
|
||
| class A | ||
| { | ||
| /** @var A[][] */ | ||
| public array $a; | ||
| /** @var A[] */ | ||
| public array $b; | ||
| /** @var array<string,string|string[]> */ | ||
| public array $c; | ||
| public string $d; | ||
| } | ||
|
|
||
| class B | ||
| { | ||
| private function abc(): void | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * @param A[] $a | ||
| * @param array<string,string> $b | ||
| */ | ||
| public function def(array $a, array $b, string $c, bool $d): void | ||
| { | ||
| $e = false; | ||
| $f = false; | ||
| switch ($b['repeat'] ?? null) { | ||
| case 'Y': | ||
| $e = true; | ||
| break; | ||
| case 'A': | ||
| $e = true; | ||
| $f = true; | ||
| break; | ||
| } | ||
| $g = 5; | ||
| $h = 0; | ||
| for ($i = 1; $i <= $g; $i++) { | ||
| if (!$d) { | ||
| $arr = ['a' => 1]; | ||
| } | ||
| $j = $a[$i] ?? null; | ||
| if ($j) { | ||
| /** @var array<A[]> $k */ | ||
| $k = []; | ||
| if ($e) { | ||
| foreach ($j->a as $l) { | ||
| /** @var A[] $m */ | ||
| $m = $f || empty($k) ? $j->b : []; | ||
| array_push($m, ...$l); | ||
| array_push($k, $m); | ||
| } | ||
| if (empty($k)) { | ||
| array_push($k, $j->b); | ||
| } | ||
| } else { | ||
| array_push($k, $j->b); | ||
| foreach ($j->a as $l) { | ||
| array_push($k[0], ...$l); | ||
| break; | ||
| } | ||
| } | ||
| foreach ($k as $n) { | ||
| if (!$d) { | ||
| foreach ($n as $o) { | ||
| switch ($o->c['x'] ?? '') { | ||
| case 'y': | ||
| $p = $o->c[$o->d] ?? null; | ||
| if (is_array($p)) { | ||
| $this->abc(); | ||
| } | ||
| break; | ||
| case 'z': | ||
| $p = $o->c[$o->d] ?? null; | ||
| if (is_array($p)) { | ||
| $this->abc(); | ||
| } | ||
| break; | ||
| default: | ||
| $this->abc(); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| if (!empty($n)) { | ||
| $h++; | ||
| } | ||
| } | ||
| } | ||
| if (!$c && !$d) { | ||
| echo $arr['a']; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public function ghi(string $a, bool $b): void | ||
| { | ||
| if (!$b) { | ||
| $arr = ['a' => 1]; | ||
| } | ||
| $this->abc(); | ||
| if (!$a && !$b) { | ||
| echo $arr['a']; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in this file I added a few more tests which e.g. also check the interaction with
unset().while doing so I found a case which does not yet work: phpstan/phpstan#14227