Skip to content

Commit a4b6070

Browse files
committed
Add handling an attempt to declare a hooked property static
1 parent e140197 commit a4b6070

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ lint:
9090
--exclude tests/PHPStan/Rules/Properties/data/hooked-properties-without-bodies-in-class.php \
9191
--exclude tests/PHPStan/Rules/Properties/data/readonly-property-hooks.php \
9292
--exclude tests/PHPStan/Rules/Properties/data/readonly-property-hooks-in-interface.php \
93+
--exclude tests/PHPStan/Rules/Properties/data/static-hooked-properties.php \
9394
--exclude tests/PHPStan/Rules/Properties/data/virtual-hooked-properties.php \
9495
--exclude tests/PHPStan/Rules/Classes/data/bug-12281.php \
9596
--exclude tests/PHPStan/Rules/Traits/data/bug-12281.php \

src/Rules/Properties/PropertyInClassRule.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ public function processNode(Node $node, Scope $scope): array
9292
}
9393
}
9494

95+
if ($node->isStatic()) {
96+
if ($node->hasHooks()) {
97+
return [
98+
RuleErrorBuilder::message('Hooked properties cannot be static.')
99+
->nonIgnorable()
100+
->identifier('property.hookStatic')
101+
->build(),
102+
];
103+
}
104+
}
105+
95106
if ($node->isVirtual()) {
96107
if ($node->getDefault() !== null) {
97108
return [

tests/PHPStan/Rules/Properties/PropertyInClassRuleTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,22 @@ public function testPhp84AndVirtualHookedProperties(): void
195195
]);
196196
}
197197

198+
public function testPhp84AndStaticHookedProperties(): void
199+
{
200+
if (PHP_VERSION_ID < 80400) {
201+
$this->markTestSkipped('Test requires PHP 8.4 or later.');
202+
}
203+
204+
$this->analyse([__DIR__ . '/data/static-hooked-properties.php'], [
205+
[
206+
'Hooked properties cannot be static.',
207+
7,
208+
],
209+
[
210+
'Hooked properties cannot be static.',
211+
15,
212+
],
213+
]);
214+
}
215+
198216
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace StaticHookedProperties;
4+
5+
class HelloWorld
6+
{
7+
public static string $foo {
8+
get => $this->foo;
9+
set => $this->foo = $value;
10+
}
11+
}
12+
13+
abstract class HiWorld
14+
{
15+
public static string $foo {
16+
get => 'dummy';
17+
}
18+
}

0 commit comments

Comments
 (0)