-
-
Notifications
You must be signed in to change notification settings - Fork 671
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
f0223b6
commit 90dae50
Showing
10 changed files
with
109 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Naming\Naming; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Namespace_; | ||
use PhpParser\Node\Stmt\Use_; | ||
use Rector\Core\PhpParser\Node\BetterNodeFinder; | ||
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace; | ||
|
||
final class UseImportsResolver | ||
{ | ||
public function __construct( | ||
private readonly BetterNodeFinder $betterNodeFinder | ||
) { | ||
} | ||
|
||
/** | ||
* @return Use_[] | ||
*/ | ||
public function resolveForNode(Node $node): array | ||
{ | ||
$namespace = $this->betterNodeFinder->findParentByTypes( | ||
$node, | ||
[Namespace_::class, FileWithoutNamespace::class] | ||
); | ||
if (! $namespace instanceof Node) { | ||
return []; | ||
} | ||
|
||
return $this->betterNodeFinder->findInstanceOf($namespace, Use_::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/PhpParser/NodeTraverser/FileWithoutNamespaceNodeTraverser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\PhpParser\NodeTraverser; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Namespace_; | ||
use PhpParser\NodeFinder; | ||
use PhpParser\NodeTraverser; | ||
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
|
||
final class FileWithoutNamespaceNodeTraverser extends NodeTraverser | ||
{ | ||
public function __construct( | ||
private readonly NodeFinder $nodeFinder, | ||
) { | ||
} | ||
|
||
/** | ||
* @template TNode as Node | ||
* @param TNode[] $nodes | ||
* @return TNode[]|FileWithoutNamespace[] | ||
*/ | ||
public function traverse(array $nodes): array | ||
{ | ||
$hasNamespace = (bool) $this->nodeFinder->findFirstInstanceOf($nodes, Namespace_::class); | ||
if (! $hasNamespace && $nodes !== []) { | ||
$fileWithoutNamespace = new FileWithoutNamespace($nodes); | ||
foreach ($nodes as $node) { | ||
$node->setAttribute(AttributeKey::PARENT_NODE, $fileWithoutNamespace); | ||
} | ||
|
||
return [$fileWithoutNamespace]; | ||
} | ||
|
||
return $nodes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters