Skip to content

Commit a00961c

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

File tree

5 files changed

+45
-1
lines changed

5 files changed

+45
-1
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ lint:
9191
--exclude tests/PHPStan/Rules/Properties/data/readonly-property-hooks.php \
9292
--exclude tests/PHPStan/Rules/Properties/data/readonly-property-hooks-in-interface.php \
9393
--exclude tests/PHPStan/Rules/Properties/data/static-hooked-properties.php \
94+
--exclude tests/PHPStan/Rules/Properties/data/static-hooked-property-in-interface.php \
9495
--exclude tests/PHPStan/Rules/Classes/data/bug-12281.php \
9596
--exclude tests/PHPStan/Rules/Traits/data/bug-12281.php \
9697
--exclude tests/PHPStan/Rules/Classes/data/invalid-hooked-properties.php \

src/Rules/Properties/PropertiesInInterfaceRule.php

Lines changed: 9 additions & 0 deletions
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.hookedStaticInInterface')
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function processNode(Node $node, Scope $scope): array
9797
return [
9898
RuleErrorBuilder::message('Hooked properties cannot be static.')
9999
->nonIgnorable()
100-
->identifier('property.hookStatic')
100+
->identifier('property.hookedStatic')
101101
->build(),
102102
];
103103
}

tests/PHPStan/Rules/Properties/PropertiesInInterfaceRuleTest.php

Lines changed: 22 additions & 0 deletions
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
}
Lines changed: 12 additions & 0 deletions
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)