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 handling anonymous classes #5593

Merged
merged 1 commit into from Apr 22, 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
2 changes: 1 addition & 1 deletion src/Fixer/Basic/BracesFixer.php
Expand Up @@ -126,7 +126,7 @@ public function bar($baz)
* {@inheritdoc}
*
* Must run before ArrayIndentationFixer, MethodArgumentSpaceFixer, MethodChainingIndentationFixer.
* Must run after ClassAttributesSeparationFixer, ClassDefinitionFixer, ElseifFixer, LineEndingFixer, MethodSeparationFixer, NoAlternativeSyntaxFixer, NoEmptyStatementFixer, NoUselessElseFixer, SingleSpaceAfterConstructFixer, SingleTraitInsertPerStatementFixer.
* Must run after ClassAttributesSeparationFixer, ClassDefinitionFixer, ElseifFixer, LineEndingFixer, MethodSeparationFixer, NoAlternativeSyntaxFixer, NoEmptyStatementFixer, NoUselessElseFixer, SingleLineThrowFixer, SingleSpaceAfterConstructFixer, SingleTraitInsertPerStatementFixer.
*/
public function getPriority()
{
Expand Down
19 changes: 8 additions & 11 deletions src/Fixer/FunctionNotation/SingleLineThrowFixer.php
Expand Up @@ -63,12 +63,12 @@ public function isCandidate(Tokens $tokens)
/**
* {@inheritdoc}
*
* Must run before ConcatSpaceFixer.
* Must run before BracesFixer, ConcatSpaceFixer.
*/
public function getPriority()
{
// must be fun before ConcatSpaceFixer
return 1;
return 36;
}

/**
Expand All @@ -81,18 +81,15 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
continue;
}

/** @var int $openingBraceCandidateIndex */
$openingBraceCandidateIndex = $tokens->getNextTokenOfKind($index, [';', '(']);
/** @var int $endCandidateIndex */
$endCandidateIndex = $tokens->getNextTokenOfKind($index, [';', '(', '{']);

while ($tokens[$openingBraceCandidateIndex]->equals('(')) {
/** @var int $closingBraceIndex */
$closingBraceIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openingBraceCandidateIndex);

/** @var int $openingBraceCandidateIndex */
$openingBraceCandidateIndex = $tokens->getNextTokenOfKind($closingBraceIndex, [';', '(']);
while ($tokens[$endCandidateIndex]->equals('(')) {
$closingBraceIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $endCandidateIndex);
$endCandidateIndex = $tokens->getNextTokenOfKind($closingBraceIndex, [';', '(', '{']);
}

$this->trimNewLines($tokens, $index, $openingBraceCandidateIndex);
$this->trimNewLines($tokens, $index, $endCandidateIndex);
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/AutoReview/FixerFactoryTest.php
Expand Up @@ -266,6 +266,7 @@ public function provideFixersPriorityCases()
[$fixers['single_import_per_statement'], $fixers['no_singleline_whitespace_before_semicolons']],
[$fixers['single_import_per_statement'], $fixers['no_unused_imports']],
[$fixers['single_import_per_statement'], $fixers['space_after_semicolon']],
[$fixers['single_line_throw'], $fixers['braces']],
[$fixers['single_line_throw'], $fixers['concat_space']],
[$fixers['single_space_after_construct'], $fixers['braces']],
[$fixers['single_space_after_construct'], $fixers['function_declaration']],
Expand Down
80 changes: 55 additions & 25 deletions tests/Fixer/FunctionNotation/SingleLineThrowFixerTest.php
Expand Up @@ -46,20 +46,20 @@ public function provideFixCases()
"Foo"
);'];

yield ['<?php throw new Exception("Foo", 0);'];
yield ['<?php throw new Exception("Foo.", 0);'];

yield [
'<?php throw new Exception("Foo", 0);',
'<?php throw new Exception("Foo.", 0);',
'<?php throw new Exception(
"Foo",
"Foo.",
0
);',
];

