-
Notifications
You must be signed in to change notification settings - Fork 569
Do not create conditional expressions for non-Variable expressions absent from the other branch #5465
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
Open
phpstan-bot
wants to merge
4
commits into
phpstan:2.1.x
Choose a base branch
from
phpstan-bot:create-pull-request/patch-hn7ahpk
base: 2.1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+141
−0
Open
Do not create conditional expressions for non-Variable expressions absent from the other branch #5465
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
32c94b9
Do not create conditional expressions for non-Variable expressions ab…
VincentLanglet 5857b59
Add comment explaining Variable exception and add variable-equivalent…
phpstan-bot 96cbec6
Remove comments and use @param mixed for PHP 7.4 compatibility
phpstan-bot 8f158b1
Use @param mixed instead of native mixed type for PHP 7.4 compat, rem…
phpstan-bot 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| <?php | ||
|
|
||
| namespace Bug14469Nsrt; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| function t(array $R, bool $var1, object $user): void { | ||
| $aa = null; | ||
|
|
||
| if ($var1) { | ||
| $aa = $user->id === 10 ? 2 : null; | ||
| } elseif ($R['aa']) { | ||
| $aa = $R['aa']; | ||
| } | ||
|
|
||
| if ($aa) { | ||
| assertType('mixed', $R['aa']); | ||
| } | ||
| } | ||
|
|
||
| /** @param mixed $input */ | ||
| function variableEquivalent($input, bool $var1, object $user): void { | ||
| $aa = null; | ||
| $bb = $input; | ||
|
|
||
| if ($var1) { | ||
| $aa = $user->id === 10 ? 2 : null; | ||
| } elseif ($bb) { | ||
| $aa = $bb; | ||
| } | ||
|
|
||
| if ($aa) { | ||
| assertType('mixed', $bb); | ||
| } | ||
| } |
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,91 @@ | ||
| <?php | ||
|
|
||
| namespace Bug14469; | ||
|
|
||
| function t(array $R, bool $var1, object $user, bool $is): array { | ||
| $aa = null; | ||
|
|
||
| if ($var1) { | ||
| $aa = $user->id === 10 ? 2 : null; | ||
| } elseif ($R['aa']) { | ||
| $aa = $R['aa']; | ||
| } | ||
|
|
||
| if ($aa) { | ||
| if (!$R['aa']) { | ||
| return []; | ||
| } | ||
| } | ||
| return $R; | ||
| } | ||
|
|
||
| /** Property fetch variant */ | ||
| function propertyFetch(object $obj, bool $var1, object $user): void { | ||
| $aa = null; | ||
|
|
||
| if ($var1) { | ||
| $aa = $user->id === 10 ? 2 : null; | ||
| } elseif ($obj->prop) { | ||
| $aa = $obj->prop; | ||
| } | ||
|
|
||
| if ($aa) { | ||
| if (!$obj->prop) { | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** Nested array fetch variant */ | ||
| function nestedArrayFetch(array $R, bool $var1, object $user): void { | ||
| $aa = null; | ||
|
|
||
| if ($var1) { | ||
| $aa = $user->id === 10 ? 2 : null; | ||
| } elseif ($R['a']['b']) { | ||
| $aa = $R['a']['b']; | ||
| } | ||
|
|
||
| if ($aa) { | ||
| if (!$R['a']['b']) { | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** Multiple elseif branches */ | ||
| function multipleElseif(array $R, bool $var1, bool $var2, object $user): void { | ||
| $aa = null; | ||
|
|
||
| if ($var1) { | ||
| $aa = $user->id === 10 ? 2 : null; | ||
| } elseif ($var2) { | ||
| $aa = 5; | ||
| } elseif ($R['aa']) { | ||
| $aa = $R['aa']; | ||
| } | ||
|
|
||
| if ($aa) { | ||
| if (!$R['aa']) { | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** @param mixed $input */ | ||
| function variableEquivalent(bool $var1, object $user, $input): void { | ||
| $aa = null; | ||
| $bb = $input; | ||
|
|
||
| if ($var1) { | ||
| $aa = $user->id === 10 ? 2 : null; | ||
| } elseif ($bb) { | ||
| $aa = $bb; | ||
| } | ||
|
|
||
| if ($aa) { | ||
| if (!$bb) { | ||
| return; | ||
| } | ||
| } | ||
| } |
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.
Why is it restricted for
!$holder->getExpr() instanceof Variable?Can't the same bug occurs in that case ?
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.
The changes have been pushed. Here's a summary of what was done to address VincentLanglet's review comment:
The reviewer's question: Why is the check restricted to
!$holder->getExpr() instanceof Variable? Can't the same bug occur for Variables?Investigation and answer:
I tested the Variable case empirically:
Pre-defined Variables (defined before the if/elseif, present in both branches' expression types): These are handled correctly by the existing check at lines 3644-3651 which tests
array_key_exists($exprString, $theirExpressionTypes). I verified this with a variable-equivalent test case ($bb = $inputbefore the if/elseif) - no false positive occurs.Newly-defined Variables (only defined in one branch, NOT in
$theirExpressionTypes): Removing the Variable exception causes a regression inbug-14411-regression.phpwhere$ordermust be recognized as always-defined when two conditions ($event instanceof Eventand$event instanceof OrderInterface) together cover all cases. Conditional expressions for newly-defined variables are essential for this certainty tracking.The fundamental difference is that non-Variable expressions are "always queryable" (absence from a branch means "not narrowed"), while Variables have genuine defined/undefined semantics (absence means "not defined").
Changes made: