forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenameNamespaceRector.php
165 lines (135 loc) · 4.7 KB
/
RenameNamespaceRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
declare(strict_types=1);
namespace Rector\Renaming\Rector\Namespace_;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Use_;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\RenamedNamespace;
use Rector\Naming\NamespaceMatcher;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\Renaming\Rector\Namespace_\RenameNamespaceRector\RenameNamespaceRectorTest
*/
final class RenameNamespaceRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var string
*/
public const OLD_TO_NEW_NAMESPACES = '$oldToNewNamespaces';
/**
* @var string[]
*/
private $oldToNewNamespaces = [];
/**
* @var NamespaceMatcher
*/
private $namespaceMatcher;
public function __construct(NamespaceMatcher $namespaceMatcher)
{
$this->namespaceMatcher = $namespaceMatcher;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Replaces old namespace by new one.', [
new ConfiguredCodeSample(
'$someObject = new SomeOldNamespace\SomeClass;',
'$someObject = new SomeNewNamespace\SomeClass;',
[
self::OLD_TO_NEW_NAMESPACES => [
'SomeOldNamespace' => 'SomeNewNamespace',
],
]
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Namespace_::class, Use_::class, Name::class];
}
/**
* @param Namespace_|Use_|Name $node
*/
public function refactor(Node $node): ?Node
{
$name = $this->getName($node);
if ($name === null) {
return null;
}
$renamedNamespaceValueObject = $this->namespaceMatcher->matchRenamedNamespace($name, $this->oldToNewNamespaces);
if (! $renamedNamespaceValueObject instanceof RenamedNamespace) {
return null;
}
if ($this->isClassFullyQualifiedName($node)) {
return null;
}
if ($node instanceof Namespace_) {
$newName = $renamedNamespaceValueObject->getNameInNewNamespace();
$node->name = new Name($newName);
return $node;
}
if ($node instanceof Use_) {
$newName = $renamedNamespaceValueObject->getNameInNewNamespace();
$node->uses[0]->name = new Name($newName);
return $node;
}
$newName = $this->isPartialNamespace($node) ? $this->resolvePartialNewName(
$node,
$renamedNamespaceValueObject
) : $renamedNamespaceValueObject->getNameInNewNamespace();
return new FullyQualified($newName);
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
$this->oldToNewNamespaces = $configuration[self::OLD_TO_NEW_NAMESPACES] ?? [];
}
/**
* Checks for "new \ClassNoNamespace;"
* This should be skipped, not a namespace.
*/
private function isClassFullyQualifiedName(Node $node): bool
{
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentNode instanceof Node) {
return false;
}
if (! $parentNode instanceof New_) {
return false;
}
/** @var FullyQualified $fullyQualifiedNode */
$fullyQualifiedNode = $parentNode->class;
$newClassName = $fullyQualifiedNode->toString();
return array_key_exists($newClassName, $this->oldToNewNamespaces);
}
private function isPartialNamespace(Name $name): bool
{
$resolvedName = $name->getAttribute(AttributeKey::RESOLVED_NAME);
if (! $resolvedName instanceof Name) {
return false;
}
if ($resolvedName instanceof FullyQualified) {
return ! $this->isName($name, $resolvedName->toString());
}
return false;
}
private function resolvePartialNewName(Name $name, RenamedNamespace $renamedNamespace): string
{
$nameInNewNamespace = $renamedNamespace->getNameInNewNamespace();
// first dummy implementation - improve
$cutOffFromTheLeft = Strings::length($nameInNewNamespace) - Strings::length($name->toString());
return Strings::substring($nameInNewNamespace, $cutOffFromTheLeft);
}
}