|
| 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 ClassSuffixFixer extends AbstractFixer |
| 15 | +{ |
| 16 | + /** |
| 17 | + * Mapping of laravel components to their expected suffix. Null |
| 18 | + * values indicate that the class should _not_ be suffixed. |
| 19 | + * |
| 20 | + * @var array |
| 21 | + */ |
| 22 | + private static $suffixes = [ |
| 23 | + 'command' => 'Command', |
| 24 | + 'controller' => 'Controller', |
| 25 | + 'event' => null, |
| 26 | + 'exception' => 'Exception', |
| 27 | + 'factory' => 'Factory', |
| 28 | + 'form_request' => 'Request', |
| 29 | + 'job' => 'Job', |
| 30 | + 'listener' => 'Listener', |
| 31 | + 'mailable' => 'Mail', |
| 32 | + 'middleware' => null, |
| 33 | + 'model' => null, |
| 34 | + 'notification' => 'Notification', |
| 35 | + 'provider' => 'ServiceProvider', |
| 36 | + 'resource' => 'Resource', |
| 37 | + ]; |
| 38 | + |
| 39 | + /** |
| 40 | + * Check if the fixer is a candidate for given Tokens collection. |
| 41 | + * |
| 42 | + * Fixer is a candidate when the collection contains tokens that may be fixed |
| 43 | + * during fixer work. This could be considered as some kind of bloom filter. |
| 44 | + * When this method returns true then to the Tokens collection may or may not |
| 45 | + * need a fixing, but when this method returns false then the Tokens collection |
| 46 | + * need no fixing for sure. |
| 47 | + */ |
| 48 | + public function isCandidate(Tokens $tokens): bool |
| 49 | + { |
| 50 | + return $tokens->isAnyTokenKindsFound([T_CLASS]); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Check if fixer is risky or not. |
| 55 | + * |
| 56 | + * Risky fixer could change code behavior! |
| 57 | + */ |
| 58 | + public function isRisky(): bool |
| 59 | + { |
| 60 | + return true; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Fixes a file. |
| 65 | + */ |
| 66 | + public function applyFix(SplFileInfo $file, Tokens $tokens): void |
| 67 | + { |
| 68 | + if (! $component = LaravelIdentifier::identify($file)) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + $suffix = static::$suffixes[$component]; |
| 73 | + [$classNameToken, $classNameTokenIndex] = $this->findClassNameToken($tokens); |
| 74 | + |
| 75 | + $adjusted = $suffix |
| 76 | + ? Str::finish($classNameToken->getContent(), $suffix) |
| 77 | + : Str::beforeLast($classNameToken->getContent(), Str::studly($component)); |
| 78 | + |
| 79 | + $tokens[$classNameTokenIndex] = new Token($adjusted); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Returns the definition of the fixer. |
| 84 | + */ |
| 85 | + public function getDefinition(): FixerDefinitionInterface |
| 86 | + { |
| 87 | + return new FixerDefinition( |
| 88 | + 'Enforce classes are properly suffixed.', |
| 89 | + [], |
| 90 | + null, |
| 91 | + 'Renames classes and cannot rename the files or references. You might need to do additional manual fixing.' |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Returns true if the file is supported by this fixer. |
| 97 | + * |
| 98 | + * @return bool true if the file is supported by this fixer, false otherwise |
| 99 | + */ |
| 100 | + public function supports(SplFileInfo $file): bool |
| 101 | + { |
| 102 | + return ! Str::contains($file->getPath(), ['migrations']); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Get the token representing the name of the class. |
| 107 | + */ |
| 108 | + protected function findClassNameToken(Tokens $tokens): array|null |
| 109 | + { |
| 110 | + foreach ($tokens as $index => $token) { |
| 111 | + if ($token->isGivenKind(T_CLASS)) { |
| 112 | + $classNameTokenIndex = $index + 2; |
| 113 | + |
| 114 | + return [$tokens[$classNameTokenIndex], $classNameTokenIndex]; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return null; |
| 119 | + } |
| 120 | +} |
0 commit comments