Repository navigation
fix(override): #[\Override] on a property without a parent property is an InvalidOverride - #14390
Conversation
…meo#14006 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
✅ Performance: unchanged · Types: unchanged
How to read this table
head ee5578b · base 7bfe967 (merge-base) · workflow run |
|
Behaviour change
|
orklah
left a comment
There was a problem hiding this comment.
Thanks! The check is in the right place and I found no false positive (parent class, interface, interface of a
parent, static, abstract hooked property, property a parent got from a trait). Two cases that PHP 8.5 rejects
are still missed, though:
-
A property that comes from a trait. PHP checks the attribute in the class that uses the trait, and Psalm
already does so for trait methods:trait T { #[\Override] public int $x = 1; // PHP 8.5: Fatal error, no matching parent property; Psalm: no issue } final class A { use T; }
$property_class_name === $fq_class_nameexcludes it, since the declaring class is the trait. Comparing the
class of$appearing_property_idinstead should cover it (it is the using class for a trait property and the
parent for an inherited one). -
A promoted property:
final class C { public function __construct(#[\Override] public int $x) {} // PHP 8.5: Fatal error; Psalm: no issue }
The scanner keeps the parameter's attributes only on the parameter storage, not on the promoted property's.
Could you handle both, with test cases for each (invalid: trait without parent, trait whose parent property is
private, promoted without parent; valid: trait and promoted property with a public parent property)?
Smaller points:
- An
#[\Override]property of an interface that extends nothing is also a fatal error and isn't reported
(interfaces don't go throughaddContextProperties()); fine to leave out, but please say so in the
description, as it says the PR fixes the issues. $property_storage->suppressed_issues ?: $storage->suppressed_issues: a property suppressing some other
issue makes a class-level@psalm-suppress InvalidOverrideignored; merging both lists avoids that.- For an anonymous class, the message shows the internal class name; the method message only shows the member name.
…perty is checked too A property of a trait is checked in the class that uses it (the class of its appearing property id), the attributes of a promoted parameter are kept on the promoted property, the suppressions of the property and of the class are merged and the message names the property only, like the method message. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Pushed ee5578b.
|
PHP 8.5:
#[\Override]on a property without a matching non-private property in a parent class or interface is a fatal error. Psalm reports noInvalidOverride, as it does for methods.Before
After
#[\Override] public int $b = 2; // InvalidOverride: Property $b does not match any parent propertyA private parent property does not count, a property of an interface or a parent class does (new tests).
Fixes #14006
Fixes #14044
Not reported:
#[\Override]on a property of an interface that extends nothing (interfaces do not go throughaddContextProperties()).