Repository navigation
Narrow the left side of ?? to non-null when a comparison type rules out the right side
#7096
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.3.x
from
phpstan-bot:create-pull-request/patch-xg16i6v
Nov 17, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
321bac3
Narrow the left side of `??` to non-null when a comparison type rules…
phpstan-bot 817ac42
Add regression test for phpstan/phpstan#9946
phpstan-bot b593928
Reuse the nsrt bug-9946 file in AccessPropertiesRuleTest
phpstan-bot 33e00cb
Initialise the narrowed type of the unwrapped coalesce side up front
phpstan-bot 26a9535
Cover the null removal from the left side of `??` in every context
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
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,97 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug15748; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| function foo(?string $x) { | ||
| if (($x ?? '') == '') | ||
| return null; | ||
| assertType('non-empty-string', $x); | ||
| $y = explode(':', $x); | ||
| // ... | ||
| } | ||
|
|
||
| function bar(?string $x) { | ||
| if (($x ?? '') != '') { | ||
| assertType('non-empty-string', $x); | ||
| } | ||
| } | ||
|
|
||
| function baz(?string $x) { | ||
| if (($x ?? '') === '') | ||
| return null; | ||
| assertType('non-empty-string', $x); | ||
| } | ||
|
|
||
| function qux(?string $x) { | ||
| if (($x ?? '') !== '') { | ||
| assertType('non-empty-string', $x); | ||
| } | ||
| } | ||
|
|
||
| function intCase(?int $x) { | ||
| if (($x ?? 0) == 0) | ||
| return null; | ||
| assertType('int<min, -1>|int<1, max>', $x); | ||
| } | ||
|
|
||
| function intStrictCase(?int $x) { | ||
| if (($x ?? 0) === 0) | ||
| return null; | ||
| assertType('int<min, -1>|int<1, max>', $x); | ||
| } | ||
|
|
||
| class Foo | ||
| { | ||
| public ?string $bar = null; | ||
| } | ||
|
|
||
| /** @param array{k?: string|null} $a */ | ||
| function analogous(array $a, ?string $x, ?int $i, ?bool $b, ?Foo $o) { | ||
| if (($a['k'] ?? '') !== '') { | ||
| assertType('array{k: non-empty-string}', $a); | ||
| } | ||
| if (($i ?? 0) > 0) { | ||
| assertType('int<1, max>', $i); | ||
| } | ||
| if (($b ?? false) !== false) { | ||
| assertType('true', $b); | ||
| } | ||
| if (!in_array($x ?? '', ['', 'foo'], true)) { | ||
| assertType('non-empty-string', $x); | ||
| } | ||
| if (($o?->bar ?? '') !== '') { | ||
| assertType('Bug15748\Foo', $o); | ||
| assertType('non-empty-string', $o->bar); | ||
| } | ||
| if (($x ?? 'a') === null) { | ||
| assertType('*NEVER*', $x); | ||
| } | ||
| if (($x ?? '') !== 'abc') { | ||
| assertType('string|null', $x); | ||
| } | ||
| } | ||
|
|
||
| /** @param int|string|null $z */ | ||
| function typeSpecifyingExtensions(?string $x, $z) { | ||
| if (is_callable($x ?? 1)) { | ||
| assertType('callable-string', $x); | ||
| } | ||
| if (is_callable($x ?? 'strlen') !== true) { | ||
| assertType('string', $x); | ||
| } | ||
| if (!is_array($z ?? [])) { | ||
| assertType('int|string', $z); | ||
| } | ||
| } | ||
|
|
||
| function negatedTrueContext(?string $x, bool $c) { | ||
| if ((($x ?? '') === '') !== true) { | ||
| assertType('non-empty-string', $x); | ||
| } | ||
| $w = $c ? '' : ($x ?? ''); | ||
| if ($w !== '') { | ||
| assertType('non-empty-string', $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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?php // lint >= 8.0 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug9946; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class A { | ||
| public null|int $value = null; | ||
| } | ||
|
|
||
| class B | ||
| { | ||
| public null|A $valA = null; | ||
| } | ||
|
|
||
| $aObj = new A(); | ||
| $bObj = new B(); | ||
|
|
||
| if (($bObj->valA?->value ?? 0) !== 0) { | ||
| assertType('Bug9946\A', $bObj->valA); | ||
| assertType('int<min, -1>|int<1, max>', $bObj->valA->value); | ||
| $test = $bObj->valA->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
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.
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.
PHPStan reports this
?->access is not necessary.we should assert the types when people don't use
?->in addition: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.
I added the case you suggested, without
?->, totests/PHPStan/Analyser/nsrt/bug-9946.php. It failed: PHPStan inferredBug9946\A|nullfor$bObj->valA, and the rule test reported "Cannot access property $value on Bug9946\A|null" twice. So I fixed that as well, in one local commit (c45842962). It isn't pushed.The gap:
($bObj->valA->value ?? 0) !== 0narrowed$bObj->valA->value, but not$bObj->valA. Without the fix,($b->a->value ?? null) !== nulldoesn't narrow$b->aeither. If the comparison rules out the right side of??, the left side was set. So$bObj->valAcan't be null, which is whatisset($bObj->valA->value)already concludes.The fix:
src/Analyser/ExprHandler/CoalesceHandler.php: increateTypesCallback, when the comparison type rules the right side in or out, the result now also includes the narrowing thatisset()applies to every link in the left chain (createIssetTruthyChainTypes).turbo-ext/src/CoalesceHandler.cpp: the same change in the native version. ItscreateTypesCallbacknow also captures$chainResults, like the PHP closure.Checks:
bug-9946.phpandAccessPropertiesRuleTest::testBug9946failed before the fix and pass after it, with and without the extension.bug-15748.phpstill passes.make testspasses (23863 tests, 116 skipped). The full suite also passes with the turbo extension loaded.tests/smoke.phpprints ALL OK.make phpstanreports no errors.One case is still not narrowed: a bare truthy check like
if ($b->a->value ?? false)leaves$b->aasA|null. It goes through a different callback, so I left it out of this PR.I couldn't see the details of the mutant from the earlier CI run. If the mutation testing job still reports it after this push, the log will show which line needs another test.