-
Notifications
You must be signed in to change notification settings - Fork 554
/
RenameViews.php
67 lines (58 loc) · 1.64 KB
/
RenameViews.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
<?php
namespace A17\Twill\Rector;
use Illuminate\Support\Str;
use PhpParser\BuilderHelpers;
use PhpParser\Node;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
class RenameViews extends LaravelAwareRectorRule
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change usages of admin. views',
[
new CodeSample(
'view("admin.blocks.text");',
'view("twill.blocks.text");'
),
]
);
}
public function getNodeTypes(): array
{
return [
Node\Expr\FuncCall::class,
Node\Expr\StaticCall::class,
];
}
/**
* @param Node\Expr\FuncCall|Node\Expr\StaticCall $node
*/
public function refactor(Node $node)
{
if (
$node instanceof Node\Expr\StaticCall
&& ($node->class->getLast() !== 'View' || $node->name->name !== 'make')
) {
return null;
}
if (
$node instanceof Node\Expr\FuncCall
&& $node->name->getLast() !== 'view'
) {
return null;
}
if (
!($arg = $node->getArgs()[0] ?? null)
|| !property_exists($arg->value, 'value')
|| !Str::startsWith($arg->value->value, 'admin.')
) {
return null;
}
$node->args[0] = new Node\Arg(BuilderHelpers::normalizeValue(
Str::replaceFirst('admin.', 'twill.', $arg->value->value),
));
return $node;
}
}