|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Nextcloud\CodingStandard\Fixer; |
| 6 | + |
| 7 | +use PhpCsFixer\Fixer\FixerInterface; |
| 8 | +use PhpCsFixer\FixerDefinition\CodeSample; |
| 9 | +use PhpCsFixer\FixerDefinition\FixerDefinition; |
| 10 | +use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; |
| 11 | +use PhpCsFixer\Tokenizer\Token; |
| 12 | +use PhpCsFixer\Tokenizer\Tokens; |
| 13 | + |
| 14 | +final class NoThreeDotsInStringFixer implements FixerInterface { |
| 15 | + |
| 16 | + public function isCandidate(Tokens $tokens): bool { |
| 17 | + return $tokens->isAnyTokenKindsFound([T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE]); |
| 18 | + } |
| 19 | + |
| 20 | + public function isRisky(): bool { |
| 21 | + return true; |
| 22 | + } |
| 23 | + |
| 24 | + public function getName(): string { |
| 25 | + return 'Nextcloud/no_three_dots_in_string'; |
| 26 | + } |
| 27 | + |
| 28 | + public function getPriority(): int |
| 29 | + { |
| 30 | + return 0; |
| 31 | + } |
| 32 | + |
| 33 | + public function supports(\SplFileInfo $file): bool { |
| 34 | + return true; |
| 35 | + } |
| 36 | + |
| 37 | + public function getDefinition(): FixerDefinitionInterface { |
| 38 | + return new FixerDefinition( |
| 39 | + 'There must be no three dots in strings, instead ellipsis shall be used.', |
| 40 | + [ |
| 41 | + new CodeSample( |
| 42 | + "<?php \$a = 'Loading ...';\n" |
| 43 | + ), |
| 44 | + ], |
| 45 | + null, |
| 46 | + 'Changing the characters in strings might affect string comparisons and outputs.' |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + public function fix(\SplFileInfo $file, Tokens $tokens): void { |
| 51 | + for ($index = $tokens->count() - 1; $index >= 0; --$index) { |
| 52 | + /** @var Token $token */ |
| 53 | + $token = $tokens[$index]; |
| 54 | + |
| 55 | + if (!$token->isGivenKind([T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE])) { |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + $content = str_replace('...', '…', $token->getContent()); |
| 60 | + if ($token->getContent() === $content) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + $tokens[$index] = new Token([$tokens[$index]->getId(), $content]); |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments