rector/rules/CodingStyle/ClassNameImport/ClassNameImportSkipVoter/FullyQualifiedNameClassNameImportSkipVoter.php

47 lines
1.8 KiB
PHP
Raw Normal View History

<?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\ValueObject\Application\File;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
/**
* Prevents adding:
*
* use App\SomeClass;
*
* If there is already:
*
* SomeClass::callThis();
*/
final class FullyQualifiedNameClassNameImportSkipVoter implements \Rector\CodingStyle\Contract\ClassNameImport\ClassNameImportSkipVoterInterface
{
/**
* @readonly
* @var \Rector\CodingStyle\ClassNameImport\ShortNameResolver
*/
private $shortNameResolver;
public function __construct(\Rector\CodingStyle\ClassNameImport\ShortNameResolver $shortNameResolver)
{
$this->shortNameResolver = $shortNameResolver;
}
public function shouldSkip(\Rector\Core\ValueObject\Application\File $file, \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType $fullyQualifiedObjectType, \PhpParser\Node $node) : bool
{
// "new X" or "X::static()"
/** @var array<string, string> $shortNamesToFullyQualifiedNames */
$shortNamesToFullyQualifiedNames = $this->shortNameResolver->resolveFromFile($file);
$loweredShortNameFullyQualified = $fullyQualifiedObjectType->getShortNameLowered();
foreach ($shortNamesToFullyQualifiedNames as $shortName => $fullyQualifiedName) {
$shortNameLowered = \strtolower($shortName);
if ($loweredShortNameFullyQualified !== $shortNameLowered) {
continue;
}
return $fullyQualifiedObjectType->getClassNameLowered() !== \strtolower($fullyQualifiedName);
}
return \false;
}
}