diff --git a/src/Fixer/FunctionNotation/SingleLineThrowFixer.php b/src/Fixer/FunctionNotation/SingleLineThrowFixer.php index 3d2e3ee37a1..439a4a4ece6 100644 --- a/src/Fixer/FunctionNotation/SingleLineThrowFixer.php +++ b/src/Fixer/FunctionNotation/SingleLineThrowFixer.php @@ -39,6 +39,11 @@ final class SingleLineThrowFixer extends AbstractFixer */ const REMOVE_WHITESPACE_BEFORE_TOKENS = [')', ']', ',', ';']; + /** + * @internal + */ + const THROW_END_TOKENS = [';', '(', '{', '}']; + /** * {@inheritdoc} */ @@ -82,14 +87,14 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens) } /** @var int $endCandidateIndex */ - $endCandidateIndex = $tokens->getNextTokenOfKind($index, [';', '(', '{']); + $endCandidateIndex = $tokens->getNextTokenOfKind($index, self::THROW_END_TOKENS); while ($tokens[$endCandidateIndex]->equals('(')) { $closingBraceIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $endCandidateIndex); - $endCandidateIndex = $tokens->getNextTokenOfKind($closingBraceIndex, [';', '(', '{']); + $endCandidateIndex = $tokens->getNextTokenOfKind($closingBraceIndex, self::THROW_END_TOKENS); } - $this->trimNewLines($tokens, $index, $endCandidateIndex); + $this->trimNewLines($tokens, $index, $tokens->getPrevMeaningfulToken($endCandidateIndex)); } } diff --git a/tests/Fixer/FunctionNotation/SingleLineThrowFixerTest.php b/tests/Fixer/FunctionNotation/SingleLineThrowFixerTest.php index a1120926310..06580d62bdb 100644 --- a/tests/Fixer/FunctionNotation/SingleLineThrowFixerTest.php +++ b/tests/Fixer/FunctionNotation/SingleLineThrowFixerTest.php @@ -246,7 +246,8 @@ function if (\PHP_VERSION_ID >= 70000) { yield [ - ' $function->one(), + 2 => $function->two(), + default => throw new \NotOneOrTwo() + }; + ']; } } }