Repository navigation
Do not inherit abstract parent property hooks into a redeclared property in PhpClassReflectionExtension - #7154
Open
phpstan-bot wants to merge 1 commit into
Open
phpstan-bot wants to merge 1 commit into
phpstan-bot wants to merge 1 commit into
Conversation
…rty in `PhpClassReflectionExtension`
- BetterReflection's ReflectionProperty::getHooks() merges in the parent property's
hooks, including abstract ones (`abstract public string $name { get; }`). A plain
child property that implements such a hook was therefore reported as hooked.
- PhpClassReflectionExtension::getImplementedHook() drops an inherited hook when the
property itself is not abstract, the hook comes from an abstract property, and the
hook's PropertyHook node (found by name and start/end line in the declaring file)
has no body. Concrete hooks are still inherited, including a concrete `set` next to
an abstract `get` on the same parent property.
- Since the fix is in reflection, every isHooked()/hasHook() consumer is corrected:
UnsetRule (unset.hookedProperty), and throw points of property reads
(PropertyHookThrowPointsResolver), which hid dead catches.
- Covered: abstract get, abstract set, abstract get + concrete set, concrete parent
hook (still hooked), abstract hook declared in a trait, grandchild classes.
Interface-declared hooks were already fine (BetterReflection only merges hooks
from the parent class).
- Ported to the native mirror turbo-ext/src/PhpClassReflectionExtension.cpp and
regenerated its declarations header.
Contributor
|
We might wait for Roave/BetterReflection#1599 |
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
A class that implements a parent's abstract property hook with a plain property
(
abstract public string $name { get; }→public string $name = 'demo';) wastreated as hooked. As a result,
unset($record->name)reportedCannot unset hooked Record::$name property., even though PHP runs it without error.Property reflection now drops abstract hooks inherited by a redeclared, non-abstract
property.
Changes
src/Reflection/Php/PhpClassReflectionExtension.php: newgetImplementedHook()/isAbstractHook(). An inherited hook is dropped when all of these hold:PropertyHooknode has no body.The node is found with
NodeFinder::findInstanceOf()in the hook's file, matched by hook name and start/end line. Concrete parent hooks are still inherited.turbo-ext/src/PhpClassReflectionExtension.cpp+turbo-ext/src/generated/PhpClassReflectionExtension.h: native mirror of the same logic.Analogous consumers fixed by the same change (they read
hasHook()/isHooked()from this reflection):UnsetRule: no longer reports the falseunset.hookedProperty.PropertyHookThrowPointsResolver: reading such a property no longer creates an implicit throw point from a nonexistent hook. Before, this hidDead catcherrors.Probed and already correct: properties implementing interface-declared hooks (BetterReflection only merges hooks from the parent class).
??/isset()nullability reports were unaffected.Root cause
BetterReflection's
ReflectionProperty::createCachedHooks()merges every hook of theparent class's same-named property into the child property. It does this without
checking whether the parent hook is abstract (upstream: Roave/BetterReflection#1599).
Neither the hook
ReflectionMethodnor its modifiers mark a property hook as abstract,so PHPStan checks for a hook body in the AST.
Test
tests/PHPStan/Rules/Variables/data/bug-15792.php(UnsetRuleTest::testBug15792) covers:set,getnext to a concreteset(still reported, because thesethook is inherited),tests/PHPStan/Rules/Exceptions/data/bug-15792.php(CatchWithUnthrownExceptionRuleTest::testBug15792): a dead catch around a read of such a property is now reported, while a read through an inherited concrete hook still counts as possibly throwing.Fixes phpstan/phpstan#15792
🤖 Generated with Claude Code