Skip to content

Commit

Permalink
[AutoImport] Allow auto import to use existing alias in use statement (
Browse files Browse the repository at this point in the history
…#2937)

* [AutoImport] Allow auto import to use existing alias in use statement

* implemented 🎉

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* handle has other same use statment without alias

* fix

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* fix

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Sep 18, 2022
1 parent 4923a14 commit bad6a29
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 3 deletions.
38 changes: 37 additions & 1 deletion packages/PostRector/Rector/NameImportingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace Rector\PostRector\Rector;

use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\Stmt\UseUse;
use PHPStan\Reflection\ReflectionProvider;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\CodingStyle\ClassNameImport\ClassNameImportSkipper;
Expand All @@ -16,6 +18,8 @@
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\ValueObject\Application\File;
use Rector\Naming\Naming\AliasNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockNameImporter;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -30,7 +34,8 @@ public function __construct(
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly ReflectionProvider $reflectionProvider,
private readonly CurrentFileProvider $currentFileProvider,
private readonly BetterNodeFinder $betterNodeFinder
private readonly BetterNodeFinder $betterNodeFinder,
private readonly AliasNameResolver $aliasNameResolver
) {
}

Expand Down Expand Up @@ -105,12 +110,43 @@ private function processNodeName(Name $name, File $file): ?Node
$currentUses = $this->betterNodeFinder->findInstanceOf($file->getNewStmts(), Use_::class);

if ($this->shouldImportName($name, $currentUses)) {
$aliasName = $this->aliasNameResolver->resolveByName($name);
$node = $name->getAttribute(AttributeKey::PARENT_NODE);

if (is_string($aliasName) && ! $node instanceof UseUse && ! $this->hasOtherSameUseStatementWithoutAlias(
$name,
$currentUses
)) {
return new Name($aliasName);
}

return $this->nameImporter->importName($name, $file, $currentUses);
}

return null;
}

/**
* @param Use_[] $currentUses
*/
private function hasOtherSameUseStatementWithoutAlias(Name $name, array $currentUses): bool
{
$currentName = $name->toString();
foreach ($currentUses as $currentUse) {
foreach ($currentUse->uses as $useUse) {
if ($useUse->alias instanceof Identifier) {
continue;
}

if ($useUse->name->toString() === $currentName) {
return true;
}
}
}

return false;
}

/**
* @param Use_[] $currentUses
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private function processType(ClassMethod | Function_ $node, Type $inferedType):
return null;
}

/** @var Name|NullableType|PhpParserUnionType $inferredReturnNode */
/** @var Name|NullableType|PhpParserUnionType|IntersectionType $inferredReturnNode */
$this->addReturnType($node, $inferredReturnNode);
$this->nonInformativeReturnTagRemover->removeReturnTagIfNotUseful($node);

Expand Down Expand Up @@ -206,7 +206,7 @@ private function shouldSkipExistingReturnType(ClassMethod | Function_ $functionL

private function addReturnType(
ClassMethod | Function_ $functionLike,
Name|NullableType|\PhpParser\Node\UnionType|IntersectionType $inferredReturnNode
Name|NullableType|PhpParserUnionType|IntersectionType $inferredReturnNode
): void {
if ($functionLike->returnType === null) {
$functionLike->returnType = $inferredReturnNode;
Expand Down
32 changes: 32 additions & 0 deletions tests/Issues/AutoImportInAlias/AutoImportInAliasTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\AutoImportInAlias;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class AutoImportInAliasTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

/**
* @return Iterator<array<string>>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Rector\Core\Tests\Issues\AutoImportDocInUse\Fixture;

use stdClass as SomeObject;

final class AutoImportInAlias extends \stdClass
{
}

?>
-----
<?php

namespace Rector\Core\Tests\Issues\AutoImportDocInUse\Fixture;

use stdClass as SomeObject;

final class AutoImportInAlias extends SomeObject
{
}

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

declare(strict_types=1);

use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->importNames();
};

0 comments on commit bad6a29

Please sign in to comment.