Skip to content

Commit

Permalink
[CodeQuality] Handle missing bracket on If only on CompleteMissingIfE…
Browse files Browse the repository at this point in the history
…lseBracketRector (#5134)

* [CodeQuality] Handle missing bracket on If only on CompleteMissingIfElseBracketRector

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* Fixed 🎉

* fix use current laststmt

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user authored Oct 6, 2023
1 parent 96d0612 commit dbd374b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

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

class MissingBracketOnIfOnly
{
public function run($value)
{
if ($value)
return 1;
else {
return 2;
}
}
}

?>
-----
<?php

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

class MissingBracketOnIfOnly
{
public function run($value)
{
if ($value) {
return 1;
} else {
return 2;
}
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,14 @@ public function refactor(Node $node): ?Node
*/
private function isIfConditionFollowedByOpeningCurlyBracket(If_|ElseIf_|Else_ $if, array $oldTokens): bool
{
for ($i = $if->getStartTokenPos(); $i < $if->getEndTokenPos(); ++$i) {
$startStmt = current($if->stmts);
if (! $startStmt instanceof Stmt) {
return true;
}

/** @var Stmt $lastStmt */
$lastStmt = end($if->stmts);
for ($i = $if->getStartTokenPos(); $i < $lastStmt->getEndTokenPos(); ++$i) {
if (! isset($oldTokens[$i + 1])) {
break;
}
Expand Down Expand Up @@ -112,8 +119,7 @@ private function isIfConditionFollowedByOpeningCurlyBracket(If_|ElseIf_|Else_ $i
}
}

$startStmt = current($if->stmts);
return ! $startStmt instanceof Stmt;
return false;
}

private function isBareNewNode(If_|ElseIf_|Else_ $if): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,8 @@ private function shouldSkip(Node $node, Scope $scope): bool
return true;
}

if ($node instanceof ClassMethod
&& $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope)) {
return true;
}

return false;
return $node instanceof ClassMethod
&& $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope);
}

/**
Expand Down

0 comments on commit dbd374b

Please sign in to comment.