2019-10-13 07:59:52 +02:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2020-11-20 13:37:53 +00:00
|
|
|
namespace Rector\CodingStyle\ClassNameImport;
|
2019-05-19 23:44:38 +02:00
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Stmt;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\Namespace_;
|
|
|
|
use PhpParser\Node\Stmt\UseUse;
|
2020-02-06 22:48:18 +01:00
|
|
|
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
2020-02-09 23:47:00 +01:00
|
|
|
use Rector\NodeNameResolver\NodeNameResolver;
|
2020-12-24 17:31:24 +01:00
|
|
|
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
|
2019-05-19 23:44:38 +02:00
|
|
|
final class UsedImportsResolver
|
|
|
|
{
|
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
|
2019-05-19 23:44:38 +02:00
|
|
|
*/
|
|
|
|
private $betterNodeFinder;
|
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\NodeNameResolver\NodeNameResolver
|
2019-05-19 23:44:38 +02:00
|
|
|
*/
|
2020-02-09 12:31:31 +01:00
|
|
|
private $nodeNameResolver;
|
2019-05-19 23:44:38 +02:00
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\CodingStyle\ClassNameImport\UseImportsTraverser
|
2019-05-19 23:44:38 +02:00
|
|
|
*/
|
|
|
|
private $useImportsTraverser;
|
2021-05-10 22:23:08 +00:00
|
|
|
public function __construct(\Rector\Core\PhpParser\Node\BetterNodeFinder $betterNodeFinder, \Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \Rector\CodingStyle\ClassNameImport\UseImportsTraverser $useImportsTraverser)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2019-05-19 23:44:38 +02:00
|
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
2020-02-09 12:31:31 +01:00
|
|
|
$this->nodeNameResolver = $nodeNameResolver;
|
2019-05-19 23:44:38 +02:00
|
|
|
$this->useImportsTraverser = $useImportsTraverser;
|
|
|
|
}
|
|
|
|
/**
|
2019-09-06 12:30:58 +02:00
|
|
|
* @return FullyQualifiedObjectType[]
|
2019-05-19 23:44:38 +02:00
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
public function resolveForNode(\PhpParser\Node $node) : array
|
2019-05-19 23:44:38 +02:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($node instanceof \PhpParser\Node\Stmt\Namespace_) {
|
2021-03-17 23:03:49 +01:00
|
|
|
$namespace = $node;
|
|
|
|
} else {
|
2021-05-10 22:23:08 +00:00
|
|
|
$namespace = $this->betterNodeFinder->findParentType($node, \PhpParser\Node\Stmt\Namespace_::class);
|
2021-03-17 23:03:49 +01:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($namespace instanceof \PhpParser\Node\Stmt\Namespace_) {
|
2019-05-19 23:44:38 +02:00
|
|
|
return $this->resolveForNamespace($namespace);
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Stmt[] $stmts
|
2019-09-06 12:30:58 +02:00
|
|
|
* @return FullyQualifiedObjectType[]
|
2019-05-19 23:44:38 +02:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function resolveForStmts(array $stmts) : array
|
2019-05-19 23:44:38 +02:00
|
|
|
{
|
|
|
|
$usedImports = [];
|
|
|
|
/** @var Class_|null $class */
|
2021-05-10 22:23:08 +00:00
|
|
|
$class = $this->betterNodeFinder->findFirstInstanceOf($stmts, \PhpParser\Node\Stmt\Class_::class);
|
2019-05-19 23:44:38 +02:00
|
|
|
// add class itself
|
|
|
|
if ($class !== null) {
|
2020-02-09 12:31:31 +01:00
|
|
|
$className = $this->nodeNameResolver->getName($class);
|
2019-05-19 23:44:38 +02:00
|
|
|
if ($className !== null) {
|
2021-05-10 22:23:08 +00:00
|
|
|
$usedImports[] = new \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType($className);
|
2019-05-19 23:44:38 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
$this->useImportsTraverser->traverserStmts($stmts, function (\PhpParser\Node\Stmt\UseUse $useUse, string $name) use(&$usedImports) : void {
|
|
|
|
$usedImports[] = new \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType($name);
|
2019-05-19 23:44:38 +02:00
|
|
|
});
|
|
|
|
return $usedImports;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Stmt[] $stmts
|
2019-09-06 12:30:58 +02:00
|
|
|
* @return FullyQualifiedObjectType[]
|
2019-05-19 23:44:38 +02:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function resolveFunctionImportsForStmts(array $stmts) : array
|
2019-05-19 23:44:38 +02:00
|
|
|
{
|
|
|
|
$usedFunctionImports = [];
|
2021-05-10 22:23:08 +00:00
|
|
|
$this->useImportsTraverser->traverserStmtsForFunctions($stmts, function (\PhpParser\Node\Stmt\UseUse $useUse, string $name) use(&$usedFunctionImports) : void {
|
|
|
|
$usedFunctionImports[] = new \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType($name);
|
2019-12-26 10:53:02 +01:00
|
|
|
});
|
2019-05-19 23:44:38 +02:00
|
|
|
return $usedFunctionImports;
|
|
|
|
}
|
|
|
|
/**
|
2019-09-06 12:30:58 +02:00
|
|
|
* @return FullyQualifiedObjectType[]
|
2019-05-19 23:44:38 +02:00
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
private function resolveForNamespace(\PhpParser\Node\Stmt\Namespace_ $namespace) : array
|
2019-05-19 23:44:38 +02:00
|
|
|
{
|
2020-06-29 23:19:37 +02:00
|
|
|
return $this->resolveForStmts($namespace->stmts);
|
2019-05-19 23:44:38 +02:00
|
|
|
}
|
|
|
|
}
|