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
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use Rector\CodingStyle\ClassNameImport\ShortNameResolver;
|
|
|
|
use Rector\CodingStyle\Contract\ClassNameImport\ClassNameImportSkipVoterInterface;
|
|
|
|
use Rector\Core\ValueObject\Application\File;
|
|
|
|
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
|
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
|
|
|
{
|
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\CodingStyle\ClassNameImport\ShortNameResolver
|
2020-11-20 13:37:53 +00:00
|
|
|
*/
|
|
|
|
private $shortNameResolver;
|
2022-06-07 08:22:29 +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);
|
2021-03-07 17:59:10 +00:00
|
|
|
foreach ($shortNamesToFullyQualifiedNames as $shortName => $fullyQualifiedName) {
|
2023-02-23 10:31:39 +00:00
|
|
|
if ($fullyQualifiedObjectType->getShortName() !== $shortName) {
|
2020-11-20 13:37:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-02-23 10:31:39 +00:00
|
|
|
return $fullyQualifiedObjectType->getClassName() !== $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
|
|
|
}
|
|
|
|
}
|