Skip to content

Commit 5aaeb3d

Browse files
committed
note
1 parent 1d93ec3 commit 5aaeb3d

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

rules/Php82/Rector/Class_/ReadOnlyClassRector.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
1010
use Rector\Core\Rector\AbstractRector;
1111
use Rector\Core\ValueObject\PhpVersionFeature;
12+
use Rector\Core\ValueObject\Visibility;
13+
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
1214
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
1315
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1416
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -20,7 +22,10 @@
2022
*/
2123
final class ReadOnlyClassRector extends AbstractRector implements MinPhpVersionInterface
2224
{
23-
public function __construct(private readonly ClassAnalyzer $classAnalyzer)
25+
public function __construct(
26+
private readonly ClassAnalyzer $classAnalyzer,
27+
private readonly VisibilityManipulator $visibilityManipulator
28+
)
2429
{
2530
}
2631

@@ -65,21 +70,34 @@ public function getNodeTypes(): array
6570
*/
6671
public function refactor(Node $node): ?Node
6772
{
68-
if ($this->classAnalyzer->isAnonymousClass($node)) {
73+
if ($this->shouldSkip($node)) {
6974
return null;
7075
}
7176

77+
$this->visibilityManipulator->changeNodeVisibility($node, Visibility::READONLY);
78+
79+
// update all properties, both in defined property or in property promotino to not readonly, as class already readonly
80+
81+
return $node;
82+
}
83+
84+
private function shouldSkip(Class_ $node): bool
85+
{
86+
if ($this->classAnalyzer->isAnonymousClass($node)) {
87+
return true;
88+
}
89+
7290
$properties = $node->getProperties();
7391
foreach ($properties as $property) {
7492
if (! $property->isReadonly()) {
75-
return null;
93+
return true;
7694
}
7795
}
7896

7997
// property promotion
8098
// has `#[AllowDynamicProperties]`
8199

82-
return $node;
100+
return false;
83101
}
84102

85103
public function provideMinPhpVersion(): int

rules/Privatization/NodeManipulator/VisibilityManipulator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function removeVisibility(ClassMethod | Property | ClassConst $node): voi
7878
}
7979
}
8080

81-
public function changeNodeVisibility(ClassMethod | Property | ClassConst $node, int $visibility): void
81+
public function changeNodeVisibility(Class_ | ClassMethod | Property | ClassConst $node, int $visibility): void
8282
{
8383
Assert::oneOf($visibility, [
8484
Visibility::PUBLIC,

0 commit comments

Comments
 (0)