Updated Rector to commit 62f2c30358

62f2c30358 [PostRector][CodingStyle] Improve Auto import performance (#1233)
This commit is contained in:
Tomas Votruba 2021-11-14 09:32:59 +00:00
parent 41dc55f91a
commit 6630ebdb90
12 changed files with 60 additions and 57 deletions

View File

@ -39,18 +39,18 @@ final class UseNodesToAddCollector implements \Rector\PostRector\Contract\Collec
*/
public function addUseImport($objectType) : void
{
/** @var File $file */
$file = $this->currentFileProvider->getFile();
$smartFileInfo = $file->getSmartFileInfo();
$this->useImportTypesInFilePath[$smartFileInfo->getRealPath()][] = $objectType;
$this->useImportTypesInFilePath[$file->getFilePath()][] = $objectType;
}
/**
* @param \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType $fullyQualifiedObjectType
*/
public function addFunctionUseImport($fullyQualifiedObjectType) : void
{
/** @var File $file */
$file = $this->currentFileProvider->getFile();
$smartFileInfo = $file->getSmartFileInfo();
$this->functionUseImportTypesInFilePath[$smartFileInfo->getRealPath()][] = $fullyQualifiedObjectType;
$this->functionUseImportTypesInFilePath[$file->getFilePath()][] = $fullyQualifiedObjectType;
}
/**
* @return AliasedObjectType[]|FullyQualifiedObjectType[]
@ -59,8 +59,7 @@ final class UseNodesToAddCollector implements \Rector\PostRector\Contract\Collec
*/
public function getUseImportTypesByNode($file, $node) : array
{
$fileInfo = $file->getSmartFileInfo();
$filePath = $fileInfo->getRealPath();
$filePath = $file->getFilePath();
$objectTypes = $this->useImportTypesInFilePath[$filePath] ?? [];
/** @var Use_[] $useNodes */
$useNodes = (array) $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::USE_NODES);
@ -96,10 +95,9 @@ final class UseNodesToAddCollector implements \Rector\PostRector\Contract\Collec
*/
public function isShortImported($file, $fullyQualifiedObjectType) : bool
{
$fileInfo = $file->getSmartFileInfo();
$filePath = $fileInfo->getRealPath();
$shortName = $fullyQualifiedObjectType->getShortName();
if ($this->isShortClassImported($file, $shortName)) {
$filePath = $file->getFilePath();
if ($this->isShortClassImported($filePath, $shortName)) {
return \true;
}
$fileFunctionUseImportTypes = $this->functionUseImportTypesInFilePath[$filePath] ?? [];
@ -116,8 +114,7 @@ final class UseNodesToAddCollector implements \Rector\PostRector\Contract\Collec
*/
public function isImportShortable($file, $fullyQualifiedObjectType) : bool
{
$fileInfo = $file->getSmartFileInfo();
$filePath = $fileInfo->getRealPath();
$filePath = $file->getFilePath();
$fileUseImportTypes = $this->useImportTypesInFilePath[$filePath] ?? [];
foreach ($fileUseImportTypes as $fileUseImportType) {
if ($fullyQualifiedObjectType->equals($fileUseImportType)) {
@ -148,11 +145,9 @@ final class UseNodesToAddCollector implements \Rector\PostRector\Contract\Collec
{
return $this->functionUseImportTypesInFilePath[$smartFileInfo->getRealPath()] ?? [];
}
private function isShortClassImported(\Rector\Core\ValueObject\Application\File $file, string $shortName) : bool
private function isShortClassImported(string $filePath, string $shortName) : bool
{
$fileInfo = $file->getSmartFileInfo();
$realPath = $fileInfo->getRealPath();
$fileUseImports = $this->useImportTypesInFilePath[$realPath] ?? [];
$fileUseImports = $this->useImportTypesInFilePath[$filePath] ?? [];
foreach ($fileUseImports as $fileUseImport) {
if ($fileUseImport->getShortName() === $shortName) {
return \true;

View File

@ -69,21 +69,18 @@ final class NameImportingPostRector extends \Rector\PostRector\Rector\AbstractPo
return null;
}
$file = $this->currentFileProvider->getFile();
if (!$file instanceof \Rector\Core\ValueObject\Application\File) {
return null;
}
if (!$this->shouldApply($file)) {
return null;
}
if ($node instanceof \PhpParser\Node\Name) {
if (!$file instanceof \Rector\Core\ValueObject\Application\File) {
return null;
}
if (!$this->shouldApply($file)) {
return null;
}
return $this->processNodeName($node, $file);
}
if (!$this->parameterProvider->provideBoolParameter(\Rector\Core\Configuration\Option::IMPORT_DOC_BLOCKS)) {
return null;
}
if ($file instanceof \Rector\Core\ValueObject\Application\File && !$this->shouldApply($file)) {
return null;
}
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$this->docBlockNameImporter->importNames($phpDocInfo->getPhpDocNode(), $node);
return $node;

View File

@ -24,10 +24,11 @@ final class FullyQualifiedObjectType extends \PHPStan\Type\ObjectType
}
public function getShortName() : string
{
if (\strpos($this->getClassName(), '\\') === \false) {
return $this->getClassName();
$className = $this->getClassName();
if (\strpos($className, '\\') === \false) {
return $className;
}
return (string) \RectorPrefix20211114\Nette\Utils\Strings::after($this->getClassName(), '\\', -1);
return (string) \RectorPrefix20211114\Nette\Utils\Strings::after($className, '\\', -1);
}
public function getShortNameNode() : \PhpParser\Node\Name
{

View File

@ -35,10 +35,11 @@ final class AliasClassNameImportSkipVoter implements \Rector\CodingStyle\Contrac
public function shouldSkip($file, $fullyQualifiedObjectType, $node) : bool
{
$aliasedUses = $this->aliasUsesResolver->resolveFromNode($node);
$shortNameLowered = $fullyQualifiedObjectType->getShortNameLowered();
foreach ($aliasedUses as $aliasedUse) {
$aliasedUseLowered = \strtolower($aliasedUse);
// its aliased, we cannot just rename it
if (\substr_compare($aliasedUseLowered, '\\' . $fullyQualifiedObjectType->getShortNameLowered(), -\strlen('\\' . $fullyQualifiedObjectType->getShortNameLowered())) === 0) {
if (\substr_compare($aliasedUseLowered, '\\' . $shortNameLowered, -\strlen('\\' . $shortNameLowered)) === 0) {
return \true;
}
}

View File

@ -36,9 +36,10 @@ final class FullyQualifiedNameClassNameImportSkipVoter implements \Rector\Coding
{
// "new X" or "X::static()"
$shortNamesToFullyQualifiedNames = $this->shortNameResolver->resolveFromFile($file);
$loweredShortNameFullyQualified = $fullyQualifiedObjectType->getShortNameLowered();
foreach ($shortNamesToFullyQualifiedNames as $shortName => $fullyQualifiedName) {
$shortNameLowered = \strtolower($shortName);
if ($fullyQualifiedObjectType->getShortNameLowered() !== $shortNameLowered) {
if ($loweredShortNameFullyQualified !== $shortNameLowered) {
continue;
}
return $fullyQualifiedObjectType->getClassNameLowered() !== \strtolower($fullyQualifiedName);

View File

@ -54,9 +54,10 @@ final class ClassNameImportSkipper
*/
public function isAlreadyImported(\PhpParser\Node\Name $name, array $uses) : bool
{
$stringName = $name->toString();
foreach ($uses as $use) {
foreach ($use->uses as $useUse) {
if ($useUse->name->toString() === $name->toString()) {
if ($useUse->name->toString() === $stringName) {
return \true;
}
}
@ -68,6 +69,7 @@ final class ClassNameImportSkipper
*/
public function isFoundInUse(\PhpParser\Node\Name $name, array $uses) : bool
{
$stringName = $name->toString();
$nameLastName = \strtolower($name->getLast());
foreach ($uses as $use) {
foreach ($use->uses as $useUse) {
@ -75,7 +77,7 @@ final class ClassNameImportSkipper
if ($useUseLastName !== $nameLastName) {
continue;
}
if ($this->isJustRenamedClass($name, $useUse)) {
if ($this->isJustRenamedClass($stringName, $useUse)) {
continue;
}
return \true;
@ -83,15 +85,16 @@ final class ClassNameImportSkipper
}
return \false;
}
private function isJustRenamedClass(\PhpParser\Node\Name $name, \PhpParser\Node\Stmt\UseUse $useUse) : bool
private function isJustRenamedClass(string $stringName, \PhpParser\Node\Stmt\UseUse $useUse) : bool
{
$useUseNameString = $useUse->name->toString();
// is in renamed classes? skip it
foreach ($this->renamedClassesDataCollector->getOldToNewClasses() as $oldClass => $newClass) {
// is class being renamed in use imports?
if ($name->toString() !== $newClass) {
if ($stringName !== $newClass) {
continue;
}
if ($useUse->name->toString() !== $oldClass) {
if ($useUseNameString !== $oldClass) {
continue;
}
return \true;

View File

@ -80,8 +80,10 @@ final class NameImporter
if (!$staticType instanceof \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType) {
return null;
}
$this->aliasedUses = $this->aliasUsesResolver->resolveFromStmts($uses);
return $this->importNameAndCollectNewUseStatement($file, $name, $staticType);
$className = $staticType->getClassName();
// class has \, no need to search in aliases, mark aliasedUses as empty
$this->aliasedUses = \strpos($className, '\\') !== \false ? [] : $this->aliasUsesResolver->resolveFromStmts($uses);
return $this->importNameAndCollectNewUseStatement($file, $name, $staticType, $className);
}
private function shouldSkipName(\PhpParser\Node\Name $name) : bool
{
@ -110,7 +112,7 @@ final class NameImporter
}
return \false;
}
private function importNameAndCollectNewUseStatement(\Rector\Core\ValueObject\Application\File $file, \PhpParser\Node\Name $name, \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType $fullyQualifiedObjectType) : ?\PhpParser\Node\Name
private function importNameAndCollectNewUseStatement(\Rector\Core\ValueObject\Application\File $file, \PhpParser\Node\Name $name, \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType $fullyQualifiedObjectType, string $className) : ?\PhpParser\Node\Name
{
// the same end is already imported → skip
if ($this->classNameImportSkipper->shouldSkipNameForFullyQualifiedObjectType($file, $name, $fullyQualifiedObjectType)) {
@ -123,9 +125,12 @@ final class NameImporter
return null;
}
$this->addUseImport($file, $name, $fullyQualifiedObjectType);
if ($this->aliasedUses === []) {
return $fullyQualifiedObjectType->getShortNameNode();
}
// possibly aliased
foreach ($this->aliasedUses as $aliasedUse) {
if ($fullyQualifiedObjectType->getClassName() === $aliasedUse) {
if ($className === $aliasedUse) {
return null;
}
}

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = '8046b81dd24ea4b2624f54886892185133416fb7';
public const PACKAGE_VERSION = '62f2c303585f9e4c6467d66c6a333bafce26b613';
/**
* @var string
*/
public const RELEASE_DATE = '2021-11-14 11:34:50';
public const RELEASE_DATE = '2021-11-14 16:18:50';
public static function resolvePackageVersion() : string
{
$process = new \RectorPrefix20211114\Symfony\Component\Process\Process(['git', 'log', '--pretty="%H"', '-n1', 'HEAD'], __DIR__);

2
vendor/autoload.php vendored
View File

@ -4,4 +4,4 @@
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit4417226e0e1da0bec63d770dcec6217c::getLoader();
return ComposerAutoloaderInitfdda04122218d22f3d1286c001e6865b::getLoader();

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit4417226e0e1da0bec63d770dcec6217c
class ComposerAutoloaderInitfdda04122218d22f3d1286c001e6865b
{
private static $loader;
@ -22,15 +22,15 @@ class ComposerAutoloaderInit4417226e0e1da0bec63d770dcec6217c
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit4417226e0e1da0bec63d770dcec6217c', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInitfdda04122218d22f3d1286c001e6865b', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(\dirname(__FILE__)));
spl_autoload_unregister(array('ComposerAutoloaderInit4417226e0e1da0bec63d770dcec6217c', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInitfdda04122218d22f3d1286c001e6865b', 'loadClassLoader'));
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) {
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit4417226e0e1da0bec63d770dcec6217c::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInitfdda04122218d22f3d1286c001e6865b::getInitializer($loader));
} else {
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
@ -42,19 +42,19 @@ class ComposerAutoloaderInit4417226e0e1da0bec63d770dcec6217c
$loader->register(true);
if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInit4417226e0e1da0bec63d770dcec6217c::$files;
$includeFiles = Composer\Autoload\ComposerStaticInitfdda04122218d22f3d1286c001e6865b::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire4417226e0e1da0bec63d770dcec6217c($fileIdentifier, $file);
composerRequirefdda04122218d22f3d1286c001e6865b($fileIdentifier, $file);
}
return $loader;
}
}
function composerRequire4417226e0e1da0bec63d770dcec6217c($fileIdentifier, $file)
function composerRequirefdda04122218d22f3d1286c001e6865b($fileIdentifier, $file)
{
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
require $file;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit4417226e0e1da0bec63d770dcec6217c
class ComposerStaticInitfdda04122218d22f3d1286c001e6865b
{
public static $files = array (
'a4a119a56e50fbb293281d9a48007e0e' => __DIR__ . '/..' . '/symfony/polyfill-php80/bootstrap.php',
@ -3542,9 +3542,9 @@ class ComposerStaticInit4417226e0e1da0bec63d770dcec6217c
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit4417226e0e1da0bec63d770dcec6217c::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit4417226e0e1da0bec63d770dcec6217c::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit4417226e0e1da0bec63d770dcec6217c::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInitfdda04122218d22f3d1286c001e6865b::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitfdda04122218d22f3d1286c001e6865b::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitfdda04122218d22f3d1286c001e6865b::$classMap;
}, null, ClassLoader::class);
}

View File

@ -12,8 +12,8 @@ if (!class_exists('GenerateChangelogCommand', false) && !interface_exists('Gener
if (!class_exists('AutoloadIncluder', false) && !interface_exists('AutoloadIncluder', false) && !trait_exists('AutoloadIncluder', false)) {
spl_autoload_call('RectorPrefix20211114\AutoloadIncluder');
}
if (!class_exists('ComposerAutoloaderInit4417226e0e1da0bec63d770dcec6217c', false) && !interface_exists('ComposerAutoloaderInit4417226e0e1da0bec63d770dcec6217c', false) && !trait_exists('ComposerAutoloaderInit4417226e0e1da0bec63d770dcec6217c', false)) {
spl_autoload_call('RectorPrefix20211114\ComposerAutoloaderInit4417226e0e1da0bec63d770dcec6217c');
if (!class_exists('ComposerAutoloaderInitfdda04122218d22f3d1286c001e6865b', false) && !interface_exists('ComposerAutoloaderInitfdda04122218d22f3d1286c001e6865b', false) && !trait_exists('ComposerAutoloaderInitfdda04122218d22f3d1286c001e6865b', false)) {
spl_autoload_call('RectorPrefix20211114\ComposerAutoloaderInitfdda04122218d22f3d1286c001e6865b');
}
if (!class_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !interface_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false) && !trait_exists('Helmich\TypoScriptParser\Parser\AST\Statement', false)) {
spl_autoload_call('RectorPrefix20211114\Helmich\TypoScriptParser\Parser\AST\Statement');
@ -3309,9 +3309,9 @@ if (!function_exists('print_node')) {
return \RectorPrefix20211114\print_node(...func_get_args());
}
}
if (!function_exists('composerRequire4417226e0e1da0bec63d770dcec6217c')) {
function composerRequire4417226e0e1da0bec63d770dcec6217c() {
return \RectorPrefix20211114\composerRequire4417226e0e1da0bec63d770dcec6217c(...func_get_args());
if (!function_exists('composerRequirefdda04122218d22f3d1286c001e6865b')) {
function composerRequirefdda04122218d22f3d1286c001e6865b() {
return \RectorPrefix20211114\composerRequirefdda04122218d22f3d1286c001e6865b(...func_get_args());
}
}
if (!function_exists('parseArgs')) {