From 861a6c056d24e3bc7425558b9832a133e985e453 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 2 Mar 2025 11:40:01 +0100 Subject: [PATCH 1/2] Fix GetNonVirtualPropertyHookReadRule on abstract property --- .../GetNonVirtualPropertyHookReadRule.php | 4 ++++ .../GetNonVirtualPropertyHookReadRuleTest.php | 9 +++++++++ .../data/get-abstract-property-hook-read.php | 15 +++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 tests/PHPStan/Rules/Properties/data/get-abstract-property-hook-read.php diff --git a/src/Rules/Properties/GetNonVirtualPropertyHookReadRule.php b/src/Rules/Properties/GetNonVirtualPropertyHookReadRule.php index 9a2fc917e7..270c9b937e 100644 --- a/src/Rules/Properties/GetNonVirtualPropertyHookReadRule.php +++ b/src/Rules/Properties/GetNonVirtualPropertyHookReadRule.php @@ -93,6 +93,10 @@ public function processNode(Node $node, Scope $scope): array continue; } + if ($propertyReflection->isAbstract()->yes()) { + continue; + } + $errors[] = RuleErrorBuilder::message(sprintf( 'Get hook for non-virtual property %s::$%s does not read its value.', $classReflection->getDisplayName(), diff --git a/tests/PHPStan/Rules/Properties/GetNonVirtualPropertyHookReadRuleTest.php b/tests/PHPStan/Rules/Properties/GetNonVirtualPropertyHookReadRuleTest.php index 8eedc78214..64745b60d9 100644 --- a/tests/PHPStan/Rules/Properties/GetNonVirtualPropertyHookReadRuleTest.php +++ b/tests/PHPStan/Rules/Properties/GetNonVirtualPropertyHookReadRuleTest.php @@ -35,4 +35,13 @@ public function testRule(): void ]); } + public function testAbstractProperty(): void + { + if (PHP_VERSION_ID < 80400) { + $this->markTestSkipped('Test requires PHP 8.4.'); + } + + $this->analyse([__DIR__ . '/data/get-abstract-property-hook-read.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Properties/data/get-abstract-property-hook-read.php b/tests/PHPStan/Rules/Properties/data/get-abstract-property-hook-read.php new file mode 100644 index 0000000000..4c26243016 --- /dev/null +++ b/tests/PHPStan/Rules/Properties/data/get-abstract-property-hook-read.php @@ -0,0 +1,15 @@ += 8.4 + +namespace GetAbstractPropertyHook; + +class NonFinalClass +{ + public string $publicProperty; +} + +abstract class Foo extends NonFinalClass +{ + abstract public string $publicProperty { + get; + } +} From 0239c902b088f137dc04380c9003133b981ebf65 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 2 Mar 2025 12:04:00 +0100 Subject: [PATCH 2/2] Update GetNonVirtualPropertyHookReadRule.php --- .../Properties/GetNonVirtualPropertyHookReadRule.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Rules/Properties/GetNonVirtualPropertyHookReadRule.php b/src/Rules/Properties/GetNonVirtualPropertyHookReadRule.php index 270c9b937e..a8a9554062 100644 --- a/src/Rules/Properties/GetNonVirtualPropertyHookReadRule.php +++ b/src/Rules/Properties/GetNonVirtualPropertyHookReadRule.php @@ -76,6 +76,10 @@ public function processNode(Node $node, Scope $scope): array continue; } + if ($hook->body === null) { + continue; + } + $hasGetHook = true; break; } @@ -93,10 +97,6 @@ public function processNode(Node $node, Scope $scope): array continue; } - if ($propertyReflection->isAbstract()->yes()) { - continue; - } - $errors[] = RuleErrorBuilder::message(sprintf( 'Get hook for non-virtual property %s::$%s does not read its value.', $classReflection->getDisplayName(),