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

bug: NoUnneededControlParenthesesFixer - Fix some curly close cases #6502

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/ControlStructure/NoAlternativeSyntaxFixer.php
Expand Up @@ -60,7 +60,7 @@ public function isCandidate(Tokens $tokens): bool
/**
* {@inheritdoc}
*
* Must run before BracesFixer, ElseifFixer, NoSuperfluousElseifFixer, NoUselessElseFixer, SwitchContinueToBreakFixer.
* Must run before BracesFixer, ElseifFixer, NoSuperfluousElseifFixer, NoUnneededControlParenthesesFixer, NoUselessElseFixer, SwitchContinueToBreakFixer.
*/
public function getPriority(): int
{
Expand Down
37 changes: 34 additions & 3 deletions src/Fixer/ControlStructure/NoUnneededControlParenthesesFixer.php
Expand Up @@ -52,7 +52,6 @@ final class NoUnneededControlParenthesesFixer extends AbstractFixer implements C
private const BEFORE_TYPES = [
';',
'{',
'}',
[T_OPEN_TAG],
[T_OPEN_TAG_WITH_ECHO],
[T_ECHO],
Expand Down Expand Up @@ -163,6 +162,7 @@ public function getDefinition(): FixerDefinitionInterface
* {@inheritdoc}
*
* Must run before ConcatSpaceFixer, NoTrailingWhitespaceFixer.
* Must run after NoAlternativeSyntaxFixer.
*/
public function getPriority(): int
{
Expand Down Expand Up @@ -408,7 +408,12 @@ private function isWrappedPartOfOperation(Tokens $tokens, int $beforeOpenIndex,
return true;
}

$beforeIsStatementOpen = $beforeToken->equalsAny(self::BEFORE_TYPES) || $beforeToken->isGivenKind(T_CASE);
if ($tokens[$beforeOpenIndex]->equals('}')) {
$beforeIsStatementOpen = !$this->closeCurlyBelongsToDynamicElement($tokens, $beforeOpenIndex);
} else {
$beforeIsStatementOpen = $beforeToken->equalsAny(self::BEFORE_TYPES) || $beforeToken->isGivenKind(T_CASE);
}

$afterIsStatementEnd = $afterToken->equalsAny([';', [T_CLOSE_TAG]]);

return
Expand Down Expand Up @@ -436,7 +441,15 @@ private function isSingleStatement(Tokens $tokens, int $beforeOpenIndex, int $af
return $tokens[$afterCloseIndex]->equalsAny([':', ';']); // `switch case`
}

return $tokens[$afterCloseIndex]->equalsAny([';', [T_CLOSE_TAG]]) && $tokens[$beforeOpenIndex]->equalsAny(self::BEFORE_TYPES);
if (!$tokens[$afterCloseIndex]->equalsAny([';', [T_CLOSE_TAG]])) {
return false;
}

if ($tokens[$beforeOpenIndex]->equals('}')) {
return !$this->closeCurlyBelongsToDynamicElement($tokens, $beforeOpenIndex);
}

return $tokens[$beforeOpenIndex]->equalsAny(self::BEFORE_TYPES);
}

private function isSimpleAssignment(Tokens $tokens, int $beforeOpenIndex, int $afterCloseIndex): bool
Expand Down Expand Up @@ -716,4 +729,22 @@ private function removeBrace(Tokens $tokens, int $index, bool $needsSpace): void
$tokens->clearTokenAndMergeSurroundingWhitespace($index);
}
}

private function closeCurlyBelongsToDynamicElement(Tokens $tokens, int $beforeOpenIndex): bool
{
$index = $tokens->findBlockStart(Tokens::BLOCK_TYPE_CURLY_BRACE, $beforeOpenIndex);
$index = $tokens->getPrevMeaningfulToken($index);

if ($tokens[$index]->isGivenKind(T_DOUBLE_COLON)) {
return true;
}

if ($tokens[$index]->equals(':')) {
$index = $tokens->getPrevTokenOfKind($index, [[T_CASE], '?']);

return !$tokens[$index]->isGivenKind(T_CASE);
}

return false;
}
}
1 change: 1 addition & 0 deletions tests/AutoReview/FixerFactoryTest.php
Expand Up @@ -517,6 +517,7 @@ private static function getFixersPriorityGraph(): array
'braces',
'elseif',
'no_superfluous_elseif',
'no_unneeded_control_parentheses',
'no_useless_else',
'switch_continue_to_break',
],
Expand Down
118 changes: 113 additions & 5 deletions tests/Fixer/ControlStructure/NoUnneededControlParenthesesFixerTest.php
Expand Up @@ -913,15 +913,15 @@ function foo() {
'<?php $a8 = [1, @( ($f.$b)() )];',
];

