Skip to content

Commit

Permalink
bug #5654 SingleLineThrowFixer - fix for match expression (kubawerlos)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.18 branch.

Discussion
----------

SingleLineThrowFixer - fix for match expression

Fixes #5653

@lyrixx do you have time for review?

Commits
-------

b11b6b1 SingleLineThrowFixer - fix for match expression
  • Loading branch information
keradus committed Apr 24, 2021
2 parents 4353039 + b11b6b1 commit fe2e08f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
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()
};
'];
}
}
}

0 comments on commit fe2e08f

Please sign in to comment.