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