Skip to content

Commit 6c205a9

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

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
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('Interfaces cannot include static hooked properties.')
72+
->nonIgnorable()
73+
->identifier('property.staticInInterface')
74+
->build(),
75+
];
76+
}
77+
6978
if ($this->hasAnyHookBody($node)) {
7079
return [
7180
RuleErrorBuilder::message('Interfaces cannot include property hooks with bodies.')

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+
'Interfaces cannot include static hooked properties.',
152+
7,
153+
],
154+
[
155+
'Interfaces cannot include static hooked properties.',
156+
9,
157+
],
158+
[
159+
'Interfaces cannot include static hooked properties.',
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)