|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace SlevomatCodingStandard\Sniffs\Classes; |
| 4 | + |
| 5 | +use PHP_CodeSniffer\Files\File; |
| 6 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 7 | +use SlevomatCodingStandard\Helpers\TokenHelper; |
| 8 | +use function array_key_first; |
| 9 | +use function array_keys; |
| 10 | +use function array_map; |
| 11 | +use function array_values; |
| 12 | +use function count; |
| 13 | +use function implode; |
| 14 | +use function ksort; |
| 15 | +use function uasort; |
| 16 | +use const T_ABSTRACT; |
| 17 | +use const T_CLASS; |
| 18 | +use const T_FINAL; |
| 19 | +use const T_READONLY; |
| 20 | +use const T_WHITESPACE; |
| 21 | + |
| 22 | +class ClassKeywordOrderSniff implements Sniff |
| 23 | +{ |
| 24 | + |
| 25 | + public const CODE_WRONG_CLASS_KEYWORD_ORDER = 'WrongClassKeywordOrder'; |
| 26 | + |
| 27 | + /** |
| 28 | + * @return array<int, (int|string)> |
| 29 | + */ |
| 30 | + public function register(): array |
| 31 | + { |
| 32 | + return [ |
| 33 | + T_CLASS, |
| 34 | + ]; |
| 35 | + } |
| 36 | + |
| 37 | + public function process(File $phpcsFile, int $stackPtr): void |
| 38 | + { |
| 39 | + $tokens = $phpcsFile->getTokens(); |
| 40 | + |
| 41 | + $modifierTokens = [ |
| 42 | + T_ABSTRACT => 'abstract', |
| 43 | + T_FINAL => 'final', |
| 44 | + T_READONLY => 'readonly', |
| 45 | + ]; |
| 46 | + |
| 47 | + $foundModifiers = []; |
| 48 | + $currentIndex = TokenHelper::findPreviousEffective($phpcsFile, $stackPtr - 1); |
| 49 | + |
| 50 | + while ($currentIndex !== null && isset($modifierTokens[$tokens[$currentIndex]['code']])) { |
| 51 | + $foundModifiers[$currentIndex] = $tokens[$currentIndex]['code']; |
| 52 | + $currentIndex = TokenHelper::findPreviousEffective($phpcsFile, $currentIndex - 1); |
| 53 | + } |
| 54 | + |
| 55 | + if (count($foundModifiers) === 0) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + ksort($foundModifiers); |
| 60 | + |
| 61 | + $actualOrderCodes = array_values($foundModifiers); |
| 62 | + $actualOrderText = array_map(static fn ($code) => $modifierTokens[$code], $actualOrderCodes); |
| 63 | + |
| 64 | + $sortedModifiers = $foundModifiers; |
| 65 | + uasort($sortedModifiers, static function ($a, $b) { |
| 66 | + $priority = [ |
| 67 | + T_ABSTRACT => 0, |
| 68 | + T_FINAL => 0, |
| 69 | + T_READONLY => 1, |
| 70 | + ]; |
| 71 | + return $priority[$a] <=> $priority[$b]; |
| 72 | + }); |
| 73 | + |
| 74 | + $expectedOrderCodes = array_values($sortedModifiers); |
| 75 | + $expectedOrderText = array_map(static fn ($code) => $modifierTokens[$code], $expectedOrderCodes); |
| 76 | + |
| 77 | + if ($actualOrderCodes === $expectedOrderCodes) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + $error = 'Class keywords are not in the correct order. Found: "%s class"; Expected: "%s class"'; |
| 82 | + $data = [ |
| 83 | + implode(' ', $actualOrderText), |
| 84 | + implode(' ', $expectedOrderText), |
| 85 | + ]; |
| 86 | + |
| 87 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, self::CODE_WRONG_CLASS_KEYWORD_ORDER, $data); |
| 88 | + |
| 89 | + if ($fix !== true) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + $phpcsFile->fixer->beginChangeset(); |
| 94 | + |
| 95 | + foreach (array_keys($foundModifiers) as $ptr) { |
| 96 | + $phpcsFile->fixer->replaceToken($ptr, ''); |
| 97 | + |
| 98 | + if ($tokens[$ptr + 1]['code'] === T_WHITESPACE) { |
| 99 | + $phpcsFile->fixer->replaceToken($ptr + 1, ''); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + $firstModifierPtr = array_key_first($foundModifiers); |
| 104 | + |
| 105 | + $newContent = implode(' ', $expectedOrderText) . ' '; |
| 106 | + |
| 107 | + $phpcsFile->fixer->addContentBefore($firstModifierPtr, $newContent); |
| 108 | + |
| 109 | + $phpcsFile->fixer->endChangeset(); |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments