-
Notifications
You must be signed in to change notification settings - Fork 568
Fix phpstan/phpstan#14458: Regression: never defined variable #5451
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
ondrejmirtes
merged 3 commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-t7spth4
Apr 13, 2026
+58
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
3ff4932
Fix phpstan/phpstan#14458: Conditional variable incorrectly reported …
phpstan-bot 19ec231
Add comment explaining why Pass 1 does not need No-certainty check
phpstan-bot b23f08f
Revert "Add comment explaining why Pass 1 does not need No-certainty …
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug14458; | ||
|
|
||
| use DateTime; | ||
|
|
||
| class Foo | ||
| { | ||
|
|
||
| /** | ||
| * @param array<mixed> $payload | ||
| * @return array<mixed> | ||
| */ | ||
| public function doFoo(array $payload): array | ||
| { | ||
| if ($payload['a'] !== 'b') { | ||
| throw new \Exception(); | ||
| } | ||
|
|
||
| if (isset($payload['c'])) { | ||
| $c = array_values(array_map(static fn (array $cd) => $cd[0], $payload['c'])); | ||
| } | ||
|
|
||
| $convertedPriceWithVat = null; | ||
| if (array_key_exists('cpwv', $payload)) { | ||
| $convertedAmount = (float) $payload['cpwv']['awv']; | ||
|
|
||
| } | ||
|
|
||
| return [ | ||
| $payload['cf'], | ||
| $payload['n'], | ||
| $payload['d'] ?? null, | ||
| $payload['mb'] ?? null, | ||
| $payload['cr'], | ||
| new DateTime($payload['p']), | ||
| $payload['pn'], | ||
| $payload['pd'] ?? null, | ||
| $payload['piu'] ?? null, | ||
| $convertedPriceWithVat, | ||
| $payload['vi'], | ||
| $payload['pi'], | ||
| $payload['user']['name'] ?? null, | ||
| $payload['user']['phone'] ?? null, | ||
| $payload['ac'] ?? null, | ||
| $c ?? null, | ||
| ]; | ||
| } | ||
|
|
||
| } |
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.
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.
do we need a similar check for the "Pass 1" foreach loop a few lines above?
if so, add a test-case which fails without a similar fix for "Pass 1".
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.
Pushed. Here's a summary of the review response:
staabm's question: Does Pass 1 need a similar
No-certainty check?Answer: No. The key difference is the matching strategy:
Pass 1 uses exact matching (
equals()). If the specified type exactly equals the No branch's condition type, the scope genuinely corresponds to the "variable is undefined" state. Exact matching can't produce the asymmetry that causes the bug — it's impossible for a type to simultaneously equal two different condition types.Pass 2 uses supertype matching (
isSuperTypeOf()), which is inherently asymmetric. A broader condition type (the "undefined" branch, with fewer constraints likearray&hasOffsetValue('a', 'b')) is more likely to be a supertype of a narrowed type than a narrower condition type (the "defined" branch, likearray&hasOffsetValue('a', 'b')&hasOffsetValue('c', ...)). This asymmetry causes only the No-certainty branch to match, incorrectly resolving the variable as never defined.I added a comment explaining this reasoning directly in the code at the Pass 2 check, so future readers understand why the guard is only needed there. All tests (11,853) and
make phpstanpass.