Skip to content

Commit

Permalink
[AutoImport] Allow auto import same namespace with sub use with docbl…
Browse files Browse the repository at this point in the history
…ock short name (#5763)

* [AutoImport] Allow auto import same namespace with sub use with docblock short name

* more fixtures

* Fix non-docblock name

* [ci-review] Rector Rectify

* docblock is require FullyQualifiedObject due to generic

* fix

* fix comment

* fix

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user authored Mar 23, 2024
1 parent c45f0e2 commit e3e741e
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use Rector\CodingStyle\ClassNameImport\ShortNameResolver;
use Rector\CodingStyle\Contract\ClassNameImport\ClassNameImportSkipVoterInterface;
use Rector\Configuration\RenamedClassesDataCollector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Rector\ValueObject\Application\File;

Expand Down Expand Up @@ -41,14 +38,6 @@ public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedO
$className = $fullyQualifiedObjectType->getClassName();
$removedUses = $this->renamedClassesDataCollector->getOldClasses();

$originalName = $node->getAttribute(AttributeKey::ORIGINAL_NAME);
$originalNameToAttribute = null;
if ($originalName instanceof Name && ! $originalName instanceof FullyQualified && $originalName->hasAttribute(
AttributeKey::PHP_ATTRIBUTE_NAME
)) {
$originalNameToAttribute = $originalName->getAttribute(AttributeKey::PHP_ATTRIBUTE_NAME);
}

foreach ($shortNamesToFullyQualifiedNames as $shortName => $fullyQualifiedName) {
if ($fullyQualifiedObjectTypeShortName !== $shortName) {
$shortName = $this->cleanShortName($shortName);
Expand All @@ -63,11 +52,7 @@ public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedO
return false;
}

if (! in_array($fullyQualifiedName, $removedUses, true)) {
return $originalNameToAttribute == null || ! in_array($originalNameToAttribute, $removedUses, true);
}

return false;
return ! in_array($fullyQualifiedName, $removedUses, true);
}

return false;
Expand Down
6 changes: 4 additions & 2 deletions rules/CodingStyle/ClassNameImport/ShortNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,11 @@ private function fqnizeShortNames(array $shortNames, array $stmts): array
foreach ($shortNames as $shortName) {
$stmtsMatchedName = $this->useImportNameMatcher->matchNameWithStmts($shortName, $stmts);

$fullyQualifiedName = is_string($stmtsMatchedName) ? $stmtsMatchedName : $shortName;
if ($stmtsMatchedName == null) {
continue;
}

$shortNamesToFullyQualifiedNames[$shortName] = $fullyQualifiedName;
$shortNamesToFullyQualifiedNames[$shortName] = $stmtsMatchedName;
}

return $shortNamesToFullyQualifiedNames;
Expand Down
7 changes: 1 addition & 6 deletions src/PostRector/Rector/ClassRenamingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,11 @@ private function resolveResultWithPhpAttributeName(
): ?FullyQualified {
$phpAttributeName = $name->getAttribute(AttributeKey::PHP_ATTRIBUTE_NAME);
if (is_string($phpAttributeName)) {
$result = $this->classRenamer->renameNode(
return $this->classRenamer->renameNode(
new FullyQualified($phpAttributeName, $name->getAttributes()),
$oldToNewClasses,
$scope
);
if ($result instanceof FullyQualified) {
$result->setAttribute(AttributeKey::ORIGINAL_NAME, $name);
}

return $result;
}

return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App;

/**
* @param \stdClass|\Rector\Tests\Issues\AutoImport\Source\stdClass $param
*/
function unionEqualLastNameClassExists($param)
{
}

?>
-----
<?php

namespace App;

use stdClass;
/**
* @param stdClass|\Rector\Tests\Issues\AutoImport\Source\stdClass $param
*/
function unionEqualLastNameClassExists($param)
{
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Database;

use Config\Database;

/**
* @param Forge $forge
*/
function same_namespace_with_sub_use(\App\Database\Forge $forge)
{

}

?>
-----
<?php

namespace App\Database;

use Config\Database;

/**
* @param Forge $forge
*/
function same_namespace_with_sub_use(Forge $forge)
{

}

?>
9 changes: 9 additions & 0 deletions tests/Issues/AutoImport/Source/stdClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Issues\AutoImport\Source;

class stdClass
{
}

0 comments on commit e3e741e

Please sign in to comment.