Updated Rector to commit 40a1f34b12c548f7be05f02a084be54e8374a58a

40a1f34b12 [Naming] Remove parent lookup on UseImportsResolver (#4367)
This commit is contained in:
Tomas Votruba 2023-06-27 17:32:34 +00:00
parent 8264d403d3
commit a3231bb81d
22 changed files with 116 additions and 46 deletions

View File

@ -61,7 +61,7 @@ final class ClassAnnotationMatcher
return $this->fullyQualifiedNameByHash[$uniqueHash];
}
$tag = \ltrim($tag, '@');
$uses = $this->useImportsResolver->resolveForNode($node);
$uses = $this->useImportsResolver->resolve();
$fullyQualifiedClass = $this->resolveFullyQualifiedClass($uses, $node, $tag, $returnNullOnUnknownClass);
if ($fullyQualifiedClass === null) {
if ($returnNullOnUnknownClass) {

View File

@ -7,6 +7,7 @@ use RectorPrefix202306\Nette\Utils\FileSystem;
use PhpParser\Node\Stmt;
use Rector\Core\PhpParser\NodeTraverser\FileWithoutNamespaceNodeTraverser;
use Rector\Core\PhpParser\Parser\RectorParser;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;
/**
@ -29,11 +30,17 @@ final class FileInfoParser
* @var \Rector\Core\PhpParser\Parser\RectorParser
*/
private $rectorParser;
public function __construct(NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator, FileWithoutNamespaceNodeTraverser $fileWithoutNamespaceNodeTraverser, RectorParser $rectorParser)
/**
* @readonly
* @var \Rector\Core\Provider\CurrentFileProvider
*/
private $currentFileProvider;
public function __construct(NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator, FileWithoutNamespaceNodeTraverser $fileWithoutNamespaceNodeTraverser, RectorParser $rectorParser, CurrentFileProvider $currentFileProvider)
{
$this->nodeScopeAndMetadataDecorator = $nodeScopeAndMetadataDecorator;
$this->fileWithoutNamespaceNodeTraverser = $fileWithoutNamespaceNodeTraverser;
$this->rectorParser = $rectorParser;
$this->currentFileProvider = $currentFileProvider;
}
/**
* @api tests only
@ -44,6 +51,9 @@ final class FileInfoParser
$stmts = $this->rectorParser->parseFile($filePath);
$stmts = $this->fileWithoutNamespaceNodeTraverser->traverse($stmts);
$file = new File($filePath, FileSystem::read($filePath));
return $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($file, $stmts);
$stmts = $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($file, $stmts);
$file->hydrateStmtsAndTokens($stmts, $stmts, []);
$this->currentFileProvider->setFile($file);
return $stmts;
}
}

View File

@ -122,7 +122,7 @@ final class ClassRenamePhpDocNodeVisitor extends AbstractPhpDocNodeVisitor
if ($staticType instanceof ShortenedObjectType) {
return $name;
}
$uses = $this->useImportsResolver->resolveForNode($phpParserNode);
$uses = $this->useImportsResolver->resolve();
$scope = $phpParserNode->getAttribute(AttributeKey::SCOPE);
if (!$scope instanceof Scope) {
if (!$phpParserNode->hasAttribute(AttributeKey::ORIGINAL_NODE)) {

View File

@ -60,7 +60,7 @@ final class UseNodesToAddCollector implements NodeCollectorInterface
{
$filePath = $file->getFilePath();
$objectTypes = $this->useImportTypesInFilePath[$filePath] ?? [];
$uses = $this->useImportsResolver->resolveForNode($node);
$uses = $this->useImportsResolver->resolve();
foreach ($uses as $use) {
$prefix = $this->useImportsResolver->resolvePrefix($use);
foreach ($use->uses as $useUse) {

View File

@ -142,7 +142,7 @@ CODE_SAMPLE
return null;
}
/** @var Use_[]|GroupUse[] $currentUses */
$currentUses = $this->useImportsResolver->resolveForNode($name);
$currentUses = $this->useImportsResolver->resolve();
if ($this->shouldImportName($name, $currentUses)) {
$nameInUse = $this->resolveNameInUse($name, $currentUses);
if ($nameInUse instanceof FullyQualified) {

View File

@ -120,7 +120,7 @@ CODE_SAMPLE
// B. no namespace? add in the top
$useImportTypes = $this->filterOutNonNamespacedNames($useImportTypes);
// then add, to prevent adding + removing false positive of same short use
return $this->useImportsAdder->addImportsToStmts($nodes, $useImportTypes, $functionUseImportTypes);
return $this->useImportsAdder->addImportsToStmts($namespace, $nodes, $useImportTypes, $functionUseImportTypes);
}
/**
* Prevents

View File

@ -61,7 +61,7 @@ final class NameScopeFactory
{
$scope = $node->getAttribute(AttributeKey::SCOPE);
$namespace = $scope instanceof Scope ? $scope->getNamespace() : null;
$uses = $this->useImportsResolver->resolveForNode($node);
$uses = $this->useImportsResolver->resolve();
$usesAliasesToNames = $this->resolveUseNamesByAlias($uses);
if ($scope instanceof Scope && $scope->getClassReflection() instanceof ClassReflection) {
$classReflection = $scope->getClassReflection();

View File

@ -8,6 +8,7 @@ use PhpParser\Node;
use Rector\Core\Configuration\Option;
use Rector\Core\Configuration\Parameter\ParameterProvider;
use Rector\Core\PhpParser\Parser\RectorParser;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;
/**
@ -30,17 +31,25 @@ final class TestingParser
* @var \Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator
*/
private $nodeScopeAndMetadataDecorator;
public function __construct(ParameterProvider $parameterProvider, RectorParser $rectorParser, NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator)
/**
* @readonly
* @var \Rector\Core\Provider\CurrentFileProvider
*/
private $currentFileProvider;
public function __construct(ParameterProvider $parameterProvider, RectorParser $rectorParser, NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator, CurrentFileProvider $currentFileProvider)
{
$this->parameterProvider = $parameterProvider;
$this->rectorParser = $rectorParser;
$this->nodeScopeAndMetadataDecorator = $nodeScopeAndMetadataDecorator;
$this->currentFileProvider = $currentFileProvider;
}
public function parseFilePathToFile(string $filePath) : File
{
$file = new File($filePath, FileSystem::read($filePath));
$stmts = $this->rectorParser->parseFile($filePath);
$stmts = $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($file, $stmts);
$file->hydrateStmtsAndTokens($stmts, $stmts, []);
$this->currentFileProvider->setFile($file);
return $file;
}
/**
@ -49,8 +58,11 @@ final class TestingParser
public function parseFileToDecoratedNodes(string $filePath) : array
{
$this->parameterProvider->changeParameter(Option::SOURCE, [$filePath]);
$nodes = $this->rectorParser->parseFile($filePath);
$stmts = $this->rectorParser->parseFile($filePath);
$file = new File($filePath, FileSystem::read($filePath));
return $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($file, $nodes);
$stmts = $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($file, $stmts);
$file->hydrateStmtsAndTokens($stmts, $stmts, []);
$this->currentFileProvider->setFile($file);
return $stmts;
}
}

View File

@ -12,6 +12,7 @@ use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\Use_;
use PHPStan\Type\ObjectType;
use Rector\CodingStyle\ClassNameImport\UsedImportsResolver;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\StaticTypeMapper\ValueObject\Type\AliasedObjectType;
@ -39,7 +40,7 @@ final class UseImportsAdder
* @param array<FullyQualifiedObjectType|AliasedObjectType> $functionUseImportTypes
* @return Stmt[]
*/
public function addImportsToStmts(array $stmts, array $useImportTypes, array $functionUseImportTypes) : array
public function addImportsToStmts(FileWithoutNamespace $fileWithoutNamespace, array $stmts, array $useImportTypes, array $functionUseImportTypes) : array
{
$existingUseImportTypes = $this->usedImportsResolver->resolveForStmts($stmts);
$existingFunctionUseImports = $this->usedImportsResolver->resolveFunctionImportsForStmts($stmts);
@ -60,12 +61,16 @@ final class UseImportsAdder
}
$this->mirrorUseComments($stmts, $newUses, $key + 1);
\array_splice($stmts, $key + 1, 0, $nodesToAdd);
return $stmts;
$fileWithoutNamespace->stmts = $stmts;
$fileWithoutNamespace->stmts = \array_values($fileWithoutNamespace->stmts);
return $fileWithoutNamespace->stmts;
}
}
$this->mirrorUseComments($stmts, $newUses);
// make use stmts first
return \array_merge($newUses, $stmts);
$fileWithoutNamespace->stmts = \array_merge($newUses, $stmts);
$fileWithoutNamespace->stmts = \array_values($fileWithoutNamespace->stmts);
return $fileWithoutNamespace->stmts;
}
/**
* @param FullyQualifiedObjectType[] $useImportTypes
@ -85,6 +90,7 @@ final class UseImportsAdder
}
$this->mirrorUseComments($namespace->stmts, $newUses);
$namespace->stmts = \array_merge($newUses, $namespace->stmts);
$namespace->stmts = \array_values($namespace->stmts);
}
/**
* @param Stmt[] $stmts

View File

@ -18,7 +18,7 @@ final class AliasNameResolver
}
public function resolveByName(Name $name) : ?string
{
$uses = $this->useImportsResolver->resolveForNode($name);
$uses = $this->useImportsResolver->resolve();
$nameString = $name->toString();
foreach ($uses as $use) {
$prefix = $this->useImportsResolver->resolvePrefix($use);

View File

@ -8,25 +8,67 @@ use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\GroupUse;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Use_;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Core\PhpParser\NodeTraverser\FileWithoutNamespaceNodeTraverser;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\ValueObject\Application\File;
final class UseImportsResolver
{
/**
* @readonly
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
* @var \Rector\Core\Provider\CurrentFileProvider
*/
private $betterNodeFinder;
public function __construct(BetterNodeFinder $betterNodeFinder)
private $currentFileProvider;
/**
* @readonly
* @var \Rector\Core\PhpParser\NodeTraverser\FileWithoutNamespaceNodeTraverser
*/
private $fileWithoutNamespaceNodeTraverser;
public function __construct(CurrentFileProvider $currentFileProvider, FileWithoutNamespaceNodeTraverser $fileWithoutNamespaceNodeTraverser)
{
$this->betterNodeFinder = $betterNodeFinder;
$this->currentFileProvider = $currentFileProvider;
$this->fileWithoutNamespaceNodeTraverser = $fileWithoutNamespaceNodeTraverser;
}
/**
* @return \PhpParser\Node\Stmt\Namespace_|\Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace|null
*/
private function resolveNamespace()
{
/** @var File|null $file */
$file = $this->currentFileProvider->getFile();
if (!$file instanceof File) {
return null;
}
$newStmts = $file->getNewStmts();
if ($newStmts === []) {
return null;
}
$namespaces = \array_filter($newStmts, static function (Stmt $stmt) : bool {
return $stmt instanceof Namespace_;
});
// multiple namespaces is not supported
if (\count($namespaces) > 1) {
return null;
}
$currentNamespace = \current($namespaces);
if ($currentNamespace instanceof Namespace_) {
return $currentNamespace;
}
$currentStmt = \current($newStmts);
if (!$currentStmt instanceof FileWithoutNamespace) {
$newStmts = $this->fileWithoutNamespaceNodeTraverser->traverse($newStmts);
/** @var FileWithoutNamespace $currentStmt */
$currentStmt = \current($newStmts);
return $currentStmt;
}
return $currentStmt;
}
/**
* @return Use_[]|GroupUse[]
*/
public function resolveForNode(Node $node) : array
public function resolve() : array
{
$namespace = $this->betterNodeFinder->findParentByTypes($node, [Namespace_::class, FileWithoutNamespace::class]);
$namespace = $this->resolveNamespace();
if (!$namespace instanceof Node) {
return [];
}
@ -38,9 +80,9 @@ final class UseImportsResolver
* @api
* @return Use_[]
*/
public function resolveBareUsesForNode(Node $node) : array
public function resolveBareUses() : array
{
$namespace = $this->betterNodeFinder->findParentByTypes($node, [Namespace_::class, FileWithoutNamespace::class]);
$namespace = $this->resolveNamespace();
if (!$namespace instanceof Node) {
return [];
}

View File

@ -129,7 +129,7 @@ CODE_SAMPLE
if (!$phpDocInfo instanceof PhpDocInfo) {
return null;
}
$uses = $this->useImportsResolver->resolveBareUsesForNode($node);
$uses = $this->useImportsResolver->resolveBareUses();
// 1. bare tags without annotation class, e.g. "@inject"
$genericAttributeGroups = $this->processGenericTags($phpDocInfo);
// 2. Doctrine annotation classes

View File

@ -111,7 +111,7 @@ CODE_SAMPLE
if (!$phpDocInfo instanceof PhpDocInfo) {
return null;
}
$uses = $this->useImportsResolver->resolveBareUsesForNode($node);
$uses = $this->useImportsResolver->resolveBareUses();
$attributeGroups = $this->transformDoctrineAnnotationClassesToAttributeGroups($phpDocInfo, $uses);
if ($attributeGroups === []) {
return null;

View File

@ -341,7 +341,7 @@ final class ClassRenamer
}
private function isValidUseImportChange(string $newName, UseUse $useUse) : bool
{
$uses = $this->useImportsResolver->resolveForNode($useUse);
$uses = $this->useImportsResolver->resolve();
if ($uses === []) {
return \true;
}

View File

@ -60,7 +60,7 @@ final class ObjectTypeSpecifier
}
}
}
$uses = $this->useImportsResolver->resolveForNode($node);
$uses = $this->useImportsResolver->resolve();
if ($uses === []) {
if (!$this->reflectionProvider->hasClass($objectType->getClassName())) {
return new NonExistingObjectType($objectType->getClassName());

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'b408d9e7cdec8dadcee766681b8bd8185da55097';
public const PACKAGE_VERSION = '40a1f34b12c548f7be05f02a084be54e8374a58a';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-06-27 16:20:06';
public const RELEASE_DATE = '2023-06-28 00:27:59';
/**
* @var int
*/

2
vendor/autoload.php vendored
View File

@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit5fec7ffbf26461fa70a699457614ecb7::getLoader();
return ComposerAutoloaderInit164e89c268f0277d2e62d6c923fb9f89::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit5fec7ffbf26461fa70a699457614ecb7
class ComposerAutoloaderInit164e89c268f0277d2e62d6c923fb9f89
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInit5fec7ffbf26461fa70a699457614ecb7
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit5fec7ffbf26461fa70a699457614ecb7', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit164e89c268f0277d2e62d6c923fb9f89', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit5fec7ffbf26461fa70a699457614ecb7', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit164e89c268f0277d2e62d6c923fb9f89', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit5fec7ffbf26461fa70a699457614ecb7::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit164e89c268f0277d2e62d6c923fb9f89::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit5fec7ffbf26461fa70a699457614ecb7::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit164e89c268f0277d2e62d6c923fb9f89::$files;
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit5fec7ffbf26461fa70a699457614ecb7
class ComposerStaticInit164e89c268f0277d2e62d6c923fb9f89
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -3098,9 +3098,9 @@ class ComposerStaticInit5fec7ffbf26461fa70a699457614ecb7
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit5fec7ffbf26461fa70a699457614ecb7::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit5fec7ffbf26461fa70a699457614ecb7::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit5fec7ffbf26461fa70a699457614ecb7::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit164e89c268f0277d2e62d6c923fb9f89::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit164e89c268f0277d2e62d6c923fb9f89::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit164e89c268f0277d2e62d6c923fb9f89::$classMap;
}, null, ClassLoader::class);
}

