Skip to content

Commit

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

This reverts commit af065e7.
  • Loading branch information
samsonasik committed Oct 6, 2023
1 parent af065e7 commit 12184ca
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 108 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

37 changes: 19 additions & 18 deletions rules/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector.php
Expand Up @@ -82,33 +82,34 @@ public function refactor(Node $node): ?Node
*/
private function isIfConditionFollowedByOpeningCurlyBracket(If_|ElseIf_|Else_ $if, array $oldTokens): bool
{
$startStmt = current($if->stmts);
if ($startStmt === false) {
return true;
}

$startTokenPos = $startStmt->getStartTokenPos();
$i = $startTokenPos - 1;
$ifStartTokenPos = $if->getStartTokenPos();

while (isset($oldTokens[$i])) {
if ($i === $ifStartTokenPos) {
return false;
for ($i = $if->getStartTokenPos(); $i < $if->getEndTokenPos(); ++$i) {
if ($oldTokens[$i] !== ')') {
if ($oldTokens[$i] === ';') {
// all good
return true;
}

continue;
}

if ($oldTokens[$i] === ')') {
break;
// first closing bracket must be followed by curly opening brackets
// what is next token?
$nextToken = $oldTokens[$i + 1];

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

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

--$i;
}

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

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

0 comments on commit 12184ca

Please sign in to comment.