Skip to content

Commit b4f41c7

Browse files
Hooked property cannot be static
Co-authored-by: Ondřej Mirtes <ondrej@mirtes.cz>
1 parent 68d5702 commit b4f41c7

7 files changed

+92
-0
lines changed

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ 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 \
94+
--exclude tests/PHPStan/Rules/Properties/data/static-hooked-property-in-interface.php \
9395
--exclude tests/PHPStan/Rules/Properties/data/virtual-hooked-properties.php \
9496
--exclude tests/PHPStan/Rules/Classes/data/bug-12281.php \
9597
--exclude tests/PHPStan/Rules/Traits/data/bug-12281.php \

src/Rules/Properties/PropertiesInInterfaceRule.php

+9
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ public function processNode(Node $node, Scope $scope): array
6666
];
6767
}
6868

69+
if ($node->isStatic()) {
70+
return [
71+
RuleErrorBuilder::message('Hooked properties cannot be static.')
72+
->nonIgnorable()
73+
->identifier('property.hookedStatic')
74+
->build(),
75+
];
76+
}
77+
6978
if ($this->hasAnyHookBody($node)) {
7079
return [
7180
RuleErrorBuilder::message('Interfaces cannot include property hooks with bodies.')

src/Rules/Properties/PropertyInClassRule.php

+11
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.hookedStatic')
101+
->build(),
102+
];
103+
}
104+
}
105+
95106
if ($node->isVirtual()) {
96107
if ($node->getDefault() !== null) {
97108
return [

tests/PHPStan/Rules/Properties/PropertiesInInterfaceRuleTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,26 @@ public function testPhp84AndReadonlyPropertyHooksInInterface(): void
140140
]);
141141
}
142142

143+
public function testPhp84AndStaticHookedPropertyInInterface(): void
144+
{
145+
if (PHP_VERSION_ID < 80400) {
146+
$this->markTestSkipped('Test requires PHP 8.4 or later.');
147+
}
148+
149+
$this->analyse([__DIR__ . '/data/static-hooked-property-in-interface.php'], [
150+
[
151+
'Hooked properties cannot be static.',
152+
7,
153+
],
154+
[
155+
'Hooked properties cannot be static.',
156+
9,
157+
],
158+
[
159+
'Hooked properties cannot be static.',
160+
11,
161+
],
162+
]);
163+
}
164+
143165
}

tests/PHPStan/Rules/Properties/PropertyInClassRuleTest.php

+18
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
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace StaticHookedPropertyInInterface;
4+
5+
interface HelloWorld
6+
{
7+
public static string $firstName { get; set; }
8+
9+
public static string $middleName { get; }
10+
11+
public static string $lastName { set; }
12+
}

0 commit comments

Comments
 (0)