From 0e1fde888c0b9767f18628f31371bb29a21b4411 Mon Sep 17 00:00:00 2001 From: "ondrej@mirtes.cz" Date: Sun, 15 Nov 2026 15:22:38 +0000 Subject: [PATCH] Report readonly property overriding non-abstract get-only virtual property PHP allows a readonly property to override a non-readonly one only when the overridden property is abstract, not whenever it has no set operation. Amp-Thread-ID: https://ampcode.com/threads/T-01a1dfbd-11f8-71c3-bc25-13fc30a330ef --- .../Properties/OverridingPropertyRule.php | 2 +- .../Properties/OverridingPropertyRuleTest.php | 8 ++++ .../data/overriding-property-hooks.php | 46 +++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/Rules/Properties/OverridingPropertyRule.php b/src/Rules/Properties/OverridingPropertyRule.php index 2009ca10241..e36cdbf741a 100644 --- a/src/Rules/Properties/OverridingPropertyRule.php +++ b/src/Rules/Properties/OverridingPropertyRule.php @@ -205,7 +205,7 @@ private function processPrototype( } } elseif ($node->isReadOnly()) { if ( - !$this->phpVersion->supportsPropertyHooks() + !$prototype->isAbstract()->yes() || $prototype->isWritable() ) { $errors[] = RuleErrorBuilder::message(sprintf( diff --git a/tests/PHPStan/Rules/Properties/OverridingPropertyRuleTest.php b/tests/PHPStan/Rules/Properties/OverridingPropertyRuleTest.php index 9bbe1e77446..75364cf12b9 100644 --- a/tests/PHPStan/Rules/Properties/OverridingPropertyRuleTest.php +++ b/tests/PHPStan/Rules/Properties/OverridingPropertyRuleTest.php @@ -475,6 +475,14 @@ public function testHooks(): void 'Get hook for property OverridingPropertyHooks\OverrideChild::$notInParent has #[\Override] attribute but does not override any get hook.', 227, ], + [ + 'Readonly property OverridingPropertyHooks\ReadonlyOverridesVirtualGet::$prop overrides readwrite property OverridingPropertyHooks\VirtualGetParent::$prop.', + 245, + ], + [ + 'Readonly property OverridingPropertyHooks\ReadonlyOverridesVirtualGetInAbstractClass::$prop overrides readwrite property OverridingPropertyHooks\AbstractClassVirtualGetParent::$prop.', + 261, + ], ]); } diff --git a/tests/PHPStan/Rules/Properties/data/overriding-property-hooks.php b/tests/PHPStan/Rules/Properties/data/overriding-property-hooks.php index 3e4da370b9a..5b8c26a2ff5 100644 --- a/tests/PHPStan/Rules/Properties/data/overriding-property-hooks.php +++ b/tests/PHPStan/Rules/Properties/data/overriding-property-hooks.php @@ -229,3 +229,49 @@ class OverrideChild extends OverrideParent } } + +class VirtualGetParent +{ + + public string $prop { + get => 'parent'; + } + +} + +class ReadonlyOverridesVirtualGet extends VirtualGetParent +{ + + public readonly string $prop; + +} + +abstract class AbstractClassVirtualGetParent +{ + + public string $prop { + get => 'parent'; + } + +} + +class ReadonlyOverridesVirtualGetInAbstractClass extends AbstractClassVirtualGetParent +{ + + public readonly string $prop; + +} + +abstract class AbstractGetParent +{ + + abstract public string $prop { get; } + +} + +class ReadonlyImplementsAbstractGet extends AbstractGetParent +{ + + public readonly string $prop; + +}