Skip to content

Commit e1e6d74

Browse files
committed
Cover readonly hooked properties in abstract classes
1 parent 013940a commit e1e6d74

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

src/Rules/Properties/PropertyInClassRule.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ public function processNode(Node $node, Scope $scope): array
7272
->build(),
7373
];
7474
}
75-
76-
return [];
7775
}
7876

7977
if ($node->isReadOnly()) {
@@ -87,13 +85,15 @@ public function processNode(Node $node, Scope $scope): array
8785
}
8886
}
8987

90-
if (!$this->doAllHooksHaveBody($node)) {
91-
return [
92-
RuleErrorBuilder::message('Non-abstract properties cannot include hooks without bodies.')
93-
->nonIgnorable()
94-
->identifier('property.hookWithoutBody')
95-
->build(),
96-
];
88+
if (!$node->isAbstract() && !$node->isReadOnly()) {
89+
if (!$this->doAllHooksHaveBody($node)) {
90+
return [
91+
RuleErrorBuilder::message('Non-abstract properties cannot include hooks without bodies.')
92+
->nonIgnorable()
93+
->identifier('property.hookWithoutBody')
94+
->build(),
95+
];
96+
}
9797
}
9898

9999
return [];

tests/PHPStan/Rules/Properties/PropertyInClassRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ public function testPhp84AndReadonlyHookedProperties(): void
170170
'Hooked properties cannot be readonly.',
171171
14,
172172
],
173+
[
174+
'Hooked properties cannot be readonly.',
175+
19,
176+
]
173177
]);
174178
}
175179

tests/PHPStan/Rules/Properties/data/hooked-properties-without-bodies-in-class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class AbstractPerson
1212
class PromotedHookedPropertyWithoutVisibility
1313
{
1414

15-
public function __construct(mixed $test { get; })
15+
public function __construct(public mixed $test { get; })
1616
{
1717

1818
}

tests/PHPStan/Rules/Properties/data/readonly-property-hooks.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ class HelloWorld
1313

1414
public readonly string $lastName { set => $this->lastName; }
1515
}
16+
17+
abstract class HelloWorld
18+
{
19+
public abstract readonly string $firstName { get { return 'jake'; } set; }
20+
}

0 commit comments

Comments
 (0)