2020-11-20 13:37:53 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\CodingStyle\ClassNameImport\ClassNameImportSkipVoter;
|
2020-11-20 13:37:53 +00:00
|
|
|
|
2024-03-01 20:02:28 +00:00
|
|
|
use RectorPrefix202403\Nette\Utils\Strings;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use Rector\CodingStyle\ClassNameImport\ShortNameResolver;
|
|
|
|
use Rector\CodingStyle\Contract\ClassNameImport\ClassNameImportSkipVoterInterface;
|
|
|
|
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\ValueObject\Application\File;
|
2020-11-20 13:37:53 +00:00
|
|
|
/**
|
|
|
|
* Prevents adding:
|
|
|
|
*
|
|
|
|
* use App\SomeClass;
|
|
|
|
*
|
|
|
|
* If there is already:
|
|
|
|
*
|
|
|
|
* SomeClass::callThis();
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class FullyQualifiedNameClassNameImportSkipVoter implements ClassNameImportSkipVoterInterface
|
2020-11-20 13:37:53 +00:00
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\CodingStyle\ClassNameImport\ShortNameResolver
|
2020-11-20 13:37:53 +00:00
|
|
|
*/
|
|
|
|
private $shortNameResolver;
|
2024-03-23 16:49:42 +00:00
|
|
|
public function __construct(ShortNameResolver $shortNameResolver)
|
2020-11-20 13:37:53 +00:00
|
|
|
{
|
|
|
|
$this->shortNameResolver = $shortNameResolver;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedObjectType, Node $node) : bool
|
2020-11-20 13:37:53 +00:00
|
|
|
{
|
|
|
|
// "new X" or "X::static()"
|
2021-12-04 13:02:13 +00:00
|
|
|
/** @var array<string, string> $shortNamesToFullyQualifiedNames */
|
2021-11-04 20:44:47 +00:00
|
|
|
$shortNamesToFullyQualifiedNames = $this->shortNameResolver->resolveFromFile($file);
|
2023-11-03 08:39:18 +00:00
|
|
|
$fullyQualifiedObjectTypeShortName = $fullyQualifiedObjectType->getShortName();
|
|
|
|
$className = $fullyQualifiedObjectType->getClassName();
|
2021-03-07 17:59:10 +00:00
|
|
|
foreach ($shortNamesToFullyQualifiedNames as $shortName => $fullyQualifiedName) {
|
2023-11-03 08:39:18 +00:00
|
|
|
if ($fullyQualifiedObjectTypeShortName !== $shortName) {
|
2023-11-14 11:48:19 +00:00
|
|
|
$shortName = $this->cleanShortName($shortName);
|
2023-11-03 08:39:18 +00:00
|
|
|
}
|
|
|
|
if ($fullyQualifiedObjectTypeShortName !== $shortName) {
|
2020-11-20 13:37:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-11-03 08:39:18 +00:00
|
|
|
$fullyQualifiedName = \ltrim($fullyQualifiedName, '\\');
|
2024-03-23 16:49:42 +00:00
|
|
|
return $className !== $fullyQualifiedName;
|
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
|
|
|
}
|
2023-11-14 11:48:19 +00:00
|
|
|
private function cleanShortName(string $shortName) : string
|
|
|
|
{
|
|
|
|
return \strncmp($shortName, '\\', \strlen('\\')) === 0 ? \ltrim((string) Strings::after($shortName, '\\', -1)) : $shortName;
|
|
|
|
}
|
2020-11-20 13:37:53 +00:00
|
|
|
}
|