2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\CodingStyle\ClassNameImport;
|
2019-05-19 21:44:38 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
2023-03-23 23:21:34 +00:00
|
|
|
use PhpParser\Node\Identifier;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt;
|
|
|
|
use PhpParser\Node\Stmt\Namespace_;
|
2023-09-12 20:17:09 +00:00
|
|
|
use PhpParser\Node\Stmt\Use_;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\UseUse;
|
2019-05-19 21:44:38 +00:00
|
|
|
final class AliasUsesResolver
|
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\CodingStyle\ClassNameImport\UseImportsTraverser
|
2019-05-19 21:44:38 +00:00
|
|
|
*/
|
|
|
|
private $useImportsTraverser;
|
2023-06-17 18:02:56 +00:00
|
|
|
public function __construct(\Rector\CodingStyle\ClassNameImport\UseImportsTraverser $useImportsTraverser)
|
2019-05-19 21:44:38 +00:00
|
|
|
{
|
|
|
|
$this->useImportsTraverser = $useImportsTraverser;
|
|
|
|
}
|
|
|
|
/**
|
2023-06-17 16:01:00 +00:00
|
|
|
* @param Stmt[] $stmts
|
2019-05-19 21:44:38 +00:00
|
|
|
* @return string[]
|
|
|
|
*/
|
2023-06-17 16:01:00 +00:00
|
|
|
public function resolveFromNode(Node $node, array $stmts) : array
|
2019-05-19 21:44:38 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$node instanceof Namespace_) {
|
2023-06-17 16:01:00 +00:00
|
|
|
/** @var Namespace_[] $namespaces */
|
|
|
|
$namespaces = \array_filter($stmts, static function (Stmt $stmt) : bool {
|
|
|
|
return $stmt instanceof Namespace_;
|
|
|
|
});
|
2023-06-17 18:02:56 +00:00
|
|
|
if (\count($namespaces) !== 1) {
|
|
|
|
return [];
|
2023-06-17 16:01:00 +00:00
|
|
|
}
|
2023-06-17 18:02:56 +00:00
|
|
|
$node = \current($namespaces);
|
2019-08-22 07:43:12 +00:00
|
|
|
}
|
2023-06-17 18:02:56 +00:00
|
|
|
return $this->resolveFromStmts($node->stmts);
|
2019-05-19 21:44:38 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-10-27 12:31:55 +00:00
|
|
|
* @param Stmt[] $stmts
|
2019-05-19 21:44:38 +00:00
|
|
|
* @return string[]
|
|
|
|
*/
|
2021-10-27 12:31:55 +00:00
|
|
|
public function resolveFromStmts(array $stmts) : array
|
2019-05-19 21:44:38 +00:00
|
|
|
{
|
|
|
|
$aliasedUses = [];
|
2023-09-12 12:38:05 +00:00
|
|
|
/** @param Use_::TYPE_* $useType */
|
|
|
|
$this->useImportsTraverser->traverserStmts($stmts, static function (int $useType, UseUse $useUse, string $name) use(&$aliasedUses) : void {
|
|
|
|
if ($useType !== Use_::TYPE_NORMAL) {
|
|
|
|
return;
|
|
|
|
}
|
2023-03-23 23:21:34 +00:00
|
|
|
if (!$useUse->alias instanceof Identifier) {
|
2019-05-19 21:44:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$aliasedUses[] = $name;
|
|
|
|
});
|
|
|
|
return $aliasedUses;
|
|
|
|
}
|
|
|
|
}
|