Skip to content

Commit 70f0241

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

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/Classes/data/bug-12281.php \
9495
--exclude tests/PHPStan/Rules/Traits/data/bug-12281.php \
9596
--exclude tests/PHPStan/Rules/Classes/data/invalid-hooked-properties.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
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)