Skip to content

Commit f032aa8

Browse files
committed
refactor(RemoveNamespaceRector): Simplify node collection logic
- Replace custom node visitor with FirstFindingVisitor for clarity. - Remove unused properties and methods to streamline the class. - Update return type for collectStmts to reflect new logic.
1 parent 4e2c54f commit f032aa8

10 files changed

Lines changed: 145 additions & 130 deletions

.php-cs-fixer.dist.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,20 +275,31 @@ static function (array $carry, FixerInterface $fixer): array {
275275
],
276276
'phpdoc_order' => [
277277
'order' => [
278-
'noinspection',
279-
'phan-suppress',
280-
'phpcsSuppress',
281-
'phpstan-ignore',
282-
'psalm-suppress',
278+
'see',
279+
280+
'extends',
281+
'implements',
282+
'mixin',
283283

284284
'deprecated',
285+
'final',
285286
'internal',
287+
'readonly',
288+
286289
'covers',
287290
'uses',
288291
'dataProvider',
292+
289293
'param',
290294
'throws',
291295
'return',
296+
297+
'codeCoverageIgnore',
298+
'noinspection',
299+
'phan-suppress',
300+
'phpcsSuppress',
301+
'phpstan-ignore',
302+
'psalm-suppress',
292303
],
293304
],
294305
'phpdoc_order_by_value' => [

baselines/loader.neon

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
# total 46 errors
1+
# total 44 errors
22

33
includes:
44
- callable.nonCallable.neon
55
- complexity.classLike.neon
66
- complexity.functionLike.neon
7-
- method.childReturnType.neon
87
- property.nonObject.neon
98
- property.notFound.neon
109
- typePerfect.narrowReturnObjectType.neon

baselines/method.childReturnType.neon

Lines changed: 0 additions & 13 deletions
This file was deleted.

composer-bump

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ final class ComposerBump
158158
}
159159

160160
/**
161-
* @noinspection LongLine
162-
*
163161
* @throws \JsonException
162+
*
163+
* @noinspection LongLine
164164
*/
165165
private function outdatedDecodedComposerJson(): array
166166
{
@@ -277,11 +277,11 @@ final class ComposerBump
277277
}
278278

