Skip to content

Commit 1d50e87

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

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

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)