Passed
Pull Request — 3.x (#1388)
by Harings
15:34
created

RenameRoutes::refactor()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 14
c 1
b 0
f 0
nc 16
nop 1
dl 0
loc 25
rs 8.8333
1
<?php
2
3
namespace A17\Twill\Rector;
4
5
use Illuminate\Support\Facades\Route;
6
use Illuminate\Support\Str;
7
use PhpParser\BuilderHelpers;
8
use PhpParser\Node;
9
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
10
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
11
12
class RenameRoutes extends LaravelAwareRectorRule
13
{
14
    public static $ROUTES = null;
15
    public $baseDir = null;
16
17
    public function getRuleDefinition(): RuleDefinition
18
    {
19
        return new RuleDefinition(
20
            'Change usages of admin. routes', [
21
                new CodeSample(
22
                    'route("admin.blogs");',
23
                    'route("twill.blogs");'
24
                ),
25
            ]
26
        );
27
    }
28
29
    public function getNodeTypes(): array
30
    {
31
        return [Node\Expr\FuncCall::class, Node\Expr\StaticCall::class];
32
    }
33
34
    public function refactor(Node $node)
35
    {
36
        $isRouteCall = false;
37
        if ($node instanceof Node\Expr\StaticCall) {
38
            $isRouteCall = $node->name->name === 'route';
39
        } elseif ($node instanceof Node\Expr\FuncCall) {
40
            if ($node->name->parts ?? false) {
41
                $isRouteCall = $node->name->parts[0] === 'route';
42
            }
43
        }
44
45
        if ($isRouteCall) {
46
            $routes = $this->loadRoutes();
47
48
            if ($node->getArgs()[0] ?? false) {
0 ignored issues
show
Bug introduced by
The method getArgs() does not exist on PhpParser\Node. It seems like you code against a sub-type of PhpParser\Node such as PhpParser\Node\Expr\CallLike. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
            if ($node->/** @scrutinizer ignore-call */ getArgs()[0] ?? false) {
Loading history...
49
                /** @var \PhpParser\Node\Arg $arg */
50
                $arg = $node->getArgs()[0];
51
                if (array_key_exists($arg->value->value, $routes)) {
52
                    $node->args[0] = new Node\Arg(BuilderHelpers::normalizeValue($routes[$arg->value->value]));
0 ignored issues
show
Bug introduced by
Accessing args on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
53
                    return $node;
54
                }
55
            }
56
        }
57
58
        return null;
59
    }
60
61
    private function loadRoutes(): array
62
    {
63
        if (self::$ROUTES === null) {
64
            // Get all twill routes so we can process them properly.
65
            $app = $this->getLaravel();
0 ignored issues
show
Unused Code introduced by
The assignment to $app is dead and can be removed.
Loading history...
66
            $routes = Route::getRoutes();
67
68
            $twillRouteList = [];
69
            foreach ($routes->getRoutes() as $route) {
70
                if (Str::startsWith($route->getName(), 'twill.')) {
71
                    $twillRouteList[Str::replaceFirst('twill.', 'admin.', $route->getName())] = $route->getName();
72
                }
73
            }
74
75
            self::$ROUTES = $twillRouteList;
76
        }
77
78
        return self::$ROUTES;
79
    }
80
}
81