mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-06 19:30:50 +00:00
184cf49468
f9de5d311e
[Php80] Handle RenameClassRector+AnnotationToAttributeRector with auto import and existing attribute defined (#5219)
56 lines
2.0 KiB
PHP
56 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\CodingStyle\ClassNameImport\ClassNameImportSkipVoter;
|
|
|
|
use PhpParser\Node;
|
|
use Rector\CodingStyle\ClassNameImport\ShortNameResolver;
|
|
use Rector\CodingStyle\Contract\ClassNameImport\ClassNameImportSkipVoterInterface;
|
|
use Rector\Core\Configuration\RenamedClassesDataCollector;
|
|
use Rector\Core\ValueObject\Application\File;
|
|
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
|
|
/**
|
|
* Prevents adding:
|
|
*
|
|
* use App\SomeClass;
|
|
*
|
|
* If there is already:
|
|
*
|
|
* SomeClass::callThis();
|
|
*/
|
|
final class FullyQualifiedNameClassNameImportSkipVoter implements ClassNameImportSkipVoterInterface
|
|
{
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\CodingStyle\ClassNameImport\ShortNameResolver
|
|
*/
|
|
private $shortNameResolver;
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\Core\Configuration\RenamedClassesDataCollector
|
|
*/
|
|
private $renamedClassesDataCollector;
|
|
public function __construct(ShortNameResolver $shortNameResolver, RenamedClassesDataCollector $renamedClassesDataCollector)
|
|
{
|
|
$this->shortNameResolver = $shortNameResolver;
|
|
$this->renamedClassesDataCollector = $renamedClassesDataCollector;
|
|
}
|
|
public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedObjectType, Node $node) : bool
|
|
{
|
|
// "new X" or "X::static()"
|
|
/** @var array<string, string> $shortNamesToFullyQualifiedNames */
|
|
$shortNamesToFullyQualifiedNames = $this->shortNameResolver->resolveFromFile($file);
|
|
$removedUses = $this->renamedClassesDataCollector->getOldClasses();
|
|
foreach ($shortNamesToFullyQualifiedNames as $shortName => $fullyQualifiedName) {
|
|
if ($fullyQualifiedObjectType->getShortName() !== $shortName) {
|
|
continue;
|
|
}
|
|
if (\in_array($fullyQualifiedName, $removedUses, \true)) {
|
|
continue;
|
|
}
|
|
return $fullyQualifiedObjectType->getClassName() !== $fullyQualifiedName;
|
|
}
|
|
return \false;
|
|
}
|
|
}
|