-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathRemoveReadonlyPropertyVisibilityOnReadonlyClassRector.php
More file actions
116 lines (99 loc) · 3.07 KB
/
RemoveReadonlyPropertyVisibilityOnReadonlyClassRector.php
File metadata and controls
116 lines (99 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
declare(strict_types=1);
namespace Rector\CodeQuality\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Php81\NodeManipulator\AttributeGroupNewLiner;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\MethodName;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\Class_\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector\RemoveReadonlyPropertyVisibilityOnReadonlyClassRectorTest
*/
final class RemoveReadonlyPropertyVisibilityOnReadonlyClassRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly VisibilityManipulator $visibilityManipulator,
private readonly AttributeGroupNewLiner $attributeGroupNewLiner
) {
}
public function provideMinPhpVersion(): int
{
return PhpVersionFeature::READONLY_CLASS;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Remove readonly property visibility on readonly class', [
new CodeSample(
<<<'CODE_SAMPLE'
final readonly class SomeClass
{
public function __construct(
private readonly string $name
) {
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final readonly class SomeClass
{
public function __construct(
private string $name
) {
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
if (! $node->isReadonly()) {
return null;
}
$hasChanged = false;
$constructClassMethod = $node->getMethod(MethodName::CONSTRUCT);
if ($constructClassMethod instanceof ClassMethod) {
foreach ($constructClassMethod->getParams() as $param) {
if (! $param->isReadonly()) {
continue;
}
$this->visibilityManipulator->removeReadonly($param);
$hasChanged = true;
if ($param->attrGroups !== []) {
$this->attributeGroupNewLiner->newLine($this->file, $param);
}
}
}
foreach ($node->getProperties() as $property) {
if (! $property->isReadonly()) {
continue;
}
$this->visibilityManipulator->removeReadonly($property);
$hasChanged = true;
if ($property->attrGroups !== []) {
$this->attributeGroupNewLiner->newLine($this->file, $property);
}
}
if (! $hasChanged) {
return null;
}
return $node;
}
}