2020-11-20 13:37:53 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\CodingStyle\ClassNameImport;
|
|
|
|
|
2020-12-07 19:20:59 +07:00
|
|
|
use Nette\Utils\Strings;
|
2020-11-20 13:37:53 +00:00
|
|
|
use PhpParser\Node;
|
2020-12-07 19:20:59 +07:00
|
|
|
use PhpParser\Node\Name;
|
|
|
|
use PhpParser\Node\Stmt\Use_;
|
2020-11-20 13:37:53 +00:00
|
|
|
use Rector\CodingStyle\Contract\ClassNameImport\ClassNameImportSkipVoterInterface;
|
2020-12-07 19:20:59 +07:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2020-12-24 17:31:24 +01:00
|
|
|
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
|
2020-11-20 13:37:53 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2020-12-07 19:20:59 +07:00
|
|
|
|
|
|
|
public function isShortNameInUseStatement(Name $name): bool
|
|
|
|
{
|
|
|
|
$longName = $name->toString();
|
|
|
|
if (Strings::contains($longName, '\\')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->isFoundInUse($name);
|
|
|
|
}
|
|
|
|
|
2021-01-08 01:27:31 +07:00
|
|
|
public function isFoundInUse(Name $name): bool
|
2020-12-07 19:20:59 +07:00
|
|
|
{
|
|
|
|
/** @var Use_[] $uses */
|
2020-12-09 17:09:26 +01:00
|
|
|
$uses = (array) $name->getAttribute(AttributeKey::USE_NODES);
|
2020-12-09 23:25:53 +01:00
|
|
|
|
2020-12-07 19:20:59 +07:00
|
|
|
foreach ($uses as $use) {
|
2021-03-21 19:41:17 +01:00
|
|
|
foreach ($use->uses as $useUse) {
|
2021-02-21 16:32:45 +07:00
|
|
|
if ($useUse->name->getLast() !== $name->getLast()) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-03-21 19:41:17 +01:00
|
|
|
|
2021-02-21 16:32:45 +07:00
|
|
|
return true;
|
2020-12-07 19:20:59 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-20 13:37:53 +00:00
|
|
|
}
|