Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AutoImport] Allow auto import just renamed @var only docblock on RenameClassRector without removeUnusedImports() enabled #5220

Merged
merged 10 commits into from
Nov 3, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\Core\Configuration\Option;
use Rector\Core\Configuration\Parameter\SimpleParameterProvider;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Naming\Naming\UseImportsResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -78,14 +76,6 @@ public function enterNode(Node $node): ?Node
$identifier->name = $this->resolveNamespacedName($identifier, $currentPhpNode, $node->name);
$staticType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($identifier, $currentPhpNode);

$shouldImport = SimpleParameterProvider::provideBoolParameter(Option::AUTO_IMPORT_NAMES);
$isNoNamespacedName = ! str_starts_with($identifier->name, '\\') && substr_count($identifier->name, '\\') === 0;

// tweak overlapped import + rename
if ($shouldImport && $isNoNamespacedName) {
return null;
}

// make sure to compare FQNs
$objectType = $this->expandShortenedObjectType($staticType);
foreach ($this->oldToNewTypes as $oldToNewType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Rector\Tests\Renaming\Rector\Name\RenameClassRector\FixtureAutoImportNamesWithoutRemoveUnusedImport;

use Interop\Container\ContainerInterface;

/** @var ContainerInterface */
$container->get('Foo');

?>
-----
<?php

namespace Rector\Tests\Renaming\Rector\Name\RenameClassRector\FixtureAutoImportNamesWithoutRemoveUnusedImport;

use Psr\Container\ContainerInterface;

/** @var ContainerInterface */
$container->get('Foo');

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

namespace Rector\Tests\Renaming\Rector\Name\RenameClassRector\FixtureAutoImportNamesWithoutRemoveUnusedImport;

use Interop\Container\ContainerInterface;

/** @var ContainerInterface */
if ($container instanceof ContainerInterface) {
$container->get('Foo');
}

?>
-----
<?php

namespace Rector\Tests\Renaming\Rector\Name\RenameClassRector\FixtureAutoImportNamesWithoutRemoveUnusedImport;

use Psr\Container\ContainerInterface;

/** @var ContainerInterface */
if ($container instanceof ContainerInterface) {
$container->get('Foo');
}

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

namespace Rector\Tests\Renaming\Rector\Name\RenameClassRector\FixtureAutoImportNamesWithoutRemoveUnusedImport;

use Interop\Container\ContainerInterface;

if ($container instanceof ContainerInterface) {
/** @var ContainerInterface */
$container->get('Foo');
}

?>
-----
<?php

namespace Rector\Tests\Renaming\Rector\Name\RenameClassRector\FixtureAutoImportNamesWithoutRemoveUnusedImport;

use Psr\Container\ContainerInterface;

if ($container instanceof ContainerInterface) {
/** @var ContainerInterface */
$container->get('Foo');
}

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

namespace Rector\Tests\Renaming\Rector\Name\RenameClassRector\FixtureAutoImportNamesWithoutRemoveUnusedImport;

use DateTime;

if ($var instanceof DateTime) {
/** @var DateTime */
$var->format('Y-m-d');
}

?>
-----
<?php

namespace Rector\Tests\Renaming\Rector\Name\RenameClassRector\FixtureAutoImportNamesWithoutRemoveUnusedImport;

use DateTimeInterface;

if ($var instanceof DateTimeInterface) {
/** @var DateTimeInterface */
$var->format('Y-m-d');
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@

$rectorConfig->ruleWithConfiguration(RenameClassRector::class, [
'Interop\Container\ContainerInterface' => 'Psr\Container\ContainerInterface',
'DateTime' => 'DateTimeInterface',
]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\CodingStyle\ClassNameImport\ClassNameImportSkipVoter;

use Nette\Utils\Strings;
use PhpParser\Node;
use Rector\CodingStyle\ClassNameImport\ShortNameResolver;
use Rector\CodingStyle\Contract\ClassNameImport\ClassNameImportSkipVoterInterface;
Expand Down Expand Up @@ -34,17 +35,30 @@ public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedO
/** @var array<string, string> $shortNamesToFullyQualifiedNames */
$shortNamesToFullyQualifiedNames = $this->shortNameResolver->resolveFromFile($file);
$removedUses = $this->renamedClassesDataCollector->getOldClasses();
$fullyQualifiedObjectTypeShortName = $fullyQualifiedObjectType->getShortName();
$className = $fullyQualifiedObjectType->getClassName();

foreach ($shortNamesToFullyQualifiedNames as $shortName => $fullyQualifiedName) {
if ($fullyQualifiedObjectType->getShortName() !== $shortName) {
if ($fullyQualifiedObjectTypeShortName !== $shortName) {
$shortName = str_starts_with($shortName, '\\')
? ltrim((string) Strings::after($shortName, '\\', -1))
: $shortName;
}

if ($fullyQualifiedObjectTypeShortName !== $shortName) {
continue;
}

$fullyQualifiedName = ltrim($fullyQualifiedName, '\\');
if ($className === $fullyQualifiedName) {
return false;
}

if (in_array($fullyQualifiedName, $removedUses, true)) {
continue;
return false;
}

return $fullyQualifiedObjectType->getClassName() !== $fullyQualifiedName;
return str_contains($fullyQualifiedName, '\\');
}

return false;
Expand Down