Do not narrow the falsey branch of a nested isset() at its intermediate offset - #6175
Merged
Merged
Conversation
…te offset The falsey-branch dim-fetch narrowing in IssetHandler::specifyTypes() was written for a single-level isset(), where the target's var is a plain variable or property. For a nested isset() like isset($r['K']['Port']), the target's var is itself an ArrayDimFetch ($r['K']) that may not exist, and narrowing that intermediate offset in the falsey branch leaks its existence into the enclosing scope, so $r['K'] ?? null lost its |null after the check. Skip the block when the var is itself an offset access. Single-level isset() keeps the narrowing phpstan#4983 added. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
VincentLanglet
approved these changes
Aug 3, 2026
staabm
approved these changes
Aug 3, 2026
staabm
left a comment
Contributor
There was a problem hiding this comment.
since the fix does not break exisiting tests and fixes a false positive, I think we should go for it
Contributor
|
thank you @SanderMuller |
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.
Since 2.1.40 (#4983),
isset()on a nested array offset narrows the intermediate offset in the falsey branch, and that narrowing leaks the offset's existence into the enclosing scope. Afterisset($r['K']['Port']),$r['K']is treated as definitely existing for the rest of the scope, so$r['K'] ?? nullloses its|null.The falsey-branch narrowing block in
IssetHandler::specifyTypes()was written for the single-level case, where the isset target's var is a plain variable or property. For a nested isset likeisset($r['K']['Port']), the target's var is itself anArrayDimFetch($r['K']), an offset that may not exist, and narrowing that intermediate offset in the falsey branch is what leaks its existence outward.This skips the block when the target's var is itself an offset access, which is the case that leaks. Single-level isset (variable or property base) is unchanged, so the narrowing #4983 added still applies where it is sound.
This is the minimal fix. #6110 addresses the same issue by keeping the narrowing where the intermediate offset is known to exist (
isAlwaysSet()); this PR is the smaller alternative, so you can compare the CI impact of both.The regression test covers the reporter's cases (ternary and plain
if), deeper nesting, a property base, and the??and single-level controls that were already correct.Closes phpstan/phpstan#15005