Skip to content

Commit f7c8ddc

Browse files
committed
EnforceReadonlyPublicPropertyRule: support asymmetric visibility
Do not report publicPropertyNotReadonly error when property uses private(set) or protected(set) modifiers, as these properties are effectively readonly from outside the class. Fixes #319
1 parent b7b6029 commit f7c8ddc

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/Rule/EnforceReadonlyPublicPropertyRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function processNode(
4141
return [];
4242
}
4343

44-
if (!$node->isPublic() || $node->isReadOnly() || $node->hasHooks()) {
44+
if (!$node->isPublic() || $node->isReadOnly() || $node->hasHooks() || $node->isPrivateSet() || $node->isProtectedSet()) {
4545
return [];
4646
}
4747

tests/Rule/data/EnforceReadonlyPublicPropertyRule/code-84.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,9 @@ class ImplementingClass implements MyInterface {
5757
class ImplementingClass2 implements MyInterface {
5858
public readonly string $key;
5959
}
60+
61+
class AsymmetricVisibility {
62+
public private(set) string $privateSet;
63+
public protected(set) string $protectedSet;
64+
public public(set) string $publicSet; // error: Public property `publicSet` not marked as readonly.
65+
}

0 commit comments

Comments
 (0)