Skip to content

Give a property fetch the error native type when a receiver member lacks it - #7284

Open
SanderMuller wants to merge 3 commits into
phpstan:2.4.xfrom
SanderMuller:property-fetch-nullable-receiver-native-type
Open

SanderMuller wants to merge 3 commits into
phpstan:2.4.xfrom
SanderMuller:property-fetch-nullable-receiver-native-type

Conversation

@SanderMuller

@SanderMuller SanderMuller commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Since 2.3.0, a property fetch gets the property's native type even when the receiver can be null or a class without the property. For ?Dekking $data, $data->amount has the native type float, and 2.2.17 gave *ERROR*. With treatPhpDocTypesAsCertain: false, $data->amount === null is then reported as always false.

The native path of PropertyFetchHandler reads the property through MutatingScope::getInstancePropertyReflection(). That method keeps only the union members that have the property, so Dekking|null resolves to Dekking::$amount. Before 34bc963, the native path used PropertyReflectionFinder, which required hasInstanceProperty() 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 null out, because the chain adds null back, so $d?->self->amount keeps the native type float|null. StaticPropertyFetchHandler had the same problem with $data::$total on ?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 after if ($data->amount) {} else {}, the else branch still reported $data->amount === null as always false. A read on null gives null at runtime, so this state is now 0.0|null. Static fetches do not get null there, because null::$x throws.

When a later condition makes the receiver non-null, the tracked fetches on it lose that null again. This covers $data !== null, assert(), instanceof and an early return. A property whose declared type can hold null, such as ?Dekking or mixed, keeps its null. Only narrowing does this. The ensure step of isset(), 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 broke isset() and !empty() after a narrowing.
  • float|null without the receiver step kept null after $data !== null.

Native types compared with 2.2.17:

  • These fetches give *ERROR* again, as on 2.2.17:
    • a fetch on a nullable receiver, also with a dynamic name
    • a fetch on a union with a class that lacks the property
    • a static fetch on a nullable expression
  • Nullsafe chains keep the 2.3.x types. 2.2.17 gave *ERROR* or mixed there.
  • Method calls and class constants on a nullable receiver behave as on 2.2.17, so this PR does not change them.

Two cases still keep the receiver's null after 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 the else branch.

All C++ mirrors have the same changes. The new tests fail without the change in src/. Level 8 with treatPhpDocTypesAsCertain: false reports the same errors with and without the last commit on symfony/console, nette/di and nikic/php-parser. On src/Analyser and src/Type, three runs of each gave the same user CPU within noise.

One synthetic file is slower. It has 80 ?D parameters and 6400 if ($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 added null removed, the time goes back to 4.4 s. So the cost comes from the native state float|null, which replaces the wrong float.

#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

SanderMuller and others added 3 commits December 2, 2026 02:03
…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>
@ondrejmirtes
ondrejmirtes changed the base branch from 2.3.x to 2.4.x December 3, 2026 18:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Property fetch on a nullable receiver has a non-nullable native type since 2.3.0

1 participant