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-06 17:12:56 +00:00
|
|
|
final class FullyQualifiedNameClassNameImportSkipVoter implements \Rector\CodingStyle\Contract\ClassNameImport\ClassNameImportSkipVoterInterface
|
2020-11-20 13:37:53 +00:00
|
|
|
{
|
|
|
|
/**
|
2021-12-04 12:47:17 +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;
|
2022-06-06 17:12:56 +00:00
|
|
|
public function __construct(\Rector\CodingStyle\ClassNameImport\ShortNameResolver $shortNameResolver)
|
2020-11-20 13:37:53 +00:00
|
|
|
{
|
|
|
|
$this->shortNameResolver = $shortNameResolver;
|
|
|
|
}
|
2022-06-06 17:12:56 +00:00
|
|
|
public function shouldSkip(\Rector\Core\ValueObject\Application\File $file, \Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType $fullyQualifiedObjectType, \PhpParser\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-11-14 09:32:59 +00:00
|
|
|
$loweredShortNameFullyQualified = $fullyQualifiedObjectType->getShortNameLowered();
|
2021-03-07 17:59:10 +00:00
|
|
|
foreach ($shortNamesToFullyQualifiedNames as $shortName => $fullyQualifiedName) {
|
2021-05-09 20:15:43 +00:00
|
|
|
$shortNameLowered = \strtolower($shortName);
|
2021-11-14 09:32:59 +00:00
|
|
|
if ($loweredShortNameFullyQualified !== $shortNameLowered) {
|
2020-11-20 13:37:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return $fullyQualifiedObjectType->getClassNameLowered() !== \strtolower($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
|
|
|
}
|
|
|
|
}
|