Fix false positive nullsafe.neverNull on left side of ??#5211
Closed
kemo wants to merge 1 commit intophpstan:2.1.xfrom
Closed
Fix false positive nullsafe.neverNull on left side of ??#5211kemo wants to merge 1 commit intophpstan:2.1.xfrom
kemo wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
19dd4b0 to
9430320
Compare
The nullsafe operator ?-> on the left side of ?? was unconditionally reported as unnecessary. This is correct for isset()/empty() which handle null objects natively, but wrong for ?? which does not catch TypeError from property access on null objects.
9430320 to
2284215
Compare
Contributor
VincentLanglet
left a comment
There was a problem hiding this comment.
Please provides a php snippet with leads to an error.
| { | ||
| $this->analyse([__DIR__ . '/../Properties/data/bug-7109.php'], [ | ||
| [ | ||
| 'Using nullsafe property access "?->aaa" on left side of ?? is unnecessary. Use -> instead.', |
Contributor
There was a problem hiding this comment.
I have no error with
https://3v4l.org/5TbIf#v8.3.30
so the ?-> seems useless.
Author
|
Closing this — after testing actual PHP behavior, I confirmed that |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
IssetCheckunconditionally reports?->property access as unnecessary (nullsafe.neverNull) when it appears on the left side of??,isset(), orempty().This is correct for
isset()/empty()— PHP handles null objects natively in those constructs without throwing. But for??, it is a false positive: the null coalescing operator does not catchTypeErrorfrom property access on a null object.Root cause
In
IssetCheck::check(), theNullsafePropertyFetchblock (line 254) fires unconditionally regardless of context. It doesn't distinguish between??(where?->preventsTypeError) andisset()/empty()(where PHP already handles null objects).Fix
When the identifier is
nullCoalesce, check whether the object being accessed ($expr->var) has a nullable type. If it does, the nullsafe operator is genuinely needed and no error should be reported.isset()andempty()behavior is unchanged —?->remains correctly flagged as unnecessary there since PHP handles null objects natively in those constructs.Test changes
NullCoalesceRuleTest::testBug7109()(lines 17, 28, 66 of the test data file)testNullsafeCoalesceNullableObject()covering:?->on left of??(no error expected)?->(no error expected)?->(correctly reports "Expression not nullable")