Skip to content

Commit 72ea3ac

Browse files
committed
Cover readonly hooked properties in interfaces
1 parent e1e6d74 commit 72ea3ac

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
@@ -89,6 +89,7 @@ lint:
8989
--exclude tests/PHPStan/Rules/Properties/data/hooked-properties-in-class.php \
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 \
92+
--exclude tests/PHPStan/Rules/Properties/data/readonly-property-hooks-in-interface.php \
9293
--exclude tests/PHPStan/Rules/Classes/data/bug-12281.php \
9394
--exclude tests/PHPStan/Rules/Traits/data/bug-12281.php \
9495
--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
@@ -57,6 +57,15 @@ public function processNode(Node $node, Scope $scope): array
5757
];
5858
}
5959

60+
if ($node->isReadOnly()) {
61+
return [
62+
RuleErrorBuilder::message('Interfaces cannot include readonly hooked properties.')
63+
->nonIgnorable()
64+
->identifier('property.readOnlyInInterface')
65+
->build(),
66+
];
67+
}
68+
6069
if ($this->hasAnyHookBody($node)) {
6170
return [
6271
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
@@ -118,4 +118,26 @@ public function testPhp84AndPropertyHooksWithBodiesInInterface(): void
118118
]);
119119
}
120120

121+
public function testPhp84AndReadonlyPropertyHooksInInterface(): void
122+
{
123+
if (PHP_VERSION_ID < 80400) {
124+
$this->markTestSkipped('Test requires PHP 8.4 or later.');
125+
}
126+
127+
$this->analyse([__DIR__ . '/data/readonly-property-hooks-in-interface.php'], [
128+
[
129+
'Interfaces cannot include readonly hooked properties.',
130+
7,
131+
],
132+
[
133+
'Interfaces cannot include readonly hooked properties.',
134+
9,
135+
],
136+
[
137+
'Interfaces cannot include readonly hooked properties.',
138+
11,
139+
]
140+
]);
141+
}
142+
121143
}
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 ReadonlyPropertyHooksInInterface;
4+
5+
interface HelloWorld
6+
{
7+
public readonly string $firstName { get; set; }
8+
9+
public readonly string $middleName { get; }
10+
11+
public readonly string $lastName { set; }
12+
}

0 commit comments

Comments
 (0)