Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SingleLineThrowFixer - fix for match expression #5654

Merged
merged 1 commit into from Apr 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/Fixer/FunctionNotation/SingleLineThrowFixer.php
Expand Up @@ -39,6 +39,11 @@ final class SingleLineThrowFixer extends AbstractFixer
*/
const REMOVE_WHITESPACE_BEFORE_TOKENS = [')', ']', ',', ';'];

/**
* @internal
*/
const THROW_END_TOKENS = [';', '(', '{', '}'];

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -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));
}
}

Expand Down
14 changes: 12 additions & 2 deletions tests/Fixer/FunctionNotation/SingleLineThrowFixerTest.php
Expand Up @@ -246,7 +246,8 @@ function

if (\PHP_VERSION_ID >= 70000) {
yield [
'<?php throw new class() extends Exception {
'<?php throw new class() extends Exception
{
protected $message = "Custom message";
}
;',
Expand All @@ -260,7 +261,8 @@ function
];

yield [
'<?php throw new class extends Exception {
'<?php throw new class extends Exception
{
protected $message = "Custom message";
}
;',
Expand All @@ -283,6 +285,14 @@ function
"Foo"
);',
];

yield ['<?php
match ($number) {
1 => $function->one(),
2 => $function->two(),
default => throw new \NotOneOrTwo()
};
'];
}
}
}