|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace LiveIntent\PhpCsFixer\Fixer\Naming; |
| 4 | + |
| 5 | +use SplFileInfo; |
| 6 | +use Illuminate\Support\Str; |
| 7 | +use PhpCsFixer\Tokenizer\Token; |
| 8 | +use PhpCsFixer\Tokenizer\Tokens; |
| 9 | +use LiveIntent\PhpCsFixer\AbstractFixer; |
| 10 | +use PhpCsFixer\FixerDefinition\FixerDefinition; |
| 11 | +use LiveIntent\PhpCsFixer\Util\LaravelIdentifier; |
| 12 | +use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; |
| 13 | + |
| 14 | +class ClassNumberFixer extends AbstractFixer |
| 15 | +{ |
| 16 | + /** |
| 17 | + * Mapping of laravel components to their expected class number. |
| 18 | + * |
| 19 | + * @var array |
| 20 | + */ |
| 21 | + public const NUMBERS = [ |
| 22 | + 'command' => 'singular', |
| 23 | + 'controller' => 'singular', |
| 24 | + 'factory' => 'singular', |
| 25 | + 'form_request' => 'singular', |
| 26 | + 'model' => 'singular', |
| 27 | + 'resource' => 'singular', |
| 28 | + 'table' => 'plural', |
| 29 | + ]; |
| 30 | + |
| 31 | + /** |
| 32 | + * Check if the fixer is a candidate for given Tokens collection. |
| 33 | + * |
| 34 | + * Fixer is a candidate when the collection contains tokens that may be fixed |
| 35 | + * during fixer work. This could be considered as some kind of bloom filter. |
| 36 | + * When this method returns true then to the Tokens collection may or may not |
| 37 | + * need a fixing, but when this method returns false then the Tokens collection |
| 38 | + * need no fixing for sure. |
| 39 | + */ |
| 40 | + public function isCandidate(Tokens $tokens): bool |
| 41 | + { |
| 42 | + return $tokens->isAnyTokenKindsFound([T_CLASS]); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Check if fixer is risky or not. |
| 47 | + * |
| 48 | + * Risky fixer could change code behavior! |
| 49 | + */ |
| 50 | + public function isRisky(): bool |
| 51 | + { |
| 52 | + return true; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Fixes a file. |
| 57 | + */ |
| 58 | + public function applyFix(SplFileInfo $file, Tokens $tokens): void |
| 59 | + { |
| 60 | + if (! $component = LaravelIdentifier::identify($file)) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + if (! $number = static::NUMBERS[$component] ?? null) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + $suffix = ClassSuffixFixer::SUFFIXES[$component] ?? ''; |
| 69 | + [$classNameToken, $classNameTokenIndex] = $this->findClassNameToken($tokens); |
| 70 | + |
| 71 | + $subject = Str::beforeLast($classNameToken->getContent(), $suffix); |
| 72 | + |
| 73 | + $adjusted = $number === 'singular' ? Str::singular($subject) : Str::plural($subject); |
| 74 | + |
| 75 | + $tokens[$classNameTokenIndex] = new Token($adjusted.$suffix); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Returns the definition of the fixer. |
| 80 | + */ |
| 81 | + public function getDefinition(): FixerDefinitionInterface |
| 82 | + { |
| 83 | + return new FixerDefinition( |
| 84 | + 'Enforce class names use the proper grammatical number.', |
| 85 | + [], |
| 86 | + null, |
| 87 | + 'Renames classes and cannot rename the files or references. You might need to do additional manual fixing.' |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Returns true if the file is supported by this fixer. |
| 93 | + * |
| 94 | + * @return bool true if the file is supported by this fixer, false otherwise |
| 95 | + */ |
| 96 | + public function supports(SplFileInfo $file): bool |
| 97 | + { |
| 98 | + return ! Str::contains($file->getPath(), ['migrations']); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Get the token representing the name of the class. |
| 103 | + */ |
| 104 | + protected function findClassNameToken(Tokens $tokens): array|null |
| 105 | + { |
| 106 | + foreach ($tokens as $index => $token) { |
| 107 | + if ($token->isGivenKind(T_CLASS)) { |
| 108 | + $classNameTokenIndex = $index + 2; |
| 109 | + |
| 110 | + return [$tokens[$classNameTokenIndex], $classNameTokenIndex]; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return null; |
| 115 | + } |
| 116 | +} |
0 commit comments