Skip to content

Commit

Permalink
bug #5593 SingleLineThrowFixer - fix handling anonymous classes (kuba…
Browse files Browse the repository at this point in the history
…werlos)

This PR was squashed before being merged into the 2.18 branch.

Discussion
----------

SingleLineThrowFixer - fix handling anonymous classes

Is this a right solution or `SingleLineThrowFixer` should not remove new lines from anonymous class definition?

Ping @nicolas-grekas for an opinion (as you are the person who originally [requested](#3988) this fixer).

Commits
-------

c73f60a SingleLineThrowFixer - fix handling anonymous classes
  • Loading branch information
keradus committed Apr 22, 2021
2 parents 57c52b6 + c73f60a commit 4353039
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 37 deletions.
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";
};

0 comments on commit 4353039

Please sign in to comment.