Skip to content

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
phpstan-bot:create-pull-request/patch-l89mgu3
Open

Fix phpstan/phpstan#14311: nullsafe.neverNull false positive: nullsafe on variable assigned from array<K, V> with ??, isset(), or array_key_exists()#5247
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-l89mgu3

Conversation

@phpstan-bot
Copy link
Collaborator

Summary

When using ?-> on a variable that could be null (e.g., $bar = $stations[$key] ?? null; $bar?->address ?? 'Unknown'), PHPStan incorrectly reported nullsafe.neverNull — "Using nullsafe property access is unnecessary. Use -> instead." This happened with ??, isset(), and empty() contexts.

Changes

  • Added a null-check on $expr->var's type in src/Rules/IssetCheck.php before reporting nullsafe.neverNull, matching the logic already used in NullsafePropertyFetchRule
  • Updated test expectations in tests/PHPStan/Rules/Variables/NullCoalesceRuleTest.php — removed 3 false positive expectations from bug-7109 test
  • Updated test expectations in tests/PHPStan/Rules/Variables/IssetRuleTest.php — removed 3 false positive expectations from bug-7109 test
  • Updated test expectations in tests/PHPStan/Rules/Variables/EmptyRuleTest.php — removed 4 false positive expectations from bug-7109 test
  • Added regression test tests/PHPStan/Rules/Variables/data/bug-14311.php covering all three variants (??, isset(), array_key_exists())

Root cause

In IssetCheck::check(), when the expression passed to ??/isset()/empty() was a NullsafePropertyFetch, the code unconditionally reported the nullsafe as unnecessary without checking whether the left side of ?-> could actually be null. The standalone NullsafePropertyFetchRule already had this check ($calledOnType->isNull()->no()), but IssetCheck was 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.php with three functions testing ??, isset(), and array_key_exists() variants. Each function creates a Station|null variable and uses ?->address ?? 'Unknown'. The test expects no errors.

Fixes phpstan/phpstan#14311

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants