Skip to content

Commit

Permalink
ReadOnlyPropertyRule - check static
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 29, 2022
1 parent 10ee36c commit 3e383fc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Rules/Properties/ReadOnlyPropertyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public function processNode(Node $node, Scope $scope): array
$errors[] = RuleErrorBuilder::message('Readonly property cannot have a default value.')->nonIgnorable()->build();
}

if ($node->isStatic()) {
$errors[] = RuleErrorBuilder::message('Readonly property cannot be static.')->nonIgnorable()->build();
}

return $errors;
}

Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/Properties/ReadOnlyPropertyRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ public function dataRule(): array
'Readonly properties are supported only on PHP 8.1 and later.',
16,
],
[
'Readonly properties are supported only on PHP 8.1 and later.',
23,
],
[
'Readonly property cannot be static.',
23,
],
],
],
[
Expand All @@ -62,6 +70,10 @@ public function dataRule(): array
'Readonly property cannot have a default value.',
10,
],
[
'Readonly property cannot be static.',
23,
],
],
],
];
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Properties/data/read-only-property.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ public function __construct(public readonly string $message = '')
{
}
}

class StaticReadonlyProperty
{
private readonly static int $foo;
}

0 comments on commit 3e383fc

Please sign in to comment.