Skip to content

Commit

Permalink
Updated Rector to commit 62f2c30
Browse files Browse the repository at this point in the history
rectorphp/rector-src@62f2c30 [PostRector][CodingStyle] Improve Auto import performance (#1233)
  • Loading branch information
TomasVotruba committed Nov 14, 2021
1 parent 41dc55f commit 6630ebd
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 57 deletions.
25 changes: 10 additions & 15 deletions packages/PostRector/Collector/UseNodesToAddCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ public function isActive() : bool
*/
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[]
Expand All @@ -59,8 +59,7 @@ public function addFunctionUseImport($fullyQualifiedObjectType) : void
*/
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);
Expand Down Expand Up @@ -96,10 +95,9 @@ public function hasImport($file, $node, $fullyQualifiedObjectType) : bool
*/
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] ?? [];
Expand All @@ -116,8 +114,7 @@ public function isShortImported($file, $fullyQualifiedObjectType) : bool
*/
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)) {
Expand Down Expand Up @@ -148,11 +145,9 @@ public function getFunctionImportsByFileInfo($smartFileInfo) : array
{
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;
Expand Down
15 changes: 6 additions & 9 deletions packages/PostRector/Rector/NameImportingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,18 @@ public function enterNode(\PhpParser\Node $node) : ?\PhpParser\Node
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ public function areShortNamesEqual($comparedObjectType) : bool
}
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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ public function __construct(\Rector\CodingStyle\ClassNameImport\AliasUsesResolve
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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ public function shouldSkip($file, $fullyQualifiedObjectType, $node) : bool
{
// "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);
Expand Down
13 changes: 8 additions & 5 deletions rules/CodingStyle/ClassNameImport/ClassNameImportSkipper.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ public function isShortNameInUseStatement(\PhpParser\Node\Name $name, array $exi
*/
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;
}
}
Expand All @@ -68,30 +69,32 @@ public function isAlreadyImported(\PhpParser\Node\Name $name, array $uses) : boo
*/
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) {
$useUseLastName = \strtolower($useUse->name->getLast());
if ($useUseLastName !== $nameLastName) {
continue;
}
if ($this->isJustRenamedClass($name, $useUse)) {
if ($this->isJustRenamedClass($stringName, $useUse)) {
continue;
}
return \true;
}
}
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;
Expand Down
13 changes: 9 additions & 4 deletions rules/CodingStyle/Node/NameImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ public function importName(\PhpParser\Node\Name $name, \Rector\Core\ValueObject\
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
{
Expand Down Expand Up @@ -110,7 +112,7 @@ private function shouldSkipName(\PhpParser\Node\Name $name) : bool
}
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)) {
Expand All @@ -123,9 +125,12 @@ private function importNameAndCollectNewUseStatement(\Rector\Core\ValueObject\Ap
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;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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__);
Expand Down
2 changes: 1 addition & 1 deletion vendor/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

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

return ComposerAutoloaderInit4417226e0e1da0bec63d770dcec6217c::getLoader();
return ComposerAutoloaderInitfdda04122218d22f3d1286c001e6865b::getLoader();
14 changes: 7 additions & 7 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 ComposerAutoloaderInit4417226e0e1da0bec63d770dcec6217c
class ComposerAutoloaderInitfdda04122218d22f3d1286c001e6865b
{
private static $loader;

Expand All @@ -22,15 +22,15 @@ public static function getLoader()
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) {
Expand All @@ -42,19 +42,19 @@ public static function getLoader()
$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;
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 ComposerStaticInit4417226e0e1da0bec63d770dcec6217c
class ComposerStaticInitfdda04122218d22f3d1286c001e6865b
{
public static $files = array (
'a4a119a56e50fbb293281d9a48007e0e' => __DIR__ . '/..' . '/symfony/polyfill-php80/bootstrap.php',
Expand Down Expand Up @@ -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);
}
Expand Down
10 changes: 5 additions & 5 deletions vendor/scoper-autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
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');
Expand Down Expand Up @@ -3309,9 +3309,9 @@ function 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')) {
Expand Down

0 comments on commit 6630ebd

Please sign in to comment.