Skip to content

Commit

Permalink
[NodeTypeResolver] Skip Auto import from doc in used namespace on Nam…
Browse files Browse the repository at this point in the history
…eImportingPhpDocNodeVisitor (#912)

* failing test for 6693

* Fixes rectorphp/rector#6693

* debug

* Fixed 🎉

* clean up

* clean up

* handle first part

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* Fixed 🎉

* clean up

* eol

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Sep 22, 2021
1 parent 6ee3817 commit 5ff4058
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
use Symplify\PackageBuilder\Reflection\ClassLikeExistenceChecker;
use Symplify\SimplePhpDocParser\PhpDocNodeVisitor\AbstractPhpDocNodeVisitor;

final class NameImportingPhpDocNodeVisitor extends AbstractPhpDocNodeVisitor
Expand All @@ -32,7 +33,8 @@ public function __construct(
private ParameterProvider $parameterProvider,
private ClassNameImportSkipper $classNameImportSkipper,
private UseNodesToAddCollector $useNodesToAddCollector,
private CurrentFileProvider $currentFileProvider
private CurrentFileProvider $currentFileProvider,
private ClassLikeExistenceChecker $classLikeExistenceChecker
) {
}

Expand Down Expand Up @@ -112,15 +114,15 @@ private function processFqnNameImport(
return null;
}

if ($newNode->name !== $identifierTypeNode->name) {
if ($this->shouldImport($newNode, $identifierTypeNode, $fullyQualifiedObjectType)) {
$this->useNodesToAddCollector->addUseImport($fullyQualifiedObjectType);
return $newNode;
}

return null;
}

if ($newNode->name !== $identifierTypeNode->name) {
if ($this->shouldImport($newNode, $identifierTypeNode, $fullyQualifiedObjectType)) {
// do not import twice
if ($this->useNodesToAddCollector->isShortImported($file, $fullyQualifiedObjectType)) {
return null;
Expand All @@ -133,6 +135,20 @@ private function processFqnNameImport(
return null;
}

private function shouldImport(
IdentifierTypeNode $newNode,
IdentifierTypeNode $identifierTypeNode,
FullyQualifiedObjectType $fullyQualifiedObjectType
): bool
{
if ($newNode->name === $identifierTypeNode->name) {
return false;
}

$className = $fullyQualifiedObjectType->getClassName();
return $this->classLikeExistenceChecker->doesClassLikeInsensitiveExists($className);
}

private function shouldSkipShortClassName(FullyQualifiedObjectType $fullyQualifiedObjectType): bool
{
$importShortClasses = $this->parameterProvider->provideBoolParameter(Option::IMPORT_SHORT_CLASSES);
Expand Down
33 changes: 33 additions & 0 deletions tests/Issues/AutoImportDocInUse/AutoImportDocInUseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\AutoImportDocInUse;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class AutoImportDocInUseTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
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,29 @@
<?php

declare(strict_types=1);

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

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Rector\Core\Tests\Issues\AutoImportDocInUse\Source\Annotation;

final class SkipAutoImportDocInUseNamespace extends AbstractController
{
/**
* @Annotation\SomeEnum()
*
* @return Response
*/
#[Route('/main', name: 'main')]
public function index(): Response
{
return $this->json([
'message' => 'Welcome to your new controller!',
'path' => 'src/Controller/MainController.php',
]);
}
}

?>
16 changes: 16 additions & 0 deletions tests/Issues/AutoImportDocInUse/Source/Annotation/SomeEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\AutoImportDocInUse\Source\Annotation;

use Doctrine\Common\Annotations\Annotation\Target;

/**
* @Annotation
* @Target({"METHOD"})
*/
class SomeEnum
{

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

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::AUTO_IMPORT_NAMES, true);
};

0 comments on commit 5ff4058

Please sign in to comment.