From c0bc2824b0c33259cb1fb7b993afec77eddb987e Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 22 Feb 2024 12:50:50 +0000 Subject: [PATCH] Updated Rector to commit b9ea5fd9961997cecbd872cb345e88de582d6aca https://github.com/rectorphp/rector-src/commit/b9ea5fd9961997cecbd872cb345e88de582d6aca [PostRector] Skip remove use statement on used in annotation on removeUnusedImports() (#5657) --- src/Application/VersionResolver.php | 4 +- .../PhpDocInfo/PhpDocInfo.php | 21 ++++++ .../ArrayItemClassNameDecorator.php | 65 +++++++++++++++++++ .../LazyContainerFactory.php | 3 +- .../Rector/UnusedImportRemovingPostRector.php | 2 + vendor/composer/autoload_classmap.php | 1 + vendor/composer/autoload_static.php | 1 + 7 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 src/BetterPhpDocParser/PhpDocParser/ArrayItemClassNameDecorator.php diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index dfef81624ba..5d04b131738 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -19,12 +19,12 @@ final class VersionResolver * @api * @var string */ - public const PACKAGE_VERSION = 'fb312e00c38b17b096a7257151a4ec0571cd8607'; + public const PACKAGE_VERSION = 'b9ea5fd9961997cecbd872cb345e88de582d6aca'; /** * @api * @var string */ - public const RELEASE_DATE = '2024-02-22 17:05:46'; + public const RELEASE_DATE = '2024-02-22 19:48:37'; /** * @var int */ diff --git a/src/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php b/src/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php index e8977579c52..228860bfb99 100644 --- a/src/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php +++ b/src/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php @@ -23,6 +23,7 @@ use PHPStan\PhpDocParser\Lexer\Lexer; use PHPStan\Type\MixedType; use PHPStan\Type\Type; use Rector\BetterPhpDocParser\Annotation\AnnotationNaming; +use Rector\BetterPhpDocParser\PhpDoc\ArrayItemNode; use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode; use Rector\BetterPhpDocParser\PhpDoc\SpacelessPhpDocTagNode; use Rector\BetterPhpDocParser\PhpDocNodeFinder\PhpDocNodeByTypeFinder; @@ -399,6 +400,26 @@ final class PhpDocInfo }); return $classNames; } + /** + * @return string[] + */ + public function getArrayItemNodeClassNames() : array + { + $phpDocNodeTraverser = new PhpDocNodeTraverser(); + $classNames = []; + $phpDocNodeTraverser->traverseWithCallable($this->phpDocNode, '', static function (Node $node) use(&$classNames) : ?ArrayItemNode { + if (!$node instanceof ArrayItemNode) { + return null; + } + $resolvedClass = $node->getAttribute(PhpDocAttributeKey::RESOLVED_CLASS); + if ($resolvedClass === null) { + return null; + } + $classNames[] = $resolvedClass; + return $node; + }); + return $classNames; + } /** * @param class-string $desiredClass * @return DoctrineAnnotationTagValueNode[] diff --git a/src/BetterPhpDocParser/PhpDocParser/ArrayItemClassNameDecorator.php b/src/BetterPhpDocParser/PhpDocParser/ArrayItemClassNameDecorator.php new file mode 100644 index 00000000000..07bb4476faa --- /dev/null +++ b/src/BetterPhpDocParser/PhpDocParser/ArrayItemClassNameDecorator.php @@ -0,0 +1,65 @@ +nameScopeFactory = $nameScopeFactory; + $this->phpDocNodeTraverser = $phpDocNodeTraverser; + } + public function decorate(PhpDocNode $phpDocNode, PhpNode $phpNode) : void + { + // iterating all phpdocs has big overhead. peek into the phpdoc to exit early + if (\strpos($phpDocNode->__toString(), '::') === \false) { + return; + } + $this->phpDocNodeTraverser->traverseWithCallable($phpDocNode, '', function (Node $node) use($phpNode) : ?\PHPStan\PhpDocParser\Ast\Node { + if (!$node instanceof ArrayItemNode) { + return null; + } + if (!\is_string($node->value)) { + return null; + } + $splitScopeResolution = \explode('::', $node->value); + if (\count($splitScopeResolution) !== 2) { + return null; + } + $firstName = $splitScopeResolution[0]; + $constFetchNode = new ConstFetchNode($firstName, $firstName); + $className = $this->resolveFullyQualifiedClass($constFetchNode, $phpNode); + $node->setAttribute(PhpDocAttributeKey::RESOLVED_CLASS, $className); + return $node; + }); + } + private function resolveFullyQualifiedClass(ConstFetchNode $constFetchNode, PhpNode $phpNode) : string + { + $nameScope = $this->nameScopeFactory->createNameScopeFromNodeWithoutTemplateTypes($phpNode); + return $nameScope->resolveStringName($constFetchNode->className); + } +} diff --git a/src/DependencyInjection/LazyContainerFactory.php b/src/DependencyInjection/LazyContainerFactory.php index 8a7789493a7..c40abd37558 100644 --- a/src/DependencyInjection/LazyContainerFactory.php +++ b/src/DependencyInjection/LazyContainerFactory.php @@ -27,6 +27,7 @@ use Rector\BetterPhpDocParser\PhpDocNodeVisitor\CallableTypePhpDocNodeVisitor; use Rector\BetterPhpDocParser\PhpDocNodeVisitor\IntersectionTypeNodePhpDocNodeVisitor; use Rector\BetterPhpDocParser\PhpDocNodeVisitor\TemplatePhpDocNodeVisitor; use Rector\BetterPhpDocParser\PhpDocNodeVisitor\UnionTypeNodePhpDocNodeVisitor; +use Rector\BetterPhpDocParser\PhpDocParser\ArrayItemClassNameDecorator; use Rector\BetterPhpDocParser\PhpDocParser\BetterPhpDocParser; use Rector\BetterPhpDocParser\PhpDocParser\BetterTypeParser; use Rector\BetterPhpDocParser\PhpDocParser\ConstExprClassNameDecorator; @@ -217,7 +218,7 @@ final class LazyContainerFactory /** * @var array> */ - private const PHP_DOC_NODE_DECORATOR_CLASSES = [ConstExprClassNameDecorator::class, DoctrineAnnotationDecorator::class]; + private const PHP_DOC_NODE_DECORATOR_CLASSES = [ConstExprClassNameDecorator::class, DoctrineAnnotationDecorator::class, ArrayItemClassNameDecorator::class]; /** * @var array */ diff --git a/src/PostRector/Rector/UnusedImportRemovingPostRector.php b/src/PostRector/Rector/UnusedImportRemovingPostRector.php index 62dfa17f15c..8ce52a7ead7 100644 --- a/src/PostRector/Rector/UnusedImportRemovingPostRector.php +++ b/src/PostRector/Rector/UnusedImportRemovingPostRector.php @@ -125,6 +125,8 @@ final class UnusedImportRemovingPostRector extends \Rector\PostRector\Rector\Abs $names = \array_merge($names, $constFetchNodeNames); $genericTagClassNames = $phpDocInfo->getGenericTagClassNames(); $names = \array_merge($names, $genericTagClassNames); + $arrayItemTagClassNames = $phpDocInfo->getArrayItemNodeClassNames(); + $names = \array_merge($names, $arrayItemTagClassNames); } }); return $names; diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index 23c9d0b2954..0c128a5e6d2 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -999,6 +999,7 @@ return array( 'Rector\\BetterPhpDocParser\\PhpDocNodeVisitor\\IntersectionTypeNodePhpDocNodeVisitor' => $baseDir . '/src/BetterPhpDocParser/PhpDocNodeVisitor/IntersectionTypeNodePhpDocNodeVisitor.php', 'Rector\\BetterPhpDocParser\\PhpDocNodeVisitor\\TemplatePhpDocNodeVisitor' => $baseDir . '/src/BetterPhpDocParser/PhpDocNodeVisitor/TemplatePhpDocNodeVisitor.php', 'Rector\\BetterPhpDocParser\\PhpDocNodeVisitor\\UnionTypeNodePhpDocNodeVisitor' => $baseDir . '/src/BetterPhpDocParser/PhpDocNodeVisitor/UnionTypeNodePhpDocNodeVisitor.php', + 'Rector\\BetterPhpDocParser\\PhpDocParser\\ArrayItemClassNameDecorator' => $baseDir . '/src/BetterPhpDocParser/PhpDocParser/ArrayItemClassNameDecorator.php', 'Rector\\BetterPhpDocParser\\PhpDocParser\\BetterPhpDocParser' => $baseDir . '/src/BetterPhpDocParser/PhpDocParser/BetterPhpDocParser.php', 'Rector\\BetterPhpDocParser\\PhpDocParser\\BetterTypeParser' => $baseDir . '/src/BetterPhpDocParser/PhpDocParser/BetterTypeParser.php', 'Rector\\BetterPhpDocParser\\PhpDocParser\\ClassAnnotationMatcher' => $baseDir . '/src/BetterPhpDocParser/PhpDocParser/ClassAnnotationMatcher.php', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index e0de784b7e9..bd04fc0b234 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -1218,6 +1218,7 @@ class ComposerStaticInit2d887a2f87c676eb32b3e04612865e54 'Rector\\BetterPhpDocParser\\PhpDocNodeVisitor\\IntersectionTypeNodePhpDocNodeVisitor' => __DIR__ . '/../..' . '/src/BetterPhpDocParser/PhpDocNodeVisitor/IntersectionTypeNodePhpDocNodeVisitor.php', 'Rector\\BetterPhpDocParser\\PhpDocNodeVisitor\\TemplatePhpDocNodeVisitor' => __DIR__ . '/../..' . '/src/BetterPhpDocParser/PhpDocNodeVisitor/TemplatePhpDocNodeVisitor.php', 'Rector\\BetterPhpDocParser\\PhpDocNodeVisitor\\UnionTypeNodePhpDocNodeVisitor' => __DIR__ . '/../..' . '/src/BetterPhpDocParser/PhpDocNodeVisitor/UnionTypeNodePhpDocNodeVisitor.php', + 'Rector\\BetterPhpDocParser\\PhpDocParser\\ArrayItemClassNameDecorator' => __DIR__ . '/../..' . '/src/BetterPhpDocParser/PhpDocParser/ArrayItemClassNameDecorator.php', 'Rector\\BetterPhpDocParser\\PhpDocParser\\BetterPhpDocParser' => __DIR__ . '/../..' . '/src/BetterPhpDocParser/PhpDocParser/BetterPhpDocParser.php', 'Rector\\BetterPhpDocParser\\PhpDocParser\\BetterTypeParser' => __DIR__ . '/../..' . '/src/BetterPhpDocParser/PhpDocParser/BetterTypeParser.php', 'Rector\\BetterPhpDocParser\\PhpDocParser\\ClassAnnotationMatcher' => __DIR__ . '/../..' . '/src/BetterPhpDocParser/PhpDocParser/ClassAnnotationMatcher.php',