View File

@ -2121,12 +2121,12 @@
"source": {
"type": "git",
"url": "https:\/\/github.com\/rectorphp\/rector-symfony.git",
"reference": "6a55f36cb3f1eee5058252ca0f536f57ab3fb74c"
"reference": "30b651c19dd56dc333c13ece88dc6ed75d39bc53"
},
"dist": {
"type": "zip",
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-symfony\/zipball\/6a55f36cb3f1eee5058252ca0f536f57ab3fb74c",
"reference": "6a55f36cb3f1eee5058252ca0f536f57ab3fb74c",
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-symfony\/zipball\/30b651c19dd56dc333c13ece88dc6ed75d39bc53",
"reference": "30b651c19dd56dc333c13ece88dc6ed75d39bc53",
"shasum": ""
},
"require": {
@ -2156,7 +2156,7 @@
"tomasvotruba\/type-coverage": "^0.2",
"tomasvotruba\/unused-public": "^0.1"
},
"time": "2023-06-27T15:19:27+00:00",
"time": "2023-06-27T15:53:26+00:00",
"default-branch": true,
"type": "rector-extension",
"extra": {

File diff suppressed because one or more lines are too long

View File

@ -9,7 +9,7 @@ namespace Rector\RectorInstaller;
*/
final class GeneratedConfig
{
public const EXTENSIONS = array('rector/rector-doctrine' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-doctrine', 'relative_install_path' => '../../rector-doctrine', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main fc2dbbd'), 'rector/rector-downgrade-php' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-downgrade-php', 'relative_install_path' => '../../rector-downgrade-php', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 06c6448'), 'rector/rector-phpunit' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-phpunit', 'relative_install_path' => '../../rector-phpunit', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main c6bf48b'), 'rector/rector-symfony' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-symfony', 'relative_install_path' => '../../rector-symfony', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 6a55f36'));
public const EXTENSIONS = array('rector/rector-doctrine' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-doctrine', 'relative_install_path' => '../../rector-doctrine', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main fc2dbbd'), 'rector/rector-downgrade-php' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-downgrade-php', 'relative_install_path' => '../../rector-downgrade-php', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 06c6448'), 'rector/rector-phpunit' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-phpunit', 'relative_install_path' => '../../rector-phpunit', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main c6bf48b'), 'rector/rector-symfony' => array('install_path' => '/home/runner/work/rector-src/rector-src/vendor/rector/rector-symfony', 'relative_install_path' => '../../rector-symfony', 'extra' => array('includes' => array(0 => 'config/config.php')), 'version' => 'dev-main 30b651c'));
private function __construct()
{
}