Skip to content

Commit

Permalink
[PostRector][CodingStyle] Improve Auto import performance (#1233)
Browse files Browse the repository at this point in the history
* [PostRector][CodingStyle] Improve Auto import performance

* move method call  to before loop in NameImporter importNameAndCollectNewUseStatement

* move get short name before loop on AliasClassNameImportSkipVoter

* move get short name before loop on FullyQualifiedNameClassNameImportSkipVoter

* reduce long variable

* [ci-review] Rector Rectify

* call on return is ok to direct method call

* reduce repetitive call getClassName

* move early check File and shouldApply

* debug

* alias of namespace never has backslash, return early when classname has backslash

* fix

* class has backslash, no need to search in aliases, mark aliasedUses as empty

* early return when aliasedUses is empty

* get real path can be from File object

* move variable to near declaration

* reuse variable

* phpstan

Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
samsonasik and actions-user authored Nov 14, 2021
1 parent 8046b81 commit 62f2c30
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 45 deletions.
30 changes: 11 additions & 19 deletions packages/PostRector/Collector/UseNodesToAddCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,26 @@ public function isActive(): bool

public function addUseImport(FullyQualifiedObjectType | AliasedObjectType $objectType): void
{
/** @var File $file */
$file = $this->currentFileProvider->getFile();
$smartFileInfo = $file->getSmartFileInfo();

$this->useImportTypesInFilePath[$smartFileInfo->getRealPath()][] = $objectType;
$this->useImportTypesInFilePath[$file->getFilePath()][] = $objectType;
}

public function addFunctionUseImport(FullyQualifiedObjectType $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[]
*/
public function getUseImportTypesByNode(File $file, Node $node): array
{
$fileInfo = $file->getSmartFileInfo();
$filePath = $fileInfo->getRealPath();

$filePath = $file->getFilePath();
$objectTypes = $this->useImportTypesInFilePath[$filePath] ?? [];

/** @var Use_[] $useNodes */
Expand Down Expand Up @@ -92,16 +90,15 @@ public function hasImport(File $file, Node $node, FullyQualifiedObjectType $full

public function isShortImported(File $file, FullyQualifiedObjectType $fullyQualifiedObjectType): bool
{
$fileInfo = $file->getSmartFileInfo();
$filePath = $fileInfo->getRealPath();

$shortName = $fullyQualifiedObjectType->getShortName();
$filePath = $file->getFilePath();

if ($this->isShortClassImported($file, $shortName)) {
if ($this->isShortClassImported($filePath, $shortName)) {
return true;
}

$fileFunctionUseImportTypes = $this->functionUseImportTypesInFilePath[$filePath] ?? [];

foreach ($fileFunctionUseImportTypes as $fileFunctionUseImportType) {
if ($fileFunctionUseImportType->getShortName() === $shortName) {
return true;
Expand All @@ -113,9 +110,7 @@ public function isShortImported(File $file, FullyQualifiedObjectType $fullyQuali

public function isImportShortable(File $file, FullyQualifiedObjectType $fullyQualifiedObjectType): bool
{
$fileInfo = $file->getSmartFileInfo();
$filePath = $fileInfo->getRealPath();

$filePath = $file->getFilePath();
$fileUseImportTypes = $this->useImportTypesInFilePath[$filePath] ?? [];

foreach ($fileUseImportTypes as $fileUseImportType) {
Expand Down Expand Up @@ -150,12 +145,9 @@ public function getFunctionImportsByFileInfo(SmartFileInfo $smartFileInfo): arra
return $this->functionUseImportTypesInFilePath[$smartFileInfo->getRealPath()] ?? [];
}

private function isShortClassImported(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) {
Expand Down
19 changes: 7 additions & 12 deletions packages/PostRector/Rector/NameImportingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,22 @@ public function enterNode(Node $node): ?Node
}

$file = $this->currentFileProvider->getFile();
if (! $file instanceof File) {
return null;
}

if ($node instanceof Name) {
if (! $file instanceof File) {
return null;
}

if (! $this->shouldApply($file)) {
return null;
}
if (! $this->shouldApply($file)) {
return null;
}

if ($node instanceof Name) {
return $this->processNodeName($node, $file);
}

if (! $this->parameterProvider->provideBoolParameter(Option::IMPORT_DOC_BLOCKS)) {
return null;
}

if ($file instanceof File && ! $this->shouldApply($file)) {
return null;
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$this->docBlockNameImporter->importNames($phpDocInfo->getPhpDocNode(), $node);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ public function areShortNamesEqual(

public function getShortName(): string
{
if (! \str_contains($this->getClassName(), '\\')) {
return $this->getClassName();
$className = $this->getClassName();
if (! \str_contains($className, '\\')) {
return $className;
}

return (string) Strings::after($this->getClassName(), '\\', -1);
return (string) Strings::after($className, '\\', -1);
}

public function getShortNameNode(): Name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ public function __construct(
public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedObjectType, Node $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 (\str_ends_with($aliasedUseLowered, '\\' . $fullyQualifiedObjectType->getShortNameLowered())) {
if (\str_ends_with($aliasedUseLowered, '\\' . $shortNameLowered)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedO
{
// "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;
}

Expand Down
16 changes: 11 additions & 5 deletions rules/CodingStyle/ClassNameImport/ClassNameImportSkipper.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ public function isShortNameInUseStatement(Name $name, array $existingUses): bool
*/
public function isAlreadyImported(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 @@ -72,7 +74,9 @@ public function isAlreadyImported(Name $name, array $uses): bool
*/
public function isFoundInUse(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());
Expand All @@ -81,7 +85,7 @@ public function isFoundInUse(Name $name, array $uses): bool
continue;
}

if ($this->isJustRenamedClass($name, $useUse)) {
if ($this->isJustRenamedClass($stringName, $useUse)) {
continue;
}

Expand All @@ -92,16 +96,18 @@ public function isFoundInUse(Name $name, array $uses): bool
return false;
}

private function isJustRenamedClass(Name $name, UseUse $useUse): bool
private function isJustRenamedClass(string $stringName, 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;
}

Expand Down
17 changes: 13 additions & 4 deletions rules/CodingStyle/Node/NameImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,13 @@ public function importName(Name $name, File $file, array $uses): ?Name
return null;
}

$this->aliasedUses = $this->aliasUsesResolver->resolveFromStmts($uses);
$className = $staticType->getClassName();
// class has \, no need to search in aliases, mark aliasedUses as empty
$this->aliasedUses = str_contains($className, '\\')
? []
: $this->aliasUsesResolver->resolveFromStmts($uses);

return $this->importNameAndCollectNewUseStatement($file, $name, $staticType);
return $this->importNameAndCollectNewUseStatement($file, $name, $staticType, $className);
}

private function shouldSkipName(Name $name): bool
Expand Down Expand Up @@ -100,7 +104,8 @@ private function shouldSkipName(Name $name): bool
private function importNameAndCollectNewUseStatement(
File $file,
Name $name,
FullyQualifiedObjectType $fullyQualifiedObjectType
FullyQualifiedObjectType $fullyQualifiedObjectType,
string $className
): ?Name {
// the same end is already imported → skip
if ($this->classNameImportSkipper->shouldSkipNameForFullyQualifiedObjectType(
Expand All @@ -121,9 +126,13 @@ private function importNameAndCollectNewUseStatement(

$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

0 comments on commit 62f2c30

Please sign in to comment.