Skip to content

Commit

Permalink
[CodeQuality] Only check start from current Stmt token pos up until o…
Browse files Browse the repository at this point in the history
…pen parentheses on CompleteMissingIfElseBracketRector (#5127)

* [CodeQuality] Only check start from current Stmt token pos below on CompleteMissingIfElseBracketRector

* all good

* more fixture
  • Loading branch information
samsonasik committed Oct 6, 2023
1 parent bb7d028 commit af065e7
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\If_\CompleteMissingIfElseBracketRector\Fixture;

class AllNoBracket
{
public function run($value)
{
if ($value)
return 1;
elseif (rand(0, 1))
return 2;
else
return 3;
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\If_\CompleteMissingIfElseBracketRector\Fixture;

class AllNoBracket
{
public function run($value)
{
if ($value) {
return 1;
} elseif (rand(0, 1)) {
return 2;
} else {
return 3;
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\If_\CompleteMissingIfElseBracketRector\Fixture;

class MissingBracketOnElseIfAndElse
{
public function run($value)
{
if ($value) {
return 1;
} elseif (rand(0, 1))
return 2;
else
return 3;
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\If_\CompleteMissingIfElseBracketRector\Fixture;

class MissingBracketOnElseIfAndElse
{
public function run($value)
{
if ($value) {
return 1;
} elseif (rand(0, 1)) {
return 2;
}
else {
return 3;
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\If_\CompleteMissingIfElseBracketRector\Fixture;

class SkipDirectSemicolon
{
public function run($value)
{
if ($value);

echo 'something';
}
}
37 changes: 18 additions & 19 deletions rules/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,34 +82,33 @@ public function refactor(Node $node): ?Node
*/
private function isIfConditionFollowedByOpeningCurlyBracket(If_|ElseIf_|Else_ $if, array $oldTokens): bool
{
for ($i = $if->getStartTokenPos(); $i < $if->getEndTokenPos(); ++$i) {
if ($oldTokens[$i] !== ')') {
if ($oldTokens[$i] === ';') {
// all good
return true;
}

continue;
}
$startStmt = current($if->stmts);
if ($startStmt === false) {
return true;
}

// first closing bracket must be followed by curly opening brackets
// what is next token?
$nextToken = $oldTokens[$i + 1];
$startTokenPos = $startStmt->getStartTokenPos();
$i = $startTokenPos - 1;
$ifStartTokenPos = $if->getStartTokenPos();

if (is_array($nextToken) && trim((string) $nextToken[1]) === '') {
// next token is whitespace
$nextToken = $oldTokens[$i + 2];
while (isset($oldTokens[$i])) {
if ($i === $ifStartTokenPos) {
return false;
}

if (in_array($nextToken, ['{', ':'], true)) {
if ($oldTokens[$i] === ')') {
break;
}

if (in_array($oldTokens[$i], [':', '{'], true)) {
// all good
return true;
}

--$i;
}

$startStmt = current($if->stmts);
$lastStmt = end($if->stmts);
return $startStmt === false || $lastStmt === false;
return false;
}

private function isBareNewNode(If_|ElseIf_|Else_ $if): bool
Expand Down

0 comments on commit af065e7

Please sign in to comment.