|
3 | 3 | declare (strict_types=1);
|
4 | 4 | namespace Rector\Renaming\Rector\FileWithoutNamespace;
|
5 | 5 |
|
6 |
| -use RectorPrefix202308\Nette\Utils\Strings; |
7 | 6 | use PhpParser\Node;
|
8 |
| -use PhpParser\Node\FunctionLike; |
9 |
| -use PhpParser\Node\Identifier; |
10 |
| -use PhpParser\Node\Name; |
11 |
| -use PhpParser\Node\Name\FullyQualified; |
12 |
| -use PhpParser\Node\Stmt; |
13 | 7 | use PhpParser\Node\Stmt\Namespace_;
|
14 |
| -use PhpParser\Node\Stmt\Property; |
15 | 8 | use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
16 |
| -use Rector\Core\Exception\ShouldNotHappenException; |
17 | 9 | use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
|
18 | 10 | use Rector\Core\Rector\AbstractRector;
|
19 |
| -use Rector\NodeTypeResolver\PhpDoc\PhpDocTypeRenamer; |
20 | 11 | use Rector\Renaming\ValueObject\PseudoNamespaceToNamespace;
|
21 | 12 | use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
22 | 13 | use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
23 |
| -use RectorPrefix202308\Webmozart\Assert\Assert; |
24 | 14 | /**
|
25 |
| - * @see \Rector\Tests\Renaming\Rector\FileWithoutNamespace\PseudoNamespaceToNamespaceRector\PseudoNamespaceToNamespaceRectorTest |
| 15 | + * @api deprecated and soon to be removed |
26 | 16 | */
|
27 | 17 | final class PseudoNamespaceToNamespaceRector extends AbstractRector implements ConfigurableRectorInterface
|
28 | 18 | {
|
29 |
| - /** |
30 |
| - * @readonly |
31 |
| - * @var \Rector\NodeTypeResolver\PhpDoc\PhpDocTypeRenamer |
32 |
| - */ |
33 |
| - private $phpDocTypeRenamer; |
34 |
| - /** |
35 |
| - * @see https://regex101.com/r/chvLgs/1/ |
36 |
| - * @var string |
37 |
| - */ |
38 |
| - private const SPLIT_BY_UNDERSCORE_REGEX = '#([a-zA-Z])(_)?(_)([a-zA-Z])#'; |
39 |
| - /** |
40 |
| - * @var PseudoNamespaceToNamespace[] |
41 |
| - */ |
42 |
| - private $pseudoNamespacesToNamespaces = []; |
43 |
| - /** |
44 |
| - * @var string|null |
45 |
| - */ |
46 |
| - private $newNamespace; |
47 |
| - public function __construct(PhpDocTypeRenamer $phpDocTypeRenamer) |
48 |
| - { |
49 |
| - $this->phpDocTypeRenamer = $phpDocTypeRenamer; |
50 |
| - } |
51 | 19 | public function getRuleDefinition() : RuleDefinition
|
52 | 20 | {
|
53 | 21 | return new RuleDefinition('Replaces defined Pseudo_Namespaces by Namespace\\Ones.', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
@@ -75,132 +43,16 @@ public function getNodeTypes() : array
|
75 | 43 | */
|
76 | 44 | public function refactor(Node $node) : ?Node
|
77 | 45 | {
|
78 |
| - $this->newNamespace = null; |
79 |
| - if ($node instanceof FileWithoutNamespace) { |
80 |
| - return $this->refactorFileWithoutNamespace($node); |
81 |
| - } |
82 |
| - return $this->refactorNamespace($node); |
| 46 | + $errorMessage = \sprintf('Rule "%s" is deprecated, as unreliable and too dynamic. Use more robuts RenameClassRector instead.', self::class); |
| 47 | + \trigger_error($errorMessage, \E_USER_WARNING); |
| 48 | + \sleep(3); |
| 49 | + return null; |
83 | 50 | }
|
84 | 51 | /**
|
85 | 52 | * @param mixed[] $configuration
|
86 | 53 | */
|
87 | 54 | public function configure(array $configuration) : void
|
88 | 55 | {
|
89 |
| - Assert::allIsAOf($configuration, PseudoNamespaceToNamespace::class); |
90 |
| - $this->pseudoNamespacesToNamespaces = $configuration; |
91 |
| - } |
92 |
| - /** |
93 |
| - * @param Stmt[] $stmts |
94 |
| - * @return Stmt[]|null |
95 |
| - */ |
96 |
| - private function refactorStmts(array $stmts) : ?array |
97 |
| - { |
98 |
| - $hasChanged = \false; |
99 |
| - $this->traverseNodesWithCallable($stmts, function (Node $node) use(&$hasChanged) : ?Node { |
100 |
| - if (!$node instanceof Name && !$node instanceof Identifier && !$node instanceof Property && !$node instanceof FunctionLike) { |
101 |
| - return null; |
102 |
| - } |
103 |
| - if ($this->refactorPhpDoc($node)) { |
104 |
| - $hasChanged = \true; |
105 |
| - } |
106 |
| - // @todo - update rule to allow for bool instanceof check |
107 |
| - if ($node instanceof Name || $node instanceof Identifier) { |
108 |
| - $changedNode = $this->processNameOrIdentifier($node); |
109 |
| - if ($changedNode instanceof Node) { |
110 |
| - $hasChanged = \true; |
111 |
| - return $changedNode; |
112 |
| - } |
113 |
| - } |
114 |
| - return null; |
115 |
| - }); |
116 |
| - if ($hasChanged) { |
117 |
| - return $stmts; |
118 |
| - } |
119 |
| - return null; |
120 |
| - } |
121 |
| - /** |
122 |
| - * @return Identifier|Name|null |
123 |
| - * @param \PhpParser\Node\Name|\PhpParser\Node\Identifier $node |
124 |
| - */ |
125 |
| - private function processNameOrIdentifier($node) : ?Node |
126 |
| - { |
127 |
| - // no name → skip |
128 |
| - if ($node->toString() === '') { |
129 |
| - return null; |
130 |
| - } |
131 |
| - foreach ($this->pseudoNamespacesToNamespaces as $pseudoNamespaceToNamespace) { |
132 |
| - if (!$this->isName($node, $pseudoNamespaceToNamespace->getNamespacePrefix() . '*')) { |
133 |
| - continue; |
134 |
| - } |
135 |
| - $excludedClasses = $pseudoNamespaceToNamespace->getExcludedClasses(); |
136 |
| - if ($excludedClasses !== [] && $this->isNames($node, $excludedClasses)) { |
137 |
| - return null; |
138 |
| - } |
139 |
| - if ($node instanceof Name) { |
140 |
| - return $this->processName($node); |
141 |
| - } |
142 |
| - return $this->processIdentifier($node); |
143 |
| - } |
144 |
| - return null; |
145 |
| - } |
146 |
| - private function processName(Name $name) : Name |
147 |
| - { |
148 |
| - $nodeName = $this->getName($name); |
149 |
| - return $name instanceof FullyQualified ? new FullyQualified(\explode('_', $nodeName), $name->getAttributes()) : new Name(\explode('_', $nodeName), $name->getAttributes()); |
150 |
| - } |
151 |
| - private function processIdentifier(Identifier $identifier) : ?Identifier |
152 |
| - { |
153 |
| - $name = $this->getName($identifier); |
154 |
| - if ($name === null) { |
155 |
| - return null; |
156 |
| - } |
157 |
| - /** @var string $namespaceName */ |
158 |
| - $namespaceName = Strings::before($name, '_', -1); |
159 |
| - /** @var string $lastNewNamePart */ |
160 |
| - $lastNewNamePart = Strings::after($name, '_', -1); |
161 |
| - $newNamespace = Strings::replace($namespaceName, self::SPLIT_BY_UNDERSCORE_REGEX, '$1$2\\\\$4'); |
162 |
| - if ($this->newNamespace !== null && $this->newNamespace !== $newNamespace) { |
163 |
| - throw new ShouldNotHappenException('There cannot be 2 different namespaces in one file'); |
164 |
| - } |
165 |
| - $this->newNamespace = $newNamespace; |
166 |
| - $identifier->name = $lastNewNamePart; |
167 |
| - return $identifier; |
168 |
| - } |
169 |
| - private function refactorNamespace(Namespace_ $namespace) : ?Namespace_ |
170 |
| - { |
171 |
| - $changedStmts = $this->refactorStmts($namespace->stmts); |
172 |
| - if ($changedStmts === null) { |
173 |
| - return null; |
174 |
| - } |
175 |
| - return $namespace; |
176 |
| - } |
177 |
| - /** |
178 |
| - * @param \PhpParser\Node\Name|\PhpParser\Node\FunctionLike|\PhpParser\Node\Identifier|\PhpParser\Node\Stmt\Property $node |
179 |
| - */ |
180 |
| - private function refactorPhpDoc($node) : bool |
181 |
| - { |
182 |
| - $hasChanged = \false; |
183 |
| - $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); |
184 |
| - // replace on @var/@param/@return/@throws |
185 |
| - foreach ($this->pseudoNamespacesToNamespaces as $pseudoNamespaceToNamespace) { |
186 |
| - $hasDocTypeChanged = $this->phpDocTypeRenamer->changeUnderscoreType($phpDocInfo, $node, $pseudoNamespaceToNamespace); |
187 |
| - if ($hasDocTypeChanged) { |
188 |
| - $hasChanged = \true; |
189 |
| - } |
190 |
| - } |
191 |
| - return $hasChanged; |
192 |
| - } |
193 |
| - private function refactorFileWithoutNamespace(FileWithoutNamespace $fileWithoutNamespace) : ?Namespace_ |
194 |
| - { |
195 |
| - $changedStmts = $this->refactorStmts($fileWithoutNamespace->stmts); |
196 |
| - if ($changedStmts === null) { |
197 |
| - return null; |
198 |
| - } |
199 |
| - $fileWithoutNamespace->stmts = $changedStmts; |
200 |
| - // add a new namespace? |
201 |
| - if ($this->newNamespace !== null) { |
202 |
| - return new Namespace_(new Name($this->newNamespace), $changedStmts); |
203 |
| - } |
204 |
| - return null; |
| 56 | + // for BC |
205 | 57 | }
|
206 | 58 | }
|
0 commit comments