Skip to content

Commit 80fec2e

Browse files
staabmondrejmirtes
authored andcommitted
Hooked properties cannot be both abstract and private
1 parent 4b241b3 commit 80fec2e

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ lint:
9898
--exclude tests/PHPStan/Rules/Classes/data/invalid-hooked-properties.php \
9999
--exclude tests/PHPStan/Parser/data/cleaning-property-hooks-before.php \
100100
--exclude tests/PHPStan/Parser/data/cleaning-property-hooks-after.php \
101+
--exclude tests/PHPStan/Rules/Properties/data/abstract-private-property-hook.php \
101102
--exclude tests/PHPStan/Rules/Properties/data/existing-classes-property-hooks.php \
102103
--exclude tests/PHPStan/Rules/Properties/data/set-property-hook-parameter.php \
103104
--exclude tests/PHPStan/Rules/Properties/data/overriding-final-property.php \

src/Rules/Properties/PropertyInClassRule.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ public function processNode(Node $node, Scope $scope): array
9494
}
9595

9696
if ($node->isPrivate()) {
97+
if ($node->isAbstract()) {
98+
return [
99+
RuleErrorBuilder::message('Property cannot be both abstract and private.')
100+
->nonIgnorable()
101+
->identifier('property.abstractPrivate')
102+
->build(),
103+
];
104+
}
105+
97106
if ($node->isFinal()) {
98107
return [
99108
RuleErrorBuilder::message('Property cannot be both final and private.')

tests/PHPStan/Rules/Properties/PropertyInClassRuleTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,20 @@ public function testPhp84AndAbstractFinalHookedProperties(): void
245245
]);
246246
}
247247

248+
public function testPhp84AndAbstractPrivateHookedProperties(): void
249+
{
250+
if (PHP_VERSION_ID < 80400) {
251+
$this->markTestSkipped('Test requires PHP 8.4 or later.');
252+
}
253+
254+
$this->analyse([__DIR__ . '/data/abstract-private-property-hook.php'], [
255+
[
256+
'Property cannot be both abstract and private.',
257+
7,
258+
],
259+
]);
260+
}
261+
248262
public function testPhp84AndAbstractFinalHookedPropertiesParseError(): void
249263
{
250264
if (PHP_VERSION_ID < 80400) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php // lint >= 8.4
2+
3+
namespace AbstractPrivateHook;
4+
5+
abstract class Foo
6+
{
7+
abstract private int $i { get; }
8+
abstract protected int $ii { get; }
9+
abstract public int $iii { get; }
10+
}

0 commit comments

Comments
 (0)