Repository navigation
Give a property fetch the error native type when a receiver member lacks it - #7284
Open
SanderMuller wants to merge 3 commits into
Open
SanderMuller wants to merge 3 commits into
SanderMuller wants to merge 3 commits into
Conversation
…cks it The native type of a property fetch was the property's native type even when the receiver can be null or a class without the property. The native path looked the property up through getInstancePropertyReflection(), which keeps only the union members that have it. Before 34bc963 the path required hasInstanceProperty() to be yes on the whole receiver. Check the whole native receiver again. Leave out its null only in a nullsafe chain, which adds the null back. Do the same for a static fetch on a nullable class expression, and in both C++ mirrors. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Narrowing reads the native type of `$d->amount` through MutatingScope::resolveScopeStateType(), which kept only the receiver members that have the property. On `?Dekking $d`, the else branch of `if ($d->amount)` was then `0.0`, and `$d->amount === null` was reported as always false. A read on null gives null at runtime, so the native state is now the property's type plus null. A receiver that short-circuits a nullsafe chain leaves the null out, because the chain adds it. Static fetches are not changed: `null::$x` throws. When a narrowing makes such a receiver non-null, the tracked fetches on it lose that null again, unless their property can hold null. Only the narrowing does this: the ensure of isset(), empty() and ?? makes the receiver non-null for the operand only, so the fetches keep their state. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.3.0, a property fetch gets the property's native type even when the receiver can be
nullor a class without the property. For?Dekking $data,$data->amounthas the native typefloat, and 2.2.17 gave*ERROR*. WithtreatPhpDocTypesAsCertain: false,$data->amount === nullis then reported as always false.The native path of
PropertyFetchHandlerreads the property throughMutatingScope::getInstancePropertyReflection(). That method keeps only the union members that have the property, soDekking|nullresolves toDekking::$amount. Before 34bc963, the native path usedPropertyReflectionFinder, which requiredhasInstanceProperty()to be yes on the whole receiver.The first two commits check the whole native receiver again before the lookup. In a nullsafe chain they leave
nullout, because the chain addsnullback, so$d?->self->amountkeeps the native typefloat|null.StaticPropertyFetchHandlerhad the same problem with$data::$totalon?Dekking, and it gets the same check.The last commit fixes the narrowing path. Narrowing reads the type through
MutatingScope::resolveScopeStateType(), which used the same filtered lookup. So afterif ($data->amount) {} else {}, theelsebranch still reported$data->amount === nullas always false. A read onnullgivesnullat runtime, so this state is now0.0|null. Static fetches do not getnullthere, becausenull::$xthrows.When a later condition makes the receiver non-null, the tracked fetches on it lose that
nullagain. This covers$data !== null,assert(),instanceofand an early return. A property whose declared type can holdnull, such as?Dekkingormixed, keeps itsnull. Only narrowing does this. The ensure step ofisset(),empty(),??and?->makes the receiver non-null for its operand only, so it does not change later code.I tried two simpler versions of the last commit first. Both lost true positives:
*ERROR*in the narrowing state brokeisset()and!empty()after a narrowing.float|nullwithout the receiver step keptnullafter$data !== null.Native types compared with 2.2.17:
*ERROR*again, as on 2.2.17:*ERROR*ormixedthere.Two cases still keep the receiver's
nullafter a narrowing. One is a receiver narrowed through a variable ($ok = $data !== null; if ($ok)). The other is a value copied out first ($x = $data->amount). A union with a class that lacks the property also still gives the false positive in theelsebranch.All C++ mirrors have the same changes. The new tests fail without the change in
src/. Level 8 withtreatPhpDocTypesAsCertain: falsereports the same errors with and without the last commit onsymfony/console,nette/diandnikic/php-parser. Onsrc/Analyserandsrc/Type, three runs of each gave the same user CPU within noise.One synthetic file is slower. It has 80
?Dparameters and 6400if ($dN->fM) {}checks, and its user CPU went from 4.4 s to 5.15 s without Turbo. With the scan of the receiver's fetches disabled, the time stays the same. With the addednullremoved, the time goes back to 4.4 s. So the cost comes from the native statefloat|null, which replaces the wrongfloat.#6813 took the same approach for the first part. It was closed on 2026-11-25 without a comment, so I could not tell if the approach was the reason.
Closes phpstan/phpstan#15424
🤖 Generated with Claude Code