-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathLivewireComponentComputedMethodToComputedAttributeRector.php
120 lines (99 loc) · 3.29 KB
/
LivewireComponentComputedMethodToComputedAttributeRector.php
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
117
118
119
120
<?php
namespace RectorLaravel\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Type\ObjectType;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use RectorLaravel\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see RectorLaravel\Tests\Rector\Class_\LivewireComponentComputedMethodToComputedAttributeRector\LivewireComponentComputedMethodToComputedAttributeRectorTest
*/
final class LivewireComponentComputedMethodToComputedAttributeRector extends AbstractRector
{
private const string COMPUTED_ATTRIBUTE = 'Livewire\Attributes\Computed';
private const string COMPONENT_CLASS = 'Livewire\Component';
private const string METHOD_PATTERN = '/^get(?\'methodName\'[\w]*)Property$/';
public function __construct(private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer) {}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Converts the computed methods of a Livewire component to use the Computed Attribute',
[
new CodeSample(<<<'CODE_SAMPLE'
use Livewire\Component;
class MyComponent extends Component
{
public function getFooBarProperty()
{
}
}
CODE_SAMPLE,
<<<'CODE_SAMPLE'
use Livewire\Component;
class MyComponent extends Component
{
#[\Livewire\Attributes\Computed]
public function fooBar()
{
}
}
CODE_SAMPLE
),
]
);
}
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Class_
{
if (! $this->isObjectType($node, new ObjectType(self::COMPONENT_CLASS))) {
return null;
}
$changes = false;
foreach ($node->stmts as $stmt) {
if (
$stmt instanceof ClassMethod &&
$stmt->isPublic() &&
(bool) preg_match(self::METHOD_PATTERN, $this->getName($stmt), $matches)) {
$methodName = lcfirst($matches['methodName']);
if ($this->methodExistsInClass($node, $methodName)) {
continue;
}
$this->addComputedAttributeToClassMethodAndRename($stmt, $methodName);
$changes = true;
}
}
if ($changes === false) {
return null;
}
return $node;
}
private function addComputedAttributeToClassMethodAndRename(ClassMethod $classMethod, string $name): void
{
if ($this->phpAttributeAnalyzer->hasPhpAttribute($classMethod, self::COMPUTED_ATTRIBUTE)) {
return;
}
$classMethod->attrGroups[] = new AttributeGroup([
new Attribute(
new FullyQualified(self::COMPUTED_ATTRIBUTE)
),
]);
$classMethod->name = new Identifier($name);
}
private function methodExistsInClass(Class_ $class, string $methodName): bool
{
return $this->getType($class)->hasMethod($methodName)->yes();
}
}