mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-15 13:25:30 +01:00
74 lines
2.0 KiB
PHP
74 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Rector\CodingStyle\ClassNameImport;
|
|
|
|
use Nette\Utils\Strings;
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Name;
|
|
use PhpParser\Node\Stmt\Use_;
|
|
use Rector\CodingStyle\Contract\ClassNameImport\ClassNameImportSkipVoterInterface;
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
|
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
|
|
|
|
final class ClassNameImportSkipper
|
|
{
|
|
/**
|
|
* @var ClassNameImportSkipVoterInterface[]
|
|
*/
|
|
private $classNameImportSkipVoters = [];
|
|
|
|
/**
|
|
* @param ClassNameImportSkipVoterInterface[] $classNameImportSkipVoters
|
|
*/
|
|
public function __construct(array $classNameImportSkipVoters)
|
|
{
|
|
$this->classNameImportSkipVoters = $classNameImportSkipVoters;
|
|
}
|
|
|
|
public function shouldSkipNameForFullyQualifiedObjectType(
|
|
Node $node,
|
|
FullyQualifiedObjectType $fullyQualifiedObjectType
|
|
): bool {
|
|
foreach ($this->classNameImportSkipVoters as $classNameImportSkipVoter) {
|
|
if ($classNameImportSkipVoter->shouldSkip($fullyQualifiedObjectType, $node)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function isShortNameInUseStatement(Name $name): bool
|
|
{
|
|
$longName = $name->toString();
|
|
if (Strings::contains($longName, '\\')) {
|
|
return false;
|
|
}
|
|
|
|
return $this->isFoundInUse($name);
|
|
}
|
|
|
|
public function isFoundInUse(Name $name): bool
|
|
{
|
|
/** @var Use_[] $uses */
|
|
$uses = (array) $name->getAttribute(AttributeKey::USE_NODES);
|
|
|
|
foreach ($uses as $use) {
|
|
$useUses = $use->uses;
|
|
foreach ($useUses as $useUse) {
|
|
if (! $useUse->name instanceof Name) {
|
|
continue;
|
|
}
|
|
if ($useUse->name->getLast() !== $name->getLast()) {
|
|
continue;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|