|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Hereldar\CodingStyle\Rules; |
| 6 | + |
| 7 | +use PhpCsFixer\AbstractFixer; |
| 8 | +use PhpCsFixer\Fixer\ConfigurableFixerInterface; |
| 9 | +use PhpCsFixer\Fixer\Import\GlobalNamespaceImportFixer as BaseGlobalNamespaceImportFixer; |
| 10 | +use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface; |
| 11 | +use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface; |
| 12 | +use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; |
| 13 | +use PhpCsFixer\Tokenizer\Tokens; |
| 14 | +use SplFileInfo; |
| 15 | + |
| 16 | +final class GlobalNamespaceImportFixer extends AbstractFixer implements ConfigurableFixerInterface, WhitespacesAwareFixerInterface |
| 17 | +{ |
| 18 | + private ?BaseGlobalNamespaceImportFixer $fixer = null; |
| 19 | + |
| 20 | + public function getDefinition(): FixerDefinitionInterface |
| 21 | + { |
| 22 | + return $this->fixer()->getDefinition(); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * {@inheritdoc} |
| 27 | + * |
| 28 | + * Must run after NoUnusedImportsFixer. |
| 29 | + */ |
| 30 | + public function getPriority(): int |
| 31 | + { |
| 32 | + return -20; |
| 33 | + } |
| 34 | + |
| 35 | + public function isCandidate(Tokens $tokens): bool |
| 36 | + { |
| 37 | + return $this->fixer()->isCandidate($tokens); |
| 38 | + } |
| 39 | + |
| 40 | + protected function applyFix(SplFileInfo $file, Tokens $tokens): void |
| 41 | + { |
| 42 | + $this->fixer()->applyFix($file, $tokens); |
| 43 | + } |
| 44 | + |
| 45 | + protected function createConfigurationDefinition(): FixerConfigurationResolverInterface |
| 46 | + { |
| 47 | + return $this->fixer()->createConfigurationDefinition(); |
| 48 | + } |
| 49 | + |
| 50 | + private function fixer(): BaseGlobalNamespaceImportFixer |
| 51 | + { |
| 52 | + /** |
| 53 | + * @psalm-suppress MixedAssignment |
| 54 | + * @psalm-suppress MixedReturnStatement |
| 55 | + */ |
| 56 | + return $this->fixer ??= new BaseGlobalNamespaceImportFixer(); |
| 57 | + } |
| 58 | +} |
0 commit comments