Skip to content

Commit 8d4b70a

Browse files
committed
Add handling an attempt to declare a hooked property static
1 parent da5754c commit 8d4b70a

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

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
return [];
96107
}
97108

tests/PHPStan/Rules/Properties/PropertyInClassRuleTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,22 @@ public function testPhp84AndReadonlyHookedProperties(): void
177177
]);
178178
}
179179

180+
public function testPhp84AndStaticHookedProperties(): void
181+
{
182+
if (PHP_VERSION_ID < 80400) {
183+
$this->markTestSkipped('Test requires PHP 8.4 or later.');
184+
}
185+
186+
$this->analyse([__DIR__ . '/data/static-hooked-properties.php'], [
187+
[
188+
'Hooked properties cannot be static.',
189+
7,
190+
],
191+
[
192+
'Hooked properties cannot be static.',
193+
15,
194+
],
195+
]);
196+
}
197+
180198
}
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)