Skip to content

Commit

Permalink
Updated Rector to commit 40a1f34b12c548f7be05f02a084be54e8374a58a
Browse files Browse the repository at this point in the history
rectorphp/rector-src@40a1f34 [Naming] Remove parent lookup on UseImportsResolver (#4367)
  • Loading branch information
TomasVotruba committed Jun 27, 2023
1 parent 8264d40 commit a3231bb
Show file tree
Hide file tree
Showing 22 changed files with 116 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private function _resolveTagFullyQualifiedName(string $tag, Node $node, bool $re
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) {
Expand Down
14 changes: 12 additions & 2 deletions packages/FileSystemRector/Parser/FileInfoParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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;
/**
Expand All @@ -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
Expand All @@ -44,6 +51,9 @@ public function parseFileInfoToNodesAndDecorate(string $filePath) : array
$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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private function resolveNamespacedName(IdentifierTypeNode $identifierTypeNode, P
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)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/PostRector/Collector/UseNodesToAddCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function getUseImportTypesByNode(File $file, Node $node) : array
{
$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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/PostRector/Rector/NameImportingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private function processNodeName(Name $name, File $file) : ?Node
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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/PostRector/Rector/UseAddingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private function resolveNodesWithImportedUses(array $nodes, array $useImportType
// 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
Expand Down
2 changes: 1 addition & 1 deletion packages/StaticTypeMapper/Naming/NameScopeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function createNameScopeFromNodeWithoutTemplateTypes(Node $node) : NameSc
{
$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();
Expand Down
18 changes: 15 additions & 3 deletions packages/Testing/TestingParser/TestingParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
/**
Expand All @@ -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;
}
/**
Expand All @@ -49,8 +58,11 @@ public function parseFilePathToFile(string $filePath) : File
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;
}
}
12 changes: 9 additions & 3 deletions rules/CodingStyle/Application/UseImportsAdder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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;
Expand Down Expand Up @@ -39,7 +40,7 @@ public function __construct(UsedImportsResolver $usedImportsResolver, TypeFactor
* @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);
Expand All @@ -60,12 +61,16 @@ public function addImportsToStmts(array $stmts, array $useImportTypes, array $fu
}
$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
Expand All @@ -85,6 +90,7 @@ public function addImportsToNamespace(Namespace_ $namespace, array $useImportTyp
}
$this->mirrorUseComments($namespace->stmts, $newUses);
$namespace->stmts = \array_merge($newUses, $namespace->stmts);
$namespace->stmts = \array_values($namespace->stmts);
}
/**
* @param Stmt[] $stmts
Expand Down
2 changes: 1 addition & 1 deletion rules/Naming/Naming/AliasNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(\Rector\Naming\Naming\UseImportsResolver $useImports
}
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);
Expand Down
60 changes: 51 additions & 9 deletions rules/Naming/Naming/UseImportsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,67 @@
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->currentFileProvider = $currentFileProvider;
$this->fileWithoutNamespaceNodeTraverser = $fileWithoutNamespaceNodeTraverser;
}
/**
* @return \PhpParser\Node\Stmt\Namespace_|\Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace|null
*/
private function resolveNamespace()
{
$this->betterNodeFinder = $betterNodeFinder;
/** @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 [];
}
Expand All @@ -38,9 +80,9 @@ public function resolveForNode(Node $node) : array
* @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 [];
}
Expand Down
2 changes: 1 addition & 1 deletion rules/Php80/Rector/Class_/AnnotationToAttributeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function refactor(Node $node) : ?Node
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function refactor(Node $node) : ?Node
if (!$phpDocInfo instanceof PhpDocInfo) {
return null;
}
$uses = $this->useImportsResolver->resolveBareUsesForNode($node);
$uses = $this->useImportsResolver->resolveBareUses();
$attributeGroups = $this->transformDoctrineAnnotationClassesToAttributeGroups($phpDocInfo, $uses);
if ($attributeGroups === []) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion rules/Renaming/NodeManipulator/ClassRenamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ private function isValidClassNameChange(Name $name, Class_ $class, ClassReflecti
}
private function isValidUseImportChange(string $newName, UseUse $useUse) : bool
{
$uses = $this->useImportsResolver->resolveForNode($useUse);
$uses = $this->useImportsResolver->resolve();
if ($uses === []) {
return \true;
}
Expand Down
2 changes: 1 addition & 1 deletion rules/TypeDeclaration/PHPStan/ObjectTypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function narrowToFullyQualifiedOrAliasedObjectType(Node $node, ObjectType
}
}
}
$uses = $this->useImportsResolver->resolveForNode($node);
$uses = $this->useImportsResolver->resolve();
if ($uses === []) {
if (!$this->reflectionProvider->hasClass($objectType->getClassName())) {
return new NonExistingObjectType($objectType->getClassName());
Expand Down
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion vendor/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInit5fec7ffbf26461fa70a699457614ecb7::getLoader();
return ComposerAutoloaderInit164e89c268f0277d2e62d6c923fb9f89::getLoader();
10 changes: 5 additions & 5 deletions vendor/composer/autoload_real.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// autoload_real.php @generated by Composer

class ComposerAutoloaderInit5fec7ffbf26461fa70a699457614ecb7
class ComposerAutoloaderInit164e89c268f0277d2e62d6c923fb9f89
{
private static $loader;

Expand All @@ -22,17 +22,17 @@ public static function getLoader()
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;
Expand Down
8 changes: 4 additions & 4 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Composer\Autoload;

class ComposerStaticInit5fec7ffbf26461fa70a699457614ecb7
class ComposerStaticInit164e89c268f0277d2e62d6c923fb9f89
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
Expand Down Expand Up @@ -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);
}
Expand Down
Loading

0 comments on commit a3231bb

Please sign in to comment.