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

RenameViews::refactor()   B

Complexity

Conditions 8
Paths 15

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 14
c 1
b 0
f 0
nc 15
nop 1
dl 0
loc 25
rs 8.4444
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 RenameViews 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. views', [
21
                new CodeSample(
22
                    'view("admin.blocks.text");',
23
                    'view("twill.blocks.text");'
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
        $isViewCall = false;
37
        if ($node instanceof Node\Expr\StaticCall) {
38
            if ($node->name->name === 'make') {
39
                $isViewCall = $node->class->getLast() === 'View';
0 ignored issues
show
Bug introduced by
The method getLast() does not exist on PhpParser\Node\Expr. ( Ignorable by Annotation )

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

39
                $isViewCall = $node->class->/** @scrutinizer ignore-call */ getLast() === 'View';

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
40
            }
41
        } elseif ($node instanceof Node\Expr\FuncCall) {
42
            if ($node->name->parts ?? false) {
43
                $isViewCall = $node->name->parts[0] === 'view';
44
            }
45
        }
46
47
        if ($isViewCall && $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

47
        if ($isViewCall && $node->/** @scrutinizer ignore-call */ getArgs()[0] ?? false) {
Loading history...
48
            /** @var \PhpParser\Node\Arg $arg */
49
            $arg = $node->getArgs()[0];
50
            if (Str::startsWith($arg->value->value, 'admin.')) {
51
                $node->args[0] = new Node\Arg(
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...
52
                    BuilderHelpers::normalizeValue(Str::replaceFirst('admin.', 'twill.', $arg->value->value))
53
                );
54
                return $node;
55
            }
56
        }
57
58
        return null;
59
    }
60
}
61