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

UpdateProjectRootConstruction::refactor()   C

Complexity

Conditions 12
Paths 6

Size

Total Lines 47
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 21
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 47
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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