yield [
'<?php throw new Exception("Foo" . "Bar");',
'<?php throw new Exception("Foo." . "Bar");',
'<?php throw new Exception(
"Foo"
"Foo."
.
"Bar"
);',
Expand All @@ -75,24 +75,24 @@ public function provideFixCases()
];

yield [
'<?php throw new Exception(sprintf("Error with number %s", 42));',
'<?php throw new Exception(sprintf(\'Error with number "%s".\', 42));',
'<?php throw new Exception(sprintf(
"Error with number %s",
\'Error with number "%s".\',
42
));',
];

yield [
'<?php throw new SomeVendor\\Exception("Foo");',
'<?php throw new SomeVendor\\Exception("Foo.");',
'<?php throw new SomeVendor\\Exception(
"Foo"
"Foo."
);',
];

yield [
'<?php throw new \SomeVendor\\Exception("Foo");',
'<?php throw new \SomeVendor\\Exception("Foo.");',
'<?php throw new \SomeVendor\\Exception(
"Foo"
"Foo."
);',
];

Expand Down Expand Up @@ -141,21 +141,21 @@ function
];

yield [
'<?php throw new Exception("Foo", 0);',
'<?php throw new Exception("Foo.", 0);',
'<?php throw
new
Exception
(
"Foo"
"Foo."
,
0
);',
];

yield [
'<?php throw new $exceptionName("Foo");',
'<?php throw new $exceptionName("Foo.");',
'<?php throw new $exceptionName(
"Foo"
"Foo."
);',
];

Expand All @@ -167,15 +167,15 @@ function
];

yield [
'<?php throw clone $exceptionName("Foo");',
'<?php throw clone $exceptionName("Foo.");',
'<?php throw clone $exceptionName(
"Foo"
"Foo."
);',
];

yield [
'<?php throw new WeirdException("Foo", -20, "An elephant", 1, 2, 3, 4, 5, 6, 7, 8);',
'<?php throw new WeirdException("Foo", -20, "An elephant",
'<?php throw new WeirdException("Foo.", -20, "An elephant", 1, 2, 3, 4, 5, 6, 7, 8);',
'<?php throw new WeirdException("Foo.", -20, "An elephant",

1,
2,
Expand All @@ -186,20 +186,20 @@ function
yield [
'<?php
if ($foo) {
throw new Exception("It is foo", 1);
throw new Exception("It is foo.", 1);
} else {
throw new \Exception("It is not foo", 0);
throw new \Exception("It is not foo.", 0);
}
',
'<?php
if ($foo) {
throw new Exception(
"It is foo",
"It is foo.",
1
);
} else {
throw new \Exception(
"It is not foo", 0
"It is not foo.", 0
);
}
',
Expand Down Expand Up @@ -238,12 +238,42 @@ function
];

yield [
"<?php throw new Exception('a'. 1);",
"<?php throw new Exception('a'.
"<?php throw new Exception('Message.'. 1);",
"<?php throw new Exception('Message.'.
1
);",
];

if (\PHP_VERSION_ID >= 70000) {
yield [
'<?php throw new class() extends Exception {
protected $message = "Custom message";
}
;',
'<?php throw
new class()
extends Exception
{
protected $message = "Custom message";
}
;',
];

yield [
'<?php throw new class extends Exception {
protected $message = "Custom message";
}
;',
'<?php throw
new class
extends Exception
{
protected $message = "Custom message";
}
;',
];
}

if (\PHP_VERSION_ID >= 80000) {
yield [
'<?php throw $this?->getExceptionFactory()?->createAnException("Foo");',
Expand Down
20 changes: 20 additions & 0 deletions tests/Fixtures/Integration/priority/single_line_throw,braces.test
@@ -0,0 +1,20 @@
--TEST--
Integration of fixers: single_line_throw,braces.
--RULESET--
{"braces": true, "single_line_throw": true}
--REQUIREMENTS--
{"php": 70000}
--EXPECT--
<?php
throw new class() extends Exception {
protected $message = "Custom message";
};

--INPUT--
<?php
throw
new class()
extends Exception
{
protected $message = "Custom message";
};