mirror of https://github.com/rectorphp/rector.git
Updated Rector to commit aeba96a811638e3954028b70d8163c43a8866fa8
aeba96a811
Refactor use import resolving (#4998)
This commit is contained in:
parent
0d6604344e
commit
088accd46c
|
@ -43,9 +43,10 @@ final class UseImportsAdder
|
|||
*/
|
||||
public function addImportsToStmts(FileWithoutNamespace $fileWithoutNamespace, array $stmts, array $useImportTypes, array $constantUseImportTypes, array $functionUseImportTypes) : array
|
||||
{
|
||||
$existingUseImportTypes = $this->usedImportsResolver->resolveForStmts($stmts);
|
||||
$existingConstantUseImports = $this->usedImportsResolver->resolveConstantImportsForStmts($stmts);
|
||||
$existingFunctionUseImports = $this->usedImportsResolver->resolveFunctionImportsForStmts($stmts);
|
||||
$usedImports = $this->usedImportsResolver->resolveForStmts($stmts);
|
||||
$existingUseImportTypes = $usedImports->getUseImports();
|
||||
$existingConstantUseImports = $usedImports->getConstantImports();
|
||||
$existingFunctionUseImports = $usedImports->getFunctionImports();
|
||||
$useImportTypes = $this->diffFullyQualifiedObjectTypes($useImportTypes, $existingUseImportTypes);
|
||||
$constantUseImportTypes = $this->diffFullyQualifiedObjectTypes($constantUseImportTypes, $existingConstantUseImports);
|
||||
$functionUseImportTypes = $this->diffFullyQualifiedObjectTypes($functionUseImportTypes, $existingFunctionUseImports);
|
||||
|
@ -83,9 +84,10 @@ final class UseImportsAdder
|
|||
public function addImportsToNamespace(Namespace_ $namespace, array $useImportTypes, array $constantUseImportTypes, array $functionUseImportTypes) : void
|
||||
{
|
||||
$namespaceName = $this->getNamespaceName($namespace);
|
||||
$existingUseImportTypes = $this->usedImportsResolver->resolveForStmts($namespace->stmts);
|
||||
$existingConstantUseImportTypes = $this->usedImportsResolver->resolveConstantImportsForStmts($namespace->stmts);
|
||||
$existingFunctionUseImportTypes = $this->usedImportsResolver->resolveFunctionImportsForStmts($namespace->stmts);
|
||||
$existingUsedImports = $this->usedImportsResolver->resolveForStmts($namespace->stmts);
|
||||
$existingUseImportTypes = $existingUsedImports->getUseImports();
|
||||
$existingConstantUseImportTypes = $existingUsedImports->getConstantImports();
|
||||
$existingFunctionUseImportTypes = $existingUsedImports->getFunctionImports();
|
||||
$existingUseImportTypes = $this->typeFactory->uniquateTypes($existingUseImportTypes);
|
||||
$useImportTypes = $this->diffFullyQualifiedObjectTypes($useImportTypes, $existingUseImportTypes);
|
||||
$constantUseImportTypes = $this->diffFullyQualifiedObjectTypes($constantUseImportTypes, $existingConstantUseImportTypes);
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
declare (strict_types=1);
|
||||
namespace Rector\CodingStyle\ClassNameImport;
|
||||
|
||||
use PhpParser\Node\Stmt\Use_;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Stmt;
|
||||
|
@ -44,7 +45,11 @@ final class AliasUsesResolver
|
|||
public function resolveFromStmts(array $stmts) : array
|
||||
{
|
||||
$aliasedUses = [];
|
||||
$this->useImportsTraverser->traverserStmts($stmts, static function (UseUse $useUse, string $name) use(&$aliasedUses) : void {
|
||||
/** @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;
|
||||
}
|
||||
if (!$useUse->alias instanceof Identifier) {
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -32,68 +32,41 @@ final class UseImportsTraverser
|
|||
}
|
||||
/**
|
||||
* @param Stmt[] $stmts
|
||||
* @param callable(UseUse $useUse, string $name): void $callable
|
||||
* @param callable(Use_::TYPE_* $useType, UseUse $useUse, string $name): void $callable
|
||||
*/
|
||||
public function traverserStmts(array $stmts, callable $callable) : void
|
||||
{
|
||||
$this->traverseForType($stmts, $callable, Use_::TYPE_NORMAL);
|
||||
}
|
||||
/**
|
||||
* @param Stmt[] $stmts
|
||||
* @param callable(UseUse $useUse, string $name): void $callable
|
||||
*/
|
||||
public function traverserStmtsForConstants(array $stmts, callable $callable) : void
|
||||
{
|
||||
$this->traverseForType($stmts, $callable, Use_::TYPE_CONSTANT);
|
||||
}
|
||||
/**
|
||||
* @param Stmt[] $stmts
|
||||
* @param callable(UseUse $useUse, string $name): void $callable
|
||||
*/
|
||||
public function traverserStmtsForFunctions(array $stmts, callable $callable) : void
|
||||
{
|
||||
$this->traverseForType($stmts, $callable, Use_::TYPE_FUNCTION);
|
||||
}
|
||||
/**
|
||||
* @param callable(UseUse $useUse, string $name): void $callable
|
||||
* @param Stmt[] $stmts
|
||||
*/
|
||||
private function traverseForType(array $stmts, callable $callable, int $desiredType) : void
|
||||
{
|
||||
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($stmts, function (Node $node) use($callable, $desiredType) : ?int {
|
||||
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($stmts, function (Node $node) use($callable) : ?int {
|
||||
if ($node instanceof Namespace_ || $node instanceof FileWithoutNamespace) {
|
||||
// traverse into namespaces
|
||||
return null;
|
||||
}
|
||||
if ($node instanceof Use_ && $node->type === $desiredType) {
|
||||
if ($node instanceof Use_) {
|
||||
foreach ($node->uses as $useUse) {
|
||||
$name = $this->nodeNameResolver->getName($useUse);
|
||||
if ($name === null) {
|
||||
continue;
|
||||
}
|
||||
$callable($useUse, $name);
|
||||
$callable($node->type, $useUse, $name);
|
||||
}
|
||||
} elseif ($node instanceof GroupUse) {
|
||||
$this->processGroupUse($node, $desiredType, $callable);
|
||||
$this->processGroupUse($node, $callable);
|
||||
}
|
||||
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
|
||||
});
|
||||
}
|
||||
/**
|
||||
* @param callable(UseUse $useUse, string $name): void $callable
|
||||
* @param callable(Use_::TYPE_* $useType, UseUse $useUse, string $name): void $callable
|
||||
*/
|
||||
private function processGroupUse(GroupUse $groupUse, int $desiredType, callable $callable) : void
|
||||
private function processGroupUse(GroupUse $groupUse, callable $callable) : void
|
||||
{
|
||||
if ($groupUse->type !== Use_::TYPE_UNKNOWN) {
|
||||
return;
|
||||
}
|
||||
$prefixName = $groupUse->prefix->toString();
|
||||
foreach ($groupUse->uses as $useUse) {
|
||||
if ($useUse->type !== $desiredType) {
|
||||
continue;
|
||||
}
|
||||
$name = $prefixName . '\\' . $this->nodeNameResolver->getName($useUse);
|
||||
$callable($useUse, $name);
|
||||
$callable($useUse->type, $useUse, $name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,10 +3,12 @@
|
|||
declare (strict_types=1);
|
||||
namespace Rector\CodingStyle\ClassNameImport;
|
||||
|
||||
use PhpParser\Node\Stmt\Use_;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\UseUse;
|
||||
use Rector\CodingStyle\ClassNameImport\ValueObject\UsedImports;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
|
||||
|
@ -36,9 +38,8 @@ final class UsedImportsResolver
|
|||
}
|
||||
/**
|
||||
* @param Stmt[] $stmts
|
||||
* @return array<FullyQualifiedObjectType|AliasedObjectType>
|
||||
*/
|
||||
public function resolveForStmts(array $stmts) : array
|
||||
public function resolveForStmts(array $stmts) : UsedImports
|
||||
{
|
||||
$usedImports = [];
|
||||
/** @var Class_|null $class */
|
||||
|
@ -49,37 +50,24 @@ final class UsedImportsResolver
|
|||
$className = (string) $this->nodeNameResolver->getName($class);
|
||||
$usedImports[] = new FullyQualifiedObjectType($className);
|
||||
}
|
||||
$this->useImportsTraverser->traverserStmts($stmts, static function (UseUse $useUse, string $name) use(&$usedImports) : void {
|
||||
if ($useUse->alias instanceof Identifier) {
|
||||
$usedImports[] = new AliasedObjectType($useUse->alias->toString(), $name);
|
||||
} else {
|
||||
$usedImports[] = new FullyQualifiedObjectType($name);
|
||||
$usedConstImports = [];
|
||||
$usedFunctionImports = [];
|
||||
/** @param Use_::TYPE_* $useType */
|
||||
$this->useImportsTraverser->traverserStmts($stmts, static function (int $useType, UseUse $useUse, string $name) use(&$usedImports, &$usedFunctionImports, &$usedConstImports) : void {
|
||||
if ($useType === Use_::TYPE_NORMAL) {
|
||||
if ($useUse->alias instanceof Identifier) {
|
||||
$usedImports[] = new AliasedObjectType($useUse->alias->toString(), $name);
|
||||
} else {
|
||||
$usedImports[] = new FullyQualifiedObjectType($name);
|
||||
}
|
||||
}
|
||||
if ($useType === Use_::TYPE_FUNCTION) {
|
||||
$usedFunctionImports[] = new FullyQualifiedObjectType($name);
|
||||
}
|
||||
if ($useType === Use_::TYPE_CONSTANT) {
|
||||
$usedConstImports[] = new FullyQualifiedObjectType($name);
|
||||
}
|
||||
});
|
||||
return $usedImports;
|
||||
}
|
||||
/**
|
||||
* @param Stmt[] $stmts
|
||||
* @return FullyQualifiedObjectType[]
|
||||
*/
|
||||
public function resolveConstantImportsForStmts(array $stmts) : array
|
||||
{
|
||||
$usedConstImports = [];
|
||||
$this->useImportsTraverser->traverserStmtsForConstants($stmts, static function (UseUse $useUse, string $name) use(&$usedConstImports) : void {
|
||||
$usedConstImports[] = new FullyQualifiedObjectType($name);
|
||||
});
|
||||
return $usedConstImports;
|
||||
}
|
||||
/**
|
||||
* @param Stmt[] $stmts
|
||||
* @return FullyQualifiedObjectType[]
|
||||
*/
|
||||
public function resolveFunctionImportsForStmts(array $stmts) : array
|
||||
{
|
||||
$usedFunctionImports = [];
|
||||
$this->useImportsTraverser->traverserStmtsForFunctions($stmts, static function (UseUse $useUse, string $name) use(&$usedFunctionImports) : void {
|
||||
$usedFunctionImports[] = new FullyQualifiedObjectType($name);
|
||||
});
|
||||
return $usedFunctionImports;
|
||||
return new UsedImports($usedImports, $usedFunctionImports, $usedConstImports);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\CodingStyle\ClassNameImport\ValueObject;
|
||||
|
||||
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
|
||||
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
|
||||
final class UsedImports
|
||||
{
|
||||
/**
|
||||
* @var array<(FullyQualifiedObjectType | AliasedObjectType)>
|
||||
* @readonly
|
||||
*/
|
||||
private $useImports;
|
||||
/**
|
||||
* @var FullyQualifiedObjectType[]
|
||||
* @readonly
|
||||
*/
|
||||
private $functionImports;
|
||||
/**
|
||||
* @var FullyQualifiedObjectType[]
|
||||
* @readonly
|
||||
*/
|
||||
private $constantImports;
|
||||
/**
|
||||
* @param array<FullyQualifiedObjectType|AliasedObjectType> $useImports
|
||||
* @param FullyQualifiedObjectType[] $functionImports
|
||||
* @param FullyQualifiedObjectType[] $constantImports
|
||||
*/
|
||||
public function __construct(array $useImports, array $functionImports, array $constantImports)
|
||||
{
|
||||
$this->useImports = $useImports;
|
||||
$this->functionImports = $functionImports;
|
||||
$this->constantImports = $constantImports;
|
||||
}
|
||||
/**
|
||||
* @return array<FullyQualifiedObjectType|AliasedObjectType>
|
||||
*/
|
||||
public function getUseImports() : array
|
||||
{
|
||||
return $this->useImports;
|
||||
}
|
||||
/**
|
||||
* @return FullyQualifiedObjectType[]
|
||||
*/
|
||||
public function getFunctionImports() : array
|
||||
{
|
||||
return $this->functionImports;
|
||||
}
|
||||
/**
|
||||
* @return FullyQualifiedObjectType[]
|
||||
*/
|
||||
public function getConstantImports() : array
|
||||
{
|
||||
return $this->constantImports;
|
||||
}
|
||||
}
|
|
@ -19,12 +19,12 @@ final class VersionResolver
|
|||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = 'cbc632c1eda3f2b9aba3fba07b0f1b45cc58caec';
|
||||
public const PACKAGE_VERSION = 'aeba96a811638e3954028b70d8163c43a8866fa8';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2023-09-12 18:55:31';
|
||||
public const RELEASE_DATE = '2023-09-12 14:35:11';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
|
|
@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
|
|||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInitcf163137832100dcfaee9be37765beff::getLoader();
|
||||
return ComposerAutoloaderInit07977e21b93fabd68f9574de1dd036c6::getLoader();
|
||||
|
|
|
@ -1101,6 +1101,7 @@ return array(
|
|||
'Rector\\CodingStyle\\ClassNameImport\\ShortNameResolver' => $baseDir . '/rules/CodingStyle/ClassNameImport/ShortNameResolver.php',
|
||||
'Rector\\CodingStyle\\ClassNameImport\\UseImportsTraverser' => $baseDir . '/rules/CodingStyle/ClassNameImport/UseImportsTraverser.php',
|
||||
'Rector\\CodingStyle\\ClassNameImport\\UsedImportsResolver' => $baseDir . '/rules/CodingStyle/ClassNameImport/UsedImportsResolver.php',
|
||||
'Rector\\CodingStyle\\ClassNameImport\\ValueObject\\UsedImports' => $baseDir . '/rules/CodingStyle/ClassNameImport/ValueObject/UsedImports.php',
|
||||
'Rector\\CodingStyle\\Contract\\ClassNameImport\\ClassNameImportSkipVoterInterface' => $baseDir . '/rules/CodingStyle/Contract/ClassNameImport/ClassNameImportSkipVoterInterface.php',
|
||||
'Rector\\CodingStyle\\Guard\\StaticGuard' => $baseDir . '/rules/CodingStyle/Guard/StaticGuard.php',
|
||||
'Rector\\CodingStyle\\Naming\\ClassNaming' => $baseDir . '/rules/CodingStyle/Naming/ClassNaming.php',
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInitcf163137832100dcfaee9be37765beff
|
||||
class ComposerAutoloaderInit07977e21b93fabd68f9574de1dd036c6
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
|
@ -22,17 +22,17 @@ class ComposerAutoloaderInitcf163137832100dcfaee9be37765beff
|
|||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInitcf163137832100dcfaee9be37765beff', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInit07977e21b93fabd68f9574de1dd036c6', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitcf163137832100dcfaee9be37765beff', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit07977e21b93fabd68f9574de1dd036c6', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitcf163137832100dcfaee9be37765beff::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit07977e21b93fabd68f9574de1dd036c6::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInitcf163137832100dcfaee9be37765beff::$files;
|
||||
$filesToLoad = \Composer\Autoload\ComposerStaticInit07977e21b93fabd68f9574de1dd036c6::$files;
|
||||
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInitcf163137832100dcfaee9be37765beff
|
||||
class ComposerStaticInit07977e21b93fabd68f9574de1dd036c6
|
||||
{
|
||||
public static $files = array (
|
||||
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
|
||||
|
@ -1321,6 +1321,7 @@ class ComposerStaticInitcf163137832100dcfaee9be37765beff
|
|||
'Rector\\CodingStyle\\ClassNameImport\\ShortNameResolver' => __DIR__ . '/../..' . '/rules/CodingStyle/ClassNameImport/ShortNameResolver.php',
|
||||
'Rector\\CodingStyle\\ClassNameImport\\UseImportsTraverser' => __DIR__ . '/../..' . '/rules/CodingStyle/ClassNameImport/UseImportsTraverser.php',
|
||||
'Rector\\CodingStyle\\ClassNameImport\\UsedImportsResolver' => __DIR__ . '/../..' . '/rules/CodingStyle/ClassNameImport/UsedImportsResolver.php',
|
||||
'Rector\\CodingStyle\\ClassNameImport\\ValueObject\\UsedImports' => __DIR__ . '/../..' . '/rules/CodingStyle/ClassNameImport/ValueObject/UsedImports.php',
|
||||
'Rector\\CodingStyle\\Contract\\ClassNameImport\\ClassNameImportSkipVoterInterface' => __DIR__ . '/../..' . '/rules/CodingStyle/Contract/ClassNameImport/ClassNameImportSkipVoterInterface.php',
|
||||
'Rector\\CodingStyle\\Guard\\StaticGuard' => __DIR__ . '/../..' . '/rules/CodingStyle/Guard/StaticGuard.php',
|
||||
'Rector\\CodingStyle\\Naming\\ClassNaming' => __DIR__ . '/../..' . '/rules/CodingStyle/Naming/ClassNaming.php',
|
||||
|
@ -2592,9 +2593,9 @@ class ComposerStaticInitcf163137832100dcfaee9be37765beff
|
|||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitcf163137832100dcfaee9be37765beff::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitcf163137832100dcfaee9be37765beff::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitcf163137832100dcfaee9be37765beff::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit07977e21b93fabd68f9574de1dd036c6::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit07977e21b93fabd68f9574de1dd036c6::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit07977e21b93fabd68f9574de1dd036c6::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue