Fix #6120: Null coalesce non-null result implies the caller not to be null - not reflected in consequent conditions#4989
Closed
phpstan-bot wants to merge 1 commit into2.1.xfrom
Closed
Conversation
- When assigning from a nullsafe expression (e.g. $result = $clazz?->foo),
checking $result !== null now correctly narrows $clazz to non-null
- Added non-null/null conditional expressions in processAssignVar alongside
the existing truthy/falsey ones, so !== null checks trigger narrowing
- Updated bug-13546 test: array_key_first assignment narrowing now correctly
narrows to non-empty-list/array{} when checking result !== null
- Removed baseline entry for ClassReflection::getCacheKey() which is now
correctly resolved by the improved narrowing
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 a nullsafe property access or method call result is assigned to a variable (e.g.,
$result = $clazz?->foo) and then checked against null (if ($result !== null)), PHPStan did not narrow the caller ($clazz) to non-null. This fix adds that narrowing.Changes
src/Analyser/NodeScopeResolver.php(inprocessAssignVar), alongside the existing truthy/falsey conditional expressions$assignedExpr !== nulland$assignedExpr === nullthrough the TypeSpecifiertests/PHPStan/Analyser/nsrt/bug-13546.php: thefirstInConditionfunction now correctly narrows tonon-empty-list<string>(waslist<string>) andarray{}(waslist<string>) when checkingarray_key_firstresult against nulltests/PHPStan/Analyser/nsrt/bug-6120.php: added test cases for nullsafe property access, nullable property access, and nullsafe method call narrowing through null checksphpstan-baseline.neonforClassReflection::getCacheKey()— the improved narrowing now correctly resolves the return typeRoot cause
PHPStan's
processAssignVarinNodeScopeResolvercreatesConditionalExpressionHolderentries that link an assigned variable's type to narrowing of sub-expressions. Previously, only truthy/falsey conditional expressions were created. For$result = $clazz?->foowherefooisint, the truthy type wasint<min, -1>|int<1, max>(non-zero int). When checking$result !== null, the narrowed type of$resultisint, which didn't match the truthy condition (int<min, -1>|int<1, max>), so the conditional narrowing of$clazznever fired.The fix adds separate conditional expressions specifically for the non-null type (
TypeCombinator::removeNull($type)) and null type, so that!== nullchecks now trigger the appropriate narrowing.Test
Added regression test in
tests/PHPStan/Analyser/nsrt/bug-6120.phpcovering:$clazz?->foowherefooisint)$clazz?->nullablePropertywhere property is?string)$clazz?->getFoo()where method returnsint)!== nulland=== nullbranchesFixes phpstan/phpstan#6120