yield 'directly following `{` and `}`' => [
yield 'inside `{` and `}`' => [
'<?php
while(foo()) { bar(); }foo();
while(bar()){foo();};
while(foo()) { bar(); }
while(bar()){foo1();};
if($a){foo();}
',
'<?php
while(foo()) { (bar()); }(foo());
while(bar()){(foo());};
while(foo()) { (bar()); }
while(bar()){(foo1());};
if($a){(foo());}
',
];
Expand Down Expand Up @@ -1207,6 +1207,79 @@ function foo() {
];
}

yield 'after `}`' => [
'<?php
while(foo()){} ++$i;
for(;;){}++$i;
foreach ($a as $b){}++$i;

if (foo()) {}++$i;
if (foo()) {} else {}++$i;
if (foo()) {} elseif(bar()) {}++$i;

switch(foo()){case 1: echo 1;}++$i;
switch (foo()){
case 1: {}++$i; break;
}

function Bar(): array {
$i++;
return [];
}
function Foo1(){}++$i;
function & Foo2(){}++$i;

class A{}++$i;
class A1 extends BD{}++$i;
class A2 extends BD implements CE{}++$i;
class A3 extends BD implements CE,DE{}++$i;

interface B{}++$i;
interface B1 extends A{}++$i;
interface B2 extends A,B{}++$i;

trait X{}++$i;

try{}catch(E $e){}$i++;
try {} finally {}++$i;
',
'<?php
while(foo()){} (++$i);
for(;;){}(++$i);
foreach ($a as $b){}(++$i);

if (foo()) {}(++$i);
if (foo()) {} else {}(++$i);
if (foo()) {} elseif(bar()) {}(++$i);

switch(foo()){case 1: echo 1;}(++$i);
switch (foo()){
case 1: {}(++$i); break;
}

function Bar(): array {
($i++);
return [];
}
function Foo1(){}(++$i);
function & Foo2(){}(++$i);

class A{}(++$i);
class A1 extends BD{}(++$i);
class A2 extends BD implements CE{}(++$i);
class A3 extends BD implements CE,DE{}(++$i);

interface B{}(++$i);
interface B1 extends A{}(++$i);
interface B2 extends A,B{}(++$i);

trait X{}(++$i);

try{}catch(E $e){}($i++);
try {} finally {}(++$i);
',
];

yield 'do not fix' => [
'<?php
$b = ($a+=$c);
Expand Down Expand Up @@ -1331,6 +1404,26 @@ final class Foo { public static function bar() { return new static(); } }
__halt_compiler();
',
];

yield 'do not fix ' => [
'<?php
SomeClass::{$method}(
$variableA,
$variableB
);',
];

yield 'do not fix 2' => [
'<?php SomeClass::{$method}(2) + 1;',
];

yield 'do not fix 3' => [
'<?php $object::{$method}(...$args);',
];

yield 'alternative syntax is not completely supported' => [
'<?php if ($a):(++$i); endif;',
];
}

/**
Expand Down Expand Up @@ -1609,5 +1702,20 @@ enum Foo: string
$fn9 = fn & (): D & E => (new F());
',
];

yield [
'<?php
enum Suit
{
case Hearts;
}$i++;
',
'<?php
enum Suit
{
case Hearts;
}($i++);
',
];
}
}
@@ -0,0 +1,11 @@
--TEST--
Integration of fixers: no_alternative_syntax,no_unneeded_control_parentheses.
--RULESET--
{"no_alternative_syntax": true, "no_unneeded_control_parentheses": {"statements": ["others"]}}
--EXPECT--
<?php
if ($a) { ++$i; }

--INPUT--
<?php
if ($a):(++$i); endif;