|
| 1 | +<?php |
| 2 | +namespace PharIo\CSFixer; |
| 3 | + |
| 4 | +use PhpCsFixer\Fixer\FixerInterface; |
| 5 | +use PhpCsFixer\FixerDefinition\CodeSample; |
| 6 | +use PhpCsFixer\FixerDefinition\FixerDefinition; |
| 7 | +use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; |
| 8 | +use PhpCsFixer\Tokenizer\Tokens; |
| 9 | +use PhpCsFixer\Tokenizer\Token; |
| 10 | + |
| 11 | +/** |
| 12 | + * Main implementation taken from kubawerlos/php-cs-fixer-customere-fixers |
| 13 | + * Copyright (c) 2018 Kuba Werłos |
| 14 | + * |
| 15 | + * Slightly modified to work without the gazillion of composer dependencies |
| 16 | + * |
| 17 | + * Original: |
| 18 | + * https://github.com/kubawerlos/php-cs-fixer-custom-fixers/blob/master/src/Fixer/PhpdocSingleLineVarFixer.php |
| 19 | + * |
| 20 | + */ |
| 21 | +class PhpdocSingleLineVarFixer implements FixerInterface { |
| 22 | + |
| 23 | + public function getDefinition(): FixerDefinitionInterface { |
| 24 | + return new FixerDefinition( |
| 25 | + '`@var` annotation must be in single line when is the only content.', |
| 26 | + [new CodeSample('<?php |
| 27 | + /** |
| 28 | + * @var string |
| 29 | + */ |
| 30 | + ')] |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + public function isCandidate(Tokens $tokens): bool { |
| 35 | + return $tokens->isTokenKindFound(T_DOC_COMMENT); |
| 36 | + } |
| 37 | + |
| 38 | + public function isRisky(): bool { |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | + public function fix(\SplFileInfo $file, Tokens $tokens): void { |
| 43 | + foreach($tokens as $index => $token) { |
| 44 | + if (!$token->isGivenKind(T_DOC_COMMENT)) { |
| 45 | + continue; |
| 46 | + } |
| 47 | + if (\stripos($token->getContent(), '@var') === false) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + if (preg_match('#^/\*\*[\s\*]+(@var[^\r\n]+)[\s\*]*\*\/$#u', $token->getContent(), $matches) !== 1) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + $newContent = '/** ' . \rtrim($matches[1]) . ' */'; |
| 55 | + if ($newContent === $token->getContent()) { |
| 56 | + continue; |
| 57 | + } |
| 58 | + $tokens[$index] = new Token([T_DOC_COMMENT, $newContent]); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public function getPriority(): int { |
| 63 | + return 0; |
| 64 | + } |
| 65 | + |
| 66 | + public function getName(): string { |
| 67 | + return 'PharIo/phpdoc_single_line_var_fixer'; |
| 68 | + } |
| 69 | + |
| 70 | + public function supports(\SplFileInfo $file): bool { |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | +} |
0 commit comments