-
-
Notifications
You must be signed in to change notification settings - Fork 699
/
UsesClassNameImportSkipVoter.php
41 lines (39 loc) · 1.39 KB
/
UsesClassNameImportSkipVoter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
declare (strict_types=1);
namespace Rector\CodingStyle\ClassNameImport\ClassNameImportSkipVoter;
use PhpParser\Node;
use Rector\CodingStyle\Contract\ClassNameImport\ClassNameImportSkipVoterInterface;
use Rector\PostRector\Collector\UseNodesToAddCollector;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Rector\ValueObject\Application\File;
/**
* This prevents importing:
* - App\Some\Product
*
* if there is already:
* - use App\Another\Product
*/
final class UsesClassNameImportSkipVoter implements ClassNameImportSkipVoterInterface
{
/**
* @readonly
*/
private UseNodesToAddCollector $useNodesToAddCollector;
public function __construct(UseNodesToAddCollector $useNodesToAddCollector)
{
$this->useNodesToAddCollector = $useNodesToAddCollector;
}
public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedObjectType, Node $node) : bool
{
$useImportTypes = $this->useNodesToAddCollector->getUseImportTypesByNode($file);
foreach ($useImportTypes as $useImportType) {
if (!$useImportType->equals($fullyQualifiedObjectType) && $useImportType->areShortNamesEqual($fullyQualifiedObjectType)) {
return \true;
}
if ($useImportType->equals($fullyQualifiedObjectType)) {
return \false;
}
}
return \false;
}
}