Fix phpstan/phpstan#14311: nullsafe.neverNull false positive: nullsafe on variable assigned from array<K, V> with ??, isset(), or array_key_exists()#5247
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…ect can be null - In IssetCheck, the nullsafe.neverNull error was reported unconditionally for NullsafePropertyFetch inside ??, isset(), and empty(), without checking if the left side of ?-> could actually be null - Added a check on $expr->var's type before reporting the error, consistent with how NullsafePropertyFetchRule already handles this - Updated test expectations for bug-7109 in NullCoalesceRuleTest, IssetRuleTest, and EmptyRuleTest to remove false positive expectations - New regression test in tests/PHPStan/Rules/Variables/data/bug-14311.php
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.
Summary
When using
?->on a variable that could be null (e.g.,$bar = $stations[$key] ?? null; $bar?->address ?? 'Unknown'), PHPStan incorrectly reportednullsafe.neverNull— "Using nullsafe property access is unnecessary. Use -> instead." This happened with??,isset(), andempty()contexts.Changes
$expr->var's type insrc/Rules/IssetCheck.phpbefore reportingnullsafe.neverNull, matching the logic already used inNullsafePropertyFetchRuletests/PHPStan/Rules/Variables/NullCoalesceRuleTest.php— removed 3 false positive expectations from bug-7109 testtests/PHPStan/Rules/Variables/IssetRuleTest.php— removed 3 false positive expectations from bug-7109 testtests/PHPStan/Rules/Variables/EmptyRuleTest.php— removed 4 false positive expectations from bug-7109 testtests/PHPStan/Rules/Variables/data/bug-14311.phpcovering all three variants (??,isset(),array_key_exists())Root cause
In
IssetCheck::check(), when the expression passed to??/isset()/empty()was aNullsafePropertyFetch, the code unconditionally reported the nullsafe as unnecessary without checking whether the left side of?->could actually be null. The standaloneNullsafePropertyFetchRulealready had this check ($calledOnType->isNull()->no()), butIssetCheckwas missing it. This gave contradictory advice — PHPStan would tell users to replace?->with->, but then would report an error about accessing a property on a nullable type.Test
Added
tests/PHPStan/Rules/Variables/data/bug-14311.phpwith three functions testing??,isset(), andarray_key_exists()variants. Each function creates aStation|nullvariable and uses?->address ?? 'Unknown'. The test expects no errors.Fixes phpstan/phpstan#14311