Skip to content

Do not inherit abstract parent property hooks into a redeclared property in PhpClassReflectionExtension - #7154

Open
phpstan-bot wants to merge 1 commit into
phpstan:2.4.xfrom
phpstan-bot:create-pull-request/patch-b8t9e14
Open

phpstan-bot wants to merge 1 commit into
phpstan:2.4.xfrom
phpstan-bot:create-pull-request/patch-b8t9e14

Conversation

@phpstan-bot

Copy link
Copy Markdown
Collaborator

Summary

A class that implements a parent's abstract property hook with a plain property
(abstract public string $name { get; } → public string $name = 'demo';) was
treated as hooked. As a result, unset($record->name) reported
Cannot 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: new getImplementedHook() / isAbstractHook(). An inherited hook is dropped when all of these hold:

    • the property is not abstract,
    • the hook comes from an abstract property,
    • its PropertyHook node 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 false unset.hookedProperty.
    • PropertyHookThrowPointsResolver: reading such a property no longer creates an implicit throw point from a nonexistent hook. Before, this hid Dead catch errors.
  • 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 the
parent 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 ReflectionMethod nor 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:
    • the issue's reproducer,
    • an abstract set,
    • an abstract get next to a concrete set (still reported, because the set hook is inherited),
    • a concrete parent hook (still reported),
    • an abstract hook declared in a trait,
    • grandchild classes.
  • 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.
  • Both tests fail without the fix. The full suite passes with and without the turbo extension loaded.

Fixes phpstan/phpstan#15792

🤖 Generated with Claude Code

…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.
@VincentLanglet

Copy link
Copy Markdown
Contributor

We might wait for Roave/BetterReflection#1599

@ondrejmirtes
ondrejmirtes changed the base branch from 2.3.x to 2.4.x December 3, 2026 18:07
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.

PHPStan reports an error when unsetting a plain property that implements an abstract get hook

2 participants