279279
/**
280-
* @noinspection PhpSameParameterValueInspection
281-
* @noinspection LongLine
282-
*
283280
* @param list<null|scalar>|string $command
284281
* @param mixed $input
282+
*
283+
* @noinspection PhpSameParameterValueInspection
284+
* @noinspection LongLine
285285
*/
286286
private function mustRunCommand(
287287
string $command,

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@
396396
"sk:namespace-to-psr-4-tests": "@sk namespace-to-psr-4 tests/ --namespace-root=Guanguans\\RectorRulesTests\\",
397397
"sk:pretty-json": "@sk pretty-json .lintmdrc",
398398
"sk:pretty-json-dry-run": "@sk:pretty-json --dry-run",
399-
"sk:privatize-constants": "@sk privatize-constants src/ --exclude-path=src/Rector/Namespace_/RemoveNamespaceRector.php",
399+
"sk:privatize-constants": "@sk privatize-constants src/",
400400
"sk:search-regex": "@sk search-regex 'Guanguans.*ValetDrivers'",
401401
"sk:split-config-per-package": "@sk split-config-per-package monorepo-builder.php",
402402
"sk:spot-lazy-traits": "@sk spot-lazy-traits src/ --max-used=2",

src/Rector/Name/RenameToPsrNameRector.php

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
5252
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
5353
use Webmozart\Assert\Assert;
54-
use function Guanguans\RectorRules\Support\is_classes_of;
54+
use function Guanguans\RectorRules\Support\is_instance_of_any;
5555

5656
final class RenameToPsrNameRector extends AbstractRector implements ConfigurableRectorInterface
5757
{
@@ -131,9 +131,9 @@ public function getNodeTypes(): array
131131
}
132132

133133
/**
134-
* @noinspection PhpDocSignatureInspection
135-
*
136134
* @param \PhpParser\Node\Expr\FuncCall|\PhpParser\Node\Expr\Variable|\PhpParser\Node\Identifier|\PhpParser\Node\Name $node
135+
*
136+
* @noinspection PhpDocSignatureInspection
137137
*/
138138
public function refactor(Node $node): ?Node
139139
{
@@ -163,6 +163,8 @@ public function refactor(Node $node): ?Node
163163
}
164164

165165
/**
166+
* @see https://github.com/barryvdh/laravel-ide-helper/blob/master/resources/views/meta.php
167+
*
166168
* @throws \Symplify\RuleDocGenerator\Exception\PoorDocumentationException
167169
* @throws \Symplify\RuleDocGenerator\Exception\ShouldNotHappenException
168170
*/
@@ -311,10 +313,10 @@ public function configure(array $configuration): void
311313
* @see https://github.com/rectorphp/rector/issues/7611
312314
* @see https://github.com/rectorphp/rector/blob/main/UPGRADING.md#1-abstractscopeawarerector-is-removed-use-abstractrector-instead
313315
*
316+
* @param \PhpParser\Node\Expr\FuncCall|\PhpParser\Node\Expr\Variable|\PhpParser\Node\Identifier|\PhpParser\Node\Name $node
317+
*
314318
* @noinspection PhpDocSignatureInspection
315319
* @noinspection PhpPossiblePolymorphicInvocationInspection
316-
*
317-
* @param \PhpParser\Node\Expr\FuncCall|\PhpParser\Node\Expr\Variable|\PhpParser\Node\Identifier|\PhpParser\Node\Name $node
318320
*/
319321
private function rename(Node $node, callable $renamer): ?Node
320322
{
@@ -338,7 +340,7 @@ private function rename(Node $node, callable $renamer): ?Node
338340
}
339341

340342
if (
341-
is_classes_of($node, [
343+
is_instance_of_any($node, [
342344
Variable::class,
343345
Identifier::class,
344346
])
@@ -416,9 +418,9 @@ private function rename(Node $node, callable $renamer): ?Node
416418
}
417419

418420
/**
419-
* @noinspection PhpDocSignatureInspection
420-
*
421421
* @param \PhpParser\Node\Expr\FuncCall|\PhpParser\Node\Expr\Variable|\PhpParser\Node\Identifier|\PhpParser\Node\Name $node
422+
*
423+
* @noinspection PhpDocSignatureInspection
422424
*/
423425
private function shouldLowerSnakeName(Node $node): bool
424426
{
@@ -438,7 +440,7 @@ private function shouldLowerSnakeName(Node $node): bool
438440
&& (
439441
$parent instanceof FuncCall
440442
|| (
441-
is_classes_of($parent, [UseItem::class])
443+
is_instance_of_any($parent, [UseItem::class])
442444
&& (
443445
(Use_::TYPE_UNKNOWN === $grandfather->type && Use_::TYPE_FUNCTION === $parent->type)
444446
|| (Use_::TYPE_FUNCTION === $grandfather->type && Use_::TYPE_UNKNOWN === $parent->type)
@@ -462,9 +464,9 @@ private function shouldLowerSnakeName(Node $node): bool
462464
}
463465

464466
/**
465-
* @noinspection PhpDocSignatureInspection
466-
*
467467
* @param \PhpParser\Node\Expr\FuncCall|\PhpParser\Node\Expr\Variable|\PhpParser\Node\Identifier|\PhpParser\Node\Name $node
468+
*
469+
* @noinspection PhpDocSignatureInspection
468470
*/
469471
private function shouldUcfirstCamelName(Node $node): bool
470472
{
@@ -475,7 +477,7 @@ private function shouldUcfirstCamelName(Node $node): bool
475477

476478
if (
477479
$node instanceof Identifier
478-
&& is_classes_of($parent, [
480+
&& is_instance_of_any($parent, [
479481
// interface InterfaceName{}
480482
Interface_::class,
481483
// class ClassName{}
@@ -496,13 +498,13 @@ private function shouldUcfirstCamelName(Node $node): bool
496498
// use ClassName;
497499
/** @noinspection PhpConditionAlreadyCheckedInspection */
498500
(
499-
is_classes_of($parent, [UseItem::class])
501+
is_instance_of_any($parent, [UseItem::class])
500502
&& (
501503
(Use_::TYPE_UNKNOWN === $grandfather->type && Use_::TYPE_NORMAL === $parent->type)
502504
|| (Use_::TYPE_NORMAL === $grandfather->type && Use_::TYPE_UNKNOWN === $parent->type)
503505
)
504506
)
505-
|| is_classes_of($parent, [
507+
|| is_instance_of_any($parent, [
506508
// #[\AttributeName]
507509
Attribute::class,
508510
// class Foo extends ClassName implements InterfaceName{}
@@ -573,9 +575,9 @@ private function shouldUcfirstCamelName(Node $node): bool
573575
}
574576

575577
/**
576-
* @noinspection PhpDocSignatureInspection
577-
*
578578
* @param \PhpParser\Node\Expr\FuncCall|\PhpParser\Node\Expr\Variable|\PhpParser\Node\Identifier|\PhpParser\Node\Name $node
579+
*
580+
* @noinspection PhpDocSignatureInspection
579581
*/
580582
private function shouldUpperSnakeName(Node $node): bool
581583
{
@@ -585,7 +587,7 @@ private function shouldUpperSnakeName(Node $node): bool
585587
if (
586588
$node instanceof Identifier
587589
&& !$this->isName($node, 'class')
588-
&& is_classes_of($parent, [
590+
&& is_instance_of_any($parent, [
589591
// class Foo{public const CONST_NAME = 'const';}
590592
Const_::class,
591593
// Foo::CONST_NAME;
@@ -617,7 +619,7 @@ private function shouldUpperSnakeName(Node $node): bool
617619
&& (
618620
$parent instanceof ConstFetch
619621
|| (
620-
is_classes_of($parent, [UseItem::class])
622+
is_instance_of_any($parent, [UseItem::class])
621623
&& (
622624
(Use_::TYPE_UNKNOWN === $grandfather->type && Use_::TYPE_CONSTANT === $parent->type)
623625
|| (Use_::TYPE_CONSTANT === $grandfather->type && Use_::TYPE_UNKNOWN === $parent->type)
@@ -627,9 +629,9 @@ private function shouldUpperSnakeName(Node $node): bool
627629
}
628630

629631
/**
630-
* @noinspection PhpDocSignatureInspection
631-
*
632632
* @param \PhpParser\Node\Expr\FuncCall|\PhpParser\Node\Expr\Variable|\PhpParser\Node\Identifier|\PhpParser\Node\Name $node
633+
*
634+
* @noinspection PhpDocSignatureInspection
633635
*/
634636
private function shouldLcfirstCamelName(Node $node): bool
635637
{
@@ -640,7 +642,7 @@ private function shouldLcfirstCamelName(Node $node): bool
640642

641643
if (
642644
$node instanceof Identifier
643-
&& is_classes_of($node->getAttribute('parent'), [
645+
&& is_instance_of_any($node->getAttribute('parent'), [
644646
// class Foo{public $propertyName;}
645647
Property::class,
646648
// class Foo{public int $propertyName;}

src/Rector/Namespace_/RemoveNamespaceRector.php

Lines changed: 16 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,13 @@
2121
use PhpParser\Node\Stmt\Namespace_;
2222
use PhpParser\Node\Stmt\Nop;
2323
use PhpParser\NodeTraverser;
24-
use PhpParser\NodeVisitorAbstract;
24+
use PhpParser\NodeVisitor\FirstFindingVisitor;
2525
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2626
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
27-
use function Guanguans\RectorRules\Support\is_classes_of;
27+
use function Guanguans\RectorRules\Support\is_instance_of_any;
2828

2929
final class RemoveNamespaceRector extends AbstractRector
3030
{
31-
/** ClassLike(Attribute、Class、Enum、Interface、Trait)、Constant、Function. */
32-
public const STMT_CLASSES = [
33-
ClassLike::class,
34-
Const_::class,
35-
Function_::class,
36-
];
37-
38-
/** @var list<\PhpParser\Node\Stmt\ClassLike|\PhpParser\Node\Stmt\Const_|\PhpParser\Node\Stmt\Function_> */
39-
private array $stmts = [];
40-
4131
public function getNodeTypes(): array
4232
{
4333
return [
@@ -52,7 +42,7 @@ public function getNodeTypes(): array
5242
*/
5343
public function refactor(Node $node): ?array
5444
{
55-
if ($this->collectStmts($node)) {
45+
if ($this->findFirstSkippedNode($node) instanceof Node) {
5646
return null;
5747
}
5848

@@ -68,7 +58,6 @@ public function refactor(Node $node): ?array
6858

6959
/**
7060
* @throws \Symplify\RuleDocGenerator\Exception\PoorDocumentationException
71-
* @throws \Symplify\RuleDocGenerator\Exception\ShouldNotHappenException
7261
*/
7362
public function getRuleDefinition(): RuleDefinition
7463
{
@@ -93,49 +82,24 @@ public function getRuleDefinition(): RuleDefinition
9382
);
9483
}
9584

96-
public function addStmt(Node $stmt): void
97-
{
98-
$this->stmts[] = $stmt;
99-
}
100-
101-
public function refreshStmts(): void
102-
{
103-
$this->stmts = [];
104-
}
105-
10685
/**
107-
* @noinspection SelfClassReferencingInspection
86+
* @see \PhpParser\NodeVisitor\
87+
* @see \PhpParser\NodeVisitor\FirstFindingVisitor
10888
*
109-
* @return list<\PhpParser\Node>
89+
* @return null|\PhpParser\Node\Stmt\ClassLike|\PhpParser\Node\Stmt\Const_|\PhpParser\Node\Stmt\Function_
11090
*/
111-
private function collectStmts(Namespace_ $namespace): array
91+
private function findFirstSkippedNode(Namespace_ $namespace): ?Node
11292
{
113-
$traverser = new NodeTraverser(
114-
new class($this) extends NodeVisitorAbstract {
115-
private RemoveNamespaceRector $rector;
116-
117-
public function __construct(RemoveNamespaceRector $rector)
118-
{
119-
$rector->refreshStmts();
120-
$this->rector = $rector;
121-
}
122-
123-
/**
124-
* @noinspection PhpMissingParentCallCommonInspection
125-
*/
126-
public function enterNode(Node $node): void
127-
{
128-
if (is_classes_of($node, RemoveNamespaceRector::STMT_CLASSES)) {
129-
$this->rector->addStmt($node);
130-
}
131-
}
132-
}
133-
);
134-
93+
$traverser = new NodeTraverser($firstFindingVisitor = new FirstFindingVisitor(
94+
static fn (Node $node): bool => is_instance_of_any($node, [
95+
/** ClassLike(Attribute、Class、Enum、Interface、Trait)、Constant、Function. */
96+
ClassLike::class,
97+
Const_::class,
98+
Function_::class,
99+
])
100+
));
135101
$traverser->traverse([$namespace]);
136102

137-
// array_filter($namespace->stmts, static fn (Node $node): bool => is_classes_of($node, self::STMT_CLASSES));
138-
139-
return $this->stmts;
103+
return $firstFindingVisitor->getFoundNode();
140104
}
141105
}

0 commit comments

Comments
 (0)