Treat a property with a default value as always set in isset resolution - #6131
Merged
Conversation
issetCheck() treated every native non-virtual property without a tracked expression type as possibly uninitialized. A property with a default value cannot be uninitialized, so consult hasDefaultValue() in the guard. This restores narrowing through ?? (the coalesce only builds the left-is-null scope for its right side when the left is surely set) and makes isset() on a defaulted non-nullable property infer true. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019wqGgaD7iqL44t1KgpJS7b
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019wqGgaD7iqL44t1KgpJS7b
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, #6129, #6130).MutatingScope::issetCheck()treated every native non-virtual property without a tracked expression type as possibly uninitialized — including properties with a default value, which can never be uninitialized. The guard now consultshasDefaultValue().Effects, verified test-first (new
nsrt/isset-property-default-value.phpfails on 2.2.x withint|null, passes with the fix):$a->value ?? $b->valuenarrows through an earlieris_null && is_nullthrow guard again (the coalesce only builds the left-is-null scope for its right side when the left is surely set).isset($foo->hasDefaultValue)on a defaulted non-nullable property now inferstrue— one expectation updated innsrt/isset-coalesce-empty-type.php, consistent with the adjacent assigned-before case; a no-default contrast case in the new fixture pins that uninitialized properties keep the conservativebool.Full test suite green (17764 tests), self-analysis clean, code style clean.
🤖 Generated with Claude Code
https://claude.ai/code/session_019wqGgaD7iqL44t1KgpJS7b
Note: the rule-side mirror of this logic in
Rules\IssetCheckalready consultshasDefaultValue()since 2f58f5f (#5327, phpstan/phpstan#14393) — this PR is theMutatingScope::issetCheck()catch-up, so type inference now agrees with what the isset/empty/coalesce rules already report.Closes phpstan/phpstan#10786