Skip to content

Check property hooks, asymmetric visibility and all prototypes in overriding rules - #7080

Merged
ondrejmirtes merged 3 commits into
2.3.xfrom
overriding-rules-php-src
Nov 15, 2026
Merged

ondrejmirtes merged 3 commits into
2.3.xfrom
overriding-rules-php-src

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

Situations found by running PHPStan over the php-src PHP-8.5 branch tests that expect an inheritance fatal error (260 tests, 21 previously got no error, now 10). Every new error was verified against real PHP 8.4, and the 1147 php-src tests in the same areas that expect no error got no new errors (only the iterable false positives below disappeared).

OverridingPropertyRule

  • Narrowing set visibility (public protected(set) → public private(set)), including the implicit protected(set) of readonly properties
  • Hooks are checked against the overridden hooks by reusing the checks of OverridingMethodRule (extracted into OverridingMethodCheck): final hook, get overriding &get, set parameter contravariance, abstract hook, #[\Override] on a hook that overrides nothing, and missing #[\Override] on hooks with checkMissingOverrideMethodAttribute
  • Backed property not accepting the wider type of an abstract set(int|string $value) hook it implements
  • &get together with set on a property backed by its parent property
  • The property is checked against every interface declaring it, not just the first one
  • iterable is the same type as array|Traversable (false positive)

Hooks that are wrong by themselves (reported by SetPropertyHookParameterRule and PHP itself) are not compared with the overridden hooks, so the same mistake isn't reported twice. Hook messages use the existing hook wording, e.g. Get hook for property B::$prop overrides final get hook for property A::$prop.

OverridingMethodRule

  • void overriding mixed
  • Not returning by reference when the overridden method does

OverridingConstantRule

  • Checked against every prototype (a final constant in the second interface or in the parent next to an interface was missed)
  • Native type checks no longer require checkPhpDocMethodSignatures, it's a fatal error in PHP

phpstan/phpstan#12336

Items this covers:

  • Check overriden hook from parent - final hook cannot be overriden, check set hook contravariance
  • A get hook in an interface may be satisfied by either a get or &get hook in a class / interface &get requires &get
  • Cross-check same properties inherited from multiple interfaces in a single class
  • the #[\Override] attribute can be applied to a hook

Not covered

  • Class X inherits both A::C and B::C, which is ambiguous for constants (needs a class-level rule)
  • Methods compatible with more than one prototype (two interfaces, two abstract trait methods), MethodPrototypeFinder returns only one
  • BetterReflection doesn't mark hooks of abstract properties abstract, so the abstract hook check stays in OverridingPropertyRule until that's fixed in the fork

…ethodRule

Extract the checks shared with property hooks into OverridingMethodCheck
and let MethodParameterComparisonHelper compare any method reflection,
so OverridingPropertyRule can reuse them for hooks.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1df04-9cc5-7608-a3ae-ede0c3cf05e5
@ondrejmirtes
ondrejmirtes force-pushed the overriding-rules-php-src branch from cc66ced to 3af7847 Compare November 15, 2026 14:59
…ngPropertyRule

- set visibility can't be narrowed
- hooks are checked against the overridden hooks: final, by-ref get,
  set parameter contravariance, abstract, #[\Override] (missing one
  reported with checkMissingOverrideMethodAttribute)
- backed property can't implement a wider abstract set hook
- get hook of a property backed by its parent can't return by reference
  when there's a set hook
- the property is checked against every interface declaring it
- iterable is the same type as array|Traversable

Hooks wrong by themselves are not compared with the overridden hooks.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1df04-9cc5-7608-a3ae-ede0c3cf05e5
…ive types

Native type of an overriding constant is a fatal error in PHP, so it's no
longer behind checkPhpDocMethodSignatures.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1df04-9cc5-7608-a3ae-ede0c3cf05e5
@ondrejmirtes
ondrejmirtes force-pushed the overriding-rules-php-src branch from 3af7847 to 27520ab Compare November 15, 2026 15:03
@ondrejmirtes
ondrejmirtes merged commit 231807a into 2.3.x Nov 15, 2026
104 of 109 checks passed
@ondrejmirtes
ondrejmirtes deleted the overriding-rules-php-src branch November 15, 2026 15:04
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.

1 participant