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) { |
|
|
|
|
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; |
|
|
|
|
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
|
|
|
|