-
Notifications
You must be signed in to change notification settings - Fork 578
Skip degenerate no-op antecedents when building boolean conditional expression holders #5951
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 2 commits into
phpstan:2.2.x
from
phpstan-bot:create-pull-request/patch-z32uxw4
Jun 29, 2026
+107
−1
Merged
Changes from all commits
Commits
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,89 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug14878; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| // The `$b === true` and `$b === true && $cond` statements inside the branches | ||
| // are what regressed: with the bug $b was narrowed to `mixed~true`, so they | ||
| // emitted "Strict comparison ... will always evaluate to false" and "Result of | ||
| // && is always false". They are referenced from BooleanAndConstantConditionRuleTest | ||
| // and StrictComparisonOfDifferentTypesRuleTest as a regression guard. | ||
|
|
||
| function test($a, $b, $cond): void | ||
| { | ||
| if ( | ||
| in_array($a, [1, 2]) | ||
| && $b === true) | ||
| { | ||
|
|
||
| } elseif ( | ||
| $a == 3) { | ||
| assertType('mixed', $b); | ||
|
|
||
| $b === true; | ||
| $result = $b === true && $cond; | ||
| } | ||
| } | ||
|
|
||
| function testStrictElseIf($a, $b, $cond): void | ||
| { | ||
| if ( | ||
| in_array($a, [1, 2]) | ||
| && $b === true) | ||
| { | ||
|
|
||
| } elseif ( | ||
| $a === 3) { | ||
| assertType('mixed', $b); | ||
|
|
||
| $b === true; | ||
| $result = $b === true && $cond; | ||
| } | ||
| } | ||
|
|
||
| function testPlainElse($a, $b, $cond): void | ||
| { | ||
| if ( | ||
| in_array($a, [1, 2]) | ||
| && $b === true) | ||
| { | ||
|
|
||
| } else { | ||
| assertType('mixed', $b); | ||
|
|
||
| $b === true; | ||
| $result = $b === true && $cond; | ||
| } | ||
| } | ||
|
|
||
| // Same degenerate-condition pattern without in_array: a loose `==` whose | ||
| // falsey narrowing of $a is a no-op on a `mixed` type. | ||
| function testLooseEqual($a, $b, $cond): void | ||
| { | ||
| if ( | ||
| ($a == 1 || $a == 2) | ||
| && $b === true) | ||
| { | ||
|
|
||
| } elseif ($a == 3) { | ||
| assertType('mixed', $b); | ||
|
|
||
| $b === true; | ||
| $result = $b === true && $cond; | ||
| } | ||
| } | ||
|
|
||
| // A genuinely conditional holder must still fire: $a === 1 makes the strict | ||
| // in_array() antecedent true, so $b === true must have been false. | ||
| function testMeaningfulHolderStillFires($a, $b): void | ||
| { | ||
| if ( | ||
| in_array($a, [1, 2], true) | ||
| && $b === true) | ||
| { | ||
|
|
||
| } elseif ($a === 1) { | ||
| assertType('mixed~true', $b); | ||
| } | ||
| } | ||
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
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.
we should reference this file from *RuleTest classes to make sure no emited error like
or
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.
Done. I addressed staabm's review.
What the reviewer asked
Reference
bug-14878.phpfrom*RuleTestclasses so a regression is caught by the actual rules that emitted the wrong errors — "Result of && is always false/true" and "Strict comparison using === … will always evaluate to …".What I changed
BooleanAndConstantConditionRuleTestandStrictComparisonOfDifferentTypesRuleTest— addedtestBug14878()analysingnsrt/bug-14878.phpand expecting no errors.nsrt/bug-14878.php— the original data file's branches only containedassertType()calls, so referencing it from the rule tests would have passed even with the fix reverted (no comparison in the branch ⇒ no rule error). I added the statements that actually regressed, inside each no-narrowing branch:I had to pick operands carefully so the guards aren't constant for the wrong reason:
$a === 3becomes3 === 3once$ais narrowed in the strict elseif, and the right operand of&&sees$bnarrowed totruefrom the left — so I used a separatemixed$condparameter for the right side.Verification
I reconstructed the reproducer locally (the playground wasn't reachable) to confirm the exact errors, then proved the tests are real guards by reverting the source fix:
BooleanAndConstantConditionRuleTest+StrictComparisonOfDifferentTypesRuleTest(112 tests) pass;make phpstanclean.$bismixed~true),StrictComparisonreports "will always evaluate to false", andBooleanAndreports "Result of && is always false" — exactly the errors the reviewer wanted guarded. (ThetestPlainElsecase correctly stays clean even with the bug, matching the PR's note that the plain-elsevariant was already correct.)Committed as
cf817daa8and pushed.