|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of PHP CS Fixer. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * Dariusz Rumiński <dariusz.ruminski@gmail.com> |
| 8 | + * |
| 9 | + * This source file is subject to the MIT license that is bundled |
| 10 | + * with this source code in the file LICENSE. |
| 11 | + */ |
| 12 | + |
| 13 | +namespace PhpCsFixer\Fixer\FunctionNotation; |
| 14 | + |
| 15 | +use PhpCsFixer\AbstractFixer; |
| 16 | +use PhpCsFixer\FixerDefinition\CodeSample; |
| 17 | +use PhpCsFixer\FixerDefinition\FixerDefinition; |
| 18 | +use PhpCsFixer\Preg; |
| 19 | +use PhpCsFixer\Tokenizer\Token; |
| 20 | +use PhpCsFixer\Tokenizer\Tokens; |
| 21 | + |
| 22 | +/** |
| 23 | + * @author Kuba Werłos <werlos@gmail.com> |
| 24 | + */ |
| 25 | +final class SingleLineThrowFixer extends AbstractFixer |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @internal |
| 29 | + */ |
| 30 | + const REMOVE_WHITESPACE_AFTER_TOKENS = ['[']; |
| 31 | + |
| 32 | + /** |
| 33 | + * @internal |
| 34 | + */ |
| 35 | + const REMOVE_WHITESPACE_AROUND_TOKENS = ['(', [T_OBJECT_OPERATOR], [T_DOUBLE_COLON]]; |
| 36 | + |
| 37 | + /** |
| 38 | + * @internal |
| 39 | + */ |
| 40 | + const REMOVE_WHITESPACE_BEFORE_TOKENS = [')', ']', ',', ';']; |
| 41 | + |
| 42 | + /** |
| 43 | + * {@inheritdoc} |
| 44 | + */ |
| 45 | + public function getDefinition() |
| 46 | + { |
| 47 | + return new FixerDefinition( |
| 48 | + 'Throwing exception must be done in single line.', |
| 49 | + [ |
| 50 | + new CodeSample("<?php\nthrow new Exception(\n 'Error',\n 500\n);\n"), |
| 51 | + ] |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * {@inheritdoc} |
| 57 | + */ |
| 58 | + public function isCandidate(Tokens $tokens) |
| 59 | + { |
| 60 | + return $tokens->isTokenKindFound(T_THROW); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * {@inheritdoc} |
| 65 | + */ |
| 66 | + public function getPriority() |
| 67 | + { |
| 68 | + // must be fun before ConcatSpaceFixer |
| 69 | + return 1; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * {@inheritdoc} |
| 74 | + */ |
| 75 | + protected function applyFix(\SplFileInfo $file, Tokens $tokens) |
| 76 | + { |
| 77 | + for ($index = 0, $count = $tokens->count(); $index < $count; ++$index) { |
| 78 | + if (!$tokens[$index]->isGivenKind(T_THROW)) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + /** @var int $openingBraceCandidateIndex */ |
| 83 | + $openingBraceCandidateIndex = $tokens->getNextTokenOfKind($index, [';', '(']); |
| 84 | + |
| 85 | + while ($tokens[$openingBraceCandidateIndex]->equals('(')) { |
| 86 | + /** @var int $closingBraceIndex */ |
| 87 | + $closingBraceIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openingBraceCandidateIndex); |
| 88 | + /** @var int $openingBraceCandidateIndex */ |
| 89 | + $openingBraceCandidateIndex = $tokens->getNextTokenOfKind($closingBraceIndex, [';', '(']); |
| 90 | + } |
| 91 | + |
| 92 | + $this->trimNewLines($tokens, $index, $openingBraceCandidateIndex); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @param int $startIndex |
| 98 | + * @param int $endIndex |
| 99 | + */ |
| 100 | + private function trimNewLines(Tokens $tokens, $startIndex, $endIndex) |
| 101 | + { |
| 102 | + for ($index = $startIndex; $index < $endIndex; ++$index) { |
| 103 | + $content = $tokens[$index]->getContent(); |
| 104 | + |
| 105 | + if ($tokens[$index]->isGivenKind(T_COMMENT)) { |
| 106 | + if (0 === strpos($content, '//')) { |
| 107 | + $content = '/*'.substr($content, 2).' */'; |
| 108 | + $tokens->clearAt($index + 1); |
| 109 | + } elseif (0 === strpos($content, '#')) { |
| 110 | + $content = '/*'.substr($content, 1).' */'; |
| 111 | + $tokens->clearAt($index + 1); |
| 112 | + } elseif (false !== Preg::match('/\R/', $content)) { |
| 113 | + $content = Preg::replace('/\R/', ' ', $content); |
| 114 | + } |
| 115 | + $tokens[$index] = new Token([T_COMMENT, $content]); |
| 116 | + |
| 117 | + continue; |
| 118 | + } |
| 119 | + |
| 120 | + if (!$tokens[$index]->isGivenKind(T_WHITESPACE)) { |
| 121 | + continue; |
| 122 | + } |
| 123 | + |
| 124 | + if (0 === Preg::match('/\R/', $content)) { |
| 125 | + continue; |
| 126 | + } |
| 127 | + |
| 128 | + $prevIndex = $tokens->getNonEmptySibling($index, -1); |
| 129 | + if ($tokens[$prevIndex]->equalsAny(array_merge(self::REMOVE_WHITESPACE_AFTER_TOKENS, self::REMOVE_WHITESPACE_AROUND_TOKENS))) { |
| 130 | + $tokens->clearAt($index); |
| 131 | + |
| 132 | + continue; |
| 133 | + } |
| 134 | + |
| 135 | + $nextIndex = $tokens->getNonEmptySibling($index, 1); |
| 136 | + if ($tokens[$nextIndex]->equalsAny(array_merge(self::REMOVE_WHITESPACE_AROUND_TOKENS, self::REMOVE_WHITESPACE_BEFORE_TOKENS))) { |
| 137 | + if (!$tokens[$prevIndex]->isGivenKind(T_FUNCTION)) { |
| 138 | + $tokens->clearAt($index); |
| 139 | + |
| 140 | + continue; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + $tokens[$index] = new Token([T_WHITESPACE, ' ']); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments