Passed
Pull Request — master (#89)
by Dave
03:38
created

UpdateProjectRootConstruction   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 69
rs 10
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRuleDefinition() 0 8 1
C refactor() 0 47 12
A getNodeTypes() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace Utils\Rector;
7
8
9
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\ProjectRoot;
10
use PhpParser\Node;
11
use PhpParser\Node\Expr\New_;
12
use PhpParser\Node\Expr\StaticCall;
13
use PhpParser\Node\Name;
14
use Rector\Core\Rector\AbstractRector;
15
use Rector\Transform\ValueObject\StaticCallToMethodCall;
16
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
17
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
18
use Webmozart\Assert\Assert;
19
20
class UpdateProjectRootConstruction extends AbstractRector
21
{
22
23
    public function getNodeTypes(): array
24
    {
25
        return [New_::class];
26
    }
27
28
    public function getRuleDefinition(): RuleDefinition
29
    {
30
        return new RuleDefinition(
31
            "Updates call to `new ProjectRoot`",
32
            [
33
                new CodeSample(
34
                    'new ProjectRoot',
35
                    'ProjectRoot::fromProjectRoot'
36
                ),
37
            ]
38
        );
39
    }
40
41
    /** @param New_ $node */
42
    public function refactor(Node $node): ?Node
43
    {
44
        Assert::isInstanceOf($node, New_::class);
45
46
        if (!$node->class ===  null) {
0 ignored issues
show
Bug introduced by
Accessing class on the interface PhpParser\Node suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
introduced by
The condition ! $node->class === null is always false.
Loading history...
47
            return null;
48
        }
49
50
51
        if (!$node->class instanceof Name) {
52
            return null;
53
        }
54
55
56
        if ($node->class->parts !== ['DaveLiddament', 'StaticAnalysisResultsBaseliner', 'Domain', 'Common', 'ProjectRoot'])
57
        {
58
            return null;
59
        }
60
61
        $args = $node->args;
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...
62
63
        if (
64
            ($args[0]->value instanceof Node\Scalar\String_) &&
65
            ($args[1]->value instanceof Node\Scalar\String_) &&
66
            ($args[0]->value->value === $args[1]->value->value)
67
            ) {
68
69
            return new StaticCall(new Name\FullyQualified(ProjectRoot::class), 'fromCurrentWorkingDirectory', [$args[0]]);
70
        }
71
72
73
74
        if (
75
            ($args[0]->value instanceof Node\Expr\ClassConstFetch) &&
76
            ($args[1]->value instanceof Node\Expr\ClassConstFetch) &&
77
            ($args[0]->value->name instanceof Node\Identifier) &&
78
            ($args[1]->value->name instanceof Node\Identifier) &&
79
            ($args[0]->value->name->name === $args[1]->value->name->name)
80
81
            ) {
82
83
            return new StaticCall(new Name\FullyQualified(ProjectRoot::class), 'fromCurrentWorkingDirectory', [$args[0]]);
84
        }
85
86
87
88
        return new StaticCall(new Name\FullyQualified(ProjectRoot::class), 'fromProjectRoot', $args);
89
90
    }
91
}
92