-
Notifications
You must be signed in to change notification settings - Fork 574
Project IssetExpr certainty narrowing through ternary-assigned variables via conditional expressions
#5742
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
VincentLanglet
merged 5 commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-4jd8kmo
May 24, 2026
Merged
Project IssetExpr certainty narrowing through ternary-assigned variables via conditional expressions
#5742
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1790653
Project `IssetExpr` certainty narrowing through ternary-assigned vari…
VincentLanglet a504f51
Address review: merge fixture files and fix comment placement
phpstan-bot 96205b8
Extract addConditionalExpressionHolder to deduplicate holder creation
phpstan-bot 40e28ac
Replace by-ref parameter with return type in addConditionalExpression…
phpstan-bot a0e56f0
Add test case for variable that is either not defined or null
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
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,102 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug10090; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
| use function PHPStan\Testing\assertVariableCertainty; | ||
| use PHPStan\TrinaryLogic; | ||
|
|
||
| function doFoo(): void { | ||
| if (rand(0,1)) { | ||
| $shortcut_id = 1; | ||
| } | ||
|
|
||
| $link_mode = isset($shortcut_id) ? "remove" : "add"; | ||
| assertType("'add'|'remove'", $link_mode); | ||
|
|
||
| if ($link_mode === "add") { | ||
| assertVariableCertainty(TrinaryLogic::createNo(), $shortcut_id); | ||
| echo $shortcut_id; | ||
| } | ||
| if ($link_mode === "remove") { | ||
| assertVariableCertainty(TrinaryLogic::createYes(), $shortcut_id); | ||
| assertType('1', $shortcut_id); | ||
| } | ||
| } | ||
|
|
||
| function nullableVariable(): void { | ||
| if (rand(0,1)) { | ||
| $x = rand(0,1) ? 'hello' : null; | ||
| } | ||
|
|
||
| $mode = isset($x) ? "found" : "missing"; | ||
|
|
||
| if ($mode === "missing") { | ||
| assertVariableCertainty(TrinaryLogic::createMaybe(), $x); | ||
| echo $x; | ||
| } | ||
| if ($mode === "found") { | ||
| assertVariableCertainty(TrinaryLogic::createYes(), $x); | ||
| assertType("'hello'", $x); | ||
| } | ||
| } | ||
|
|
||
| function definitelyDefined(): void { | ||
| $x = rand(0,1) ? 'hello' : null; | ||
|
|
||
| $mode = isset($x) ? "found" : "missing"; | ||
|
|
||
| if ($mode === "missing") { | ||
| assertVariableCertainty(TrinaryLogic::createYes(), $x); | ||
| assertType('null', $x); | ||
| echo $x; | ||
| } | ||
| if ($mode === "found") { | ||
| assertVariableCertainty(TrinaryLogic::createYes(), $x); | ||
| assertType("'hello'", $x); | ||
| } | ||
| } | ||
|
|
||
| function shortTernary(): void { | ||
| if (rand(0,1)) { | ||
| $x = 1; | ||
| } | ||
|
|
||
| $mode = isset($x) ? "remove" : "add"; | ||
| if ($mode !== "add") { | ||
| assertVariableCertainty(TrinaryLogic::createYes(), $x); | ||
| assertType('1', $x); | ||
| } | ||
| } | ||
|
|
||
| function notDefinedOrNull(): void { | ||
| if (rand(0,1)) { | ||
| $x = null; | ||
| } | ||
|
|
||
| $mode = isset($x) ? "found" : "missing"; | ||
|
|
||
| if ($mode === "missing") { | ||
| assertVariableCertainty(TrinaryLogic::createMaybe(), $x); | ||
| echo $x; | ||
| } | ||
| if ($mode === "found") { | ||
| assertVariableCertainty(TrinaryLogic::createYes(), $x); | ||
| assertType('*NEVER*', $x); | ||
| } | ||
| } | ||
|
|
||
| function withBoolean(): void { | ||
| if (rand(0,1)) { | ||
| $x = 1; | ||
| } | ||
|
|
||
| $exists = isset($x); | ||
| if (!$exists) { | ||
| assertVariableCertainty(TrinaryLogic::createNo(), $x); | ||
| } | ||
| if ($exists) { | ||
| assertVariableCertainty(TrinaryLogic::createYes(), $x); | ||
| assertType('1', $x); | ||
| } | ||
| } | ||
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.
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.
Add another test in which a variable is either not defined, or has the value NULL
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. Added a
notDefinedOrNull()test function where$xis conditionally assignednull(so it's either not defined ornull). In this case:isset($x)is false in both paths (not defined, or defined as null)$xhasMaybecertainty (could be defined-as-null or not defined)$xhasYescertainty but type*NEVER*(logically unreachable since isset can never be true)