|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace PhpCsFixerCustomFixersDev\Fixer { |
| 6 | + use PhpCsFixer\Fixer\ClassNotation\OrderedClassElementsFixer; |
| 7 | + use PhpCsFixer\Fixer\FixerInterface; |
| 8 | + use PhpCsFixer\Tokenizer\Tokens; |
| 9 | + |
| 10 | + /** |
| 11 | + * @internal |
| 12 | + */ |
| 13 | + final class OrderedClassElementsInternalFixer implements FixerInterface |
| 14 | + { |
| 15 | + public const PUBLIC_METHODS_ORDER = [ |
| 16 | + 'getDefinition', |
| 17 | + 'getConfigurationDefinition', |
| 18 | + 'configure', |
| 19 | + 'getName', |
| 20 | + 'getPriority', |
| 21 | + 'getPullRequestId', |
| 22 | + 'supports', |
| 23 | + 'isCandidate', |
| 24 | + 'isRisky', |
| 25 | + 'fix', |
| 26 | + 'getSuccessorsNames', |
| 27 | + ]; |
| 28 | + |
| 29 | + /** @var OrderedClassElementsFixer */ |
| 30 | + private $orderedClassElementsFixer; |
| 31 | + |
| 32 | + public function __construct() |
| 33 | + { |
| 34 | + $this->orderedClassElementsFixer = new OrderedClassElementsFixer(); |
| 35 | + } |
| 36 | + |
| 37 | + public function getName(): string |
| 38 | + { |
| 39 | + return 'Internal/' . $this->orderedClassElementsFixer->getName(); |
| 40 | + } |
| 41 | + |
| 42 | + public function getPriority(): int |
| 43 | + { |
| 44 | + return $this->orderedClassElementsFixer->getPriority(); |
| 45 | + } |
| 46 | + |
| 47 | + public function supports(\SplFileInfo $file): bool |
| 48 | + { |
| 49 | + return $this->orderedClassElementsFixer->supports($file); |
| 50 | + } |
| 51 | + |
| 52 | + public function isCandidate(Tokens $tokens): bool |
| 53 | + { |
| 54 | + return $tokens->findSequence([[T_EXTENDS], [T_STRING, 'AbstractFixer']]) !== null; |
| 55 | + } |
| 56 | + |
| 57 | + public function isRisky(): bool |
| 58 | + { |
| 59 | + return $this->orderedClassElementsFixer->isRisky(); |
| 60 | + } |
| 61 | + |
| 62 | + public function fix(\SplFileInfo $file, Tokens $tokens): void |
| 63 | + { |
| 64 | + $this->orderedClassElementsFixer->fix($file, $tokens); |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +namespace PhpCsFixer\Fixer\ClassNotation { |
| 70 | + use PhpCsFixerCustomFixersDev\Fixer\OrderedClassElementsInternalFixer; |
| 71 | + |
| 72 | + /** |
| 73 | + * @internal |
| 74 | + */ |
| 75 | + function usort(array &$elements): void |
| 76 | + { |
| 77 | + \usort($elements, static function (array $a, array $b) { |
| 78 | + if ($a['type'] === 'method' && $a['visibility'] === 'public' |
| 79 | + && $b['type'] === 'method' && $b['visibility'] === 'public' |
| 80 | + && isset($a['name'], $b['name'])) { |
| 81 | + if (!\in_array($a['name'], OrderedClassElementsInternalFixer::PUBLIC_METHODS_ORDER, true)) { |
| 82 | + throw new \Exception(\sprintf('Method %s not in order list', $a['name'])); |
| 83 | + } |
| 84 | + if (!\in_array($b['name'], OrderedClassElementsInternalFixer::PUBLIC_METHODS_ORDER, true)) { |
| 85 | + throw new \Exception(\sprintf('Method %s not in order list', $b['name'])); |
| 86 | + } |
| 87 | + foreach (OrderedClassElementsInternalFixer::PUBLIC_METHODS_ORDER as $name) { |
| 88 | + if ($a['name'] === $name) { |
| 89 | + return -1; |
| 90 | + } |
| 91 | + if ($b['name'] === $name) { |
| 92 | + return 1; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if ($a['position'] === $b['position']) { |
| 98 | + return $a['start'] <=> $b['start']; |
| 99 | + } |
| 100 | + |
| 101 | + return $a['position'] <=> $b['position']; |
| 102 | + }); |
| 103 | + } |
| 104 | +} |
0 commit comments