Do not let type narrowing define a certainly-undefined variable - #6130
Merged
Conversation
Removing a type from a certainly-undefined variable proves nothing about its definedness, so a sureNot specification must not create a holder for it. A sure specification (e.g. is_string($a)) still can - the condition can only hold for a defined variable, so it makes the variable defined inside the branch: one error at the test site, no cascade. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019wqGgaD7iqL44t1KgpJS7b
This was referenced Jul 28, 2026
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.
Extracted from the
resolve-type-rewrite-2branch (follows #6125 and #6129).filterBySpecifiedTypes()created a type holder for a variable the scope knows is certainly undefined, makingisset()-style narrowing "define" it — the classic false negative where only the first use of an undefined variable in a loop was reported. A sureNot specification now skips certainly-undefined variables; sure specifications still apply, since a condition likeis_string($a)can only hold for a defined variable (one error at the test site, no cascade).Verified test-first: the regression test fails on 2.2.x with the line-9
Undefined variableerror missing and line 15 degraded to "might not be defined"; passes with the fix. Full test suite green (17757 tests), self-analysis clean, no other expectation changes.Closes phpstan/phpstan#2032
🤖 Generated with Claude Code
https://claude.ai/code/session_019wqGgaD7iqL44t1KgpJS7b