2020-11-20 13:37:53 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2020-11-20 13:37:53 +00:00
|
|
|
namespace Rector\CodingStyle\ClassNameImport;
|
|
|
|
|
2021-05-19 00:12:47 +00:00
|
|
|
use RectorPrefix20210519\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
|
|
|
|
{
|
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var mixed[]
|
2020-11-20 13:37:53 +00:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $classNameImportSkipVoters;
|
2020-11-20 13:37:53 +00:00
|
|
|
/**
|
|
|
|
* @param ClassNameImportSkipVoterInterface[] $classNameImportSkipVoters
|
|
|
|
*/
|
|
|
|
public function __construct(array $classNameImportSkipVoters)
|
|
|
|
{
|
|
|
|
$this->classNameImportSkipVoters = $classNameImportSkipVoters;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function shouldSkipNameForFullyQualifiedObjectType(\PhpParser\Node $node, \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType $fullyQualifiedObjectType) : bool
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2020-11-20 13:37:53 +00:00
|
|
|
foreach ($this->classNameImportSkipVoters as $classNameImportSkipVoter) {
|
|
|
|
if ($classNameImportSkipVoter->shouldSkip($fullyQualifiedObjectType, $node)) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-11-20 13:37:53 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-11-20 13:37:53 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function isShortNameInUseStatement(\PhpParser\Node\Name $name) : bool
|
2020-12-07 19:20:59 +07:00
|
|
|
{
|
|
|
|
$longName = $name->toString();
|
2021-05-19 00:12:47 +00:00
|
|
|
if (\RectorPrefix20210519\Nette\Utils\Strings::contains($longName, '\\')) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-12-07 19:20:59 +07:00
|
|
|
}
|
|
|
|
return $this->isFoundInUse($name);
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function isFoundInUse(\PhpParser\Node\Name $name) : bool
|
2020-12-07 19:20:59 +07:00
|
|
|
{
|
|
|
|
/** @var Use_[] $uses */
|
2021-05-10 22:23:08 +00:00
|
|
|
$uses = (array) $name->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::USE_NODES);
|
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-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-12-07 19:20:59 +07:00
|
|
|
}
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-12-07 19:20:59 +07:00
|
|
|
}
|
2020-11-20 13:37:53 +00:00
|
|
|
}
|