|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tempest\Rector; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Name; |
| 9 | +use PhpParser\Node\Name\FullyQualified; |
| 10 | +use PhpParser\Node\Stmt\GroupUse; |
| 11 | +use PhpParser\Node\Stmt\Use_; |
| 12 | +use Rector\CodingStyle\Node\NameImporter; |
| 13 | +use Rector\Configuration\Option; |
| 14 | +use Rector\Configuration\Parameter\SimpleParameterProvider; |
| 15 | +use Rector\Contract\Rector\ConfigurableRectorInterface; |
| 16 | +use Rector\Naming\Naming\UseImportsResolver; |
| 17 | +use Rector\NodeTypeResolver\Node\AttributeKey; |
| 18 | +use Rector\Rector\AbstractRector; |
| 19 | + |
| 20 | +final class ImportClassNamesRector extends AbstractRector implements ConfigurableRectorInterface |
| 21 | +{ |
| 22 | + private ?string $currentNamespace = null; |
| 23 | + |
| 24 | + private ?string $currentFilePath = null; |
| 25 | + |
| 26 | + /** @var list<string> */ |
| 27 | + private array $excludedClasses = []; |
| 28 | + |
| 29 | + public function __construct( |
| 30 | + private readonly NameImporter $nameImporter, |
| 31 | + private readonly UseImportsResolver $useImportsResolver, |
| 32 | + ) { |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @param array{importShortClasses?: bool, excludedClasses?: list<string>} $configuration |
| 37 | + */ |
| 38 | + public function configure(array $configuration): void |
| 39 | + { |
| 40 | + SimpleParameterProvider::setParameter( |
| 41 | + Option::IMPORT_SHORT_CLASSES, |
| 42 | + $configuration['importShortClasses'] ?? true, |
| 43 | + ); |
| 44 | + |
| 45 | + $this->excludedClasses = array_map( |
| 46 | + static fn (string $class): string => ltrim($class, '\\'), |
| 47 | + $configuration['excludedClasses'] ?? [], |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + public function getNodeTypes(): array |
| 52 | + { |
| 53 | + return [FullyQualified::class]; |
| 54 | + } |
| 55 | + |
| 56 | + public function refactor(Node $node): ?Name |
| 57 | + { |
| 58 | + if (! $node instanceof FullyQualified) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + if ($node->getAttribute(AttributeKey::IS_FUNCCALL_NAME) === true) { |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + if ($node->getAttribute(AttributeKey::IS_CONSTFETCH_NAME) === true) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + if ($this->isAlreadyShortInSource($node)) { |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + if (in_array($node->toString(), $this->excludedClasses, true)) { |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + $currentUses = $this->useImportsResolver->resolve(); |
| 79 | + |
| 80 | + $imported = $this->nameImporter->importName($node, $this->file, $currentUses); |
| 81 | + |
| 82 | + if ($imported instanceof Name) { |
| 83 | + return $imported; |
| 84 | + } |
| 85 | + |
| 86 | + return $this->shortenSameNamespaceName($node, $currentUses); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @param array<Use_|GroupUse> $currentUses |
| 91 | + */ |
| 92 | + private function shortenSameNamespaceName(FullyQualified $node, array $currentUses): ?Name |
| 93 | + { |
| 94 | + $namespaceName = $this->resolveCurrentNamespace(); |
| 95 | + |
| 96 | + if ($namespaceName === null) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + $fqcn = $node->toString(); |
| 101 | + $shortName = $node->getLast(); |
| 102 | + $fqcnNamespace = substr($fqcn, 0, -(strlen($shortName) + 1)); |
| 103 | + |
| 104 | + if ($fqcnNamespace !== $namespaceName) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + if ($this->hasConflictingUseStatement($shortName, $fqcn, $currentUses)) { |
| 109 | + return null; |
| 110 | + } |
| 111 | + |
| 112 | + return new Name($shortName); |
| 113 | + } |
| 114 | + |
| 115 | + private function isAlreadyShortInSource(FullyQualified $node): bool |
| 116 | + { |
| 117 | + $startTokenPos = $node->getStartTokenPos(); |
| 118 | + $oldTokens = $this->file->getOldTokens(); |
| 119 | + if (! isset($oldTokens[$startTokenPos])) { |
| 120 | + return false; |
| 121 | + } |
| 122 | + $originalText = $oldTokens[$startTokenPos]->text; |
| 123 | + return ! str_contains($originalText, '\\'); |
| 124 | + } |
| 125 | + |
| 126 | + private function resolveCurrentNamespace(): ?string |
| 127 | + { |
| 128 | + $filePath = $this->file->getFilePath(); |
| 129 | + |
| 130 | + if ($this->currentFilePath === $filePath) { |
| 131 | + return $this->currentNamespace; |
| 132 | + } |
| 133 | + |
| 134 | + $this->currentFilePath = $filePath; |
| 135 | + $this->currentNamespace = null; |
| 136 | + |
| 137 | + $fileNode = $this->file->getFileNode(); |
| 138 | + |
| 139 | + if ($fileNode === null) { |
| 140 | + return null; |
| 141 | + } |
| 142 | + |
| 143 | + $namespace = $fileNode->getNamespace(); |
| 144 | + |
| 145 | + if ($namespace?->name !== null) { |
| 146 | + $this->currentNamespace = $namespace->name->toString(); |
| 147 | + } |
| 148 | + |
| 149 | + return $this->currentNamespace; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * @param array<Use_|GroupUse> $currentUses |
| 154 | + */ |
| 155 | + private function hasConflictingUseStatement(string $shortName, string $fqcn, array $currentUses): bool |
| 156 | + { |
| 157 | + foreach ($currentUses as $use) { |
| 158 | + foreach ($use->uses as $useUse) { |
| 159 | + $importedName = $use instanceof GroupUse |
| 160 | + ? $use->prefix . '\\' . $useUse->name->toString() |
| 161 | + : $useUse->name->toString(); |
| 162 | + |
| 163 | + $alias = $useUse->alias?->toString() ?? $useUse->name->getLast(); |
| 164 | + |
| 165 | + if ($alias === $shortName && $importedName !== $fqcn) { |
| 166 | + return true; |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + return false; |
| 172 | + } |
| 173 | +} |
0 commit comments