Skip to content

Commit

Permalink
[CodeQuality] Add Else and ElseIf on CompleteMissingIfElseBracketRect…
Browse files Browse the repository at this point in the history
…or (#5124)

* [CodeQuality] Add Else and ElseIf on CompleteMissingIfElseBracketRector

* fix

* fix end statement

* clean up

* clean up
  • Loading branch information
samsonasik committed Oct 5, 2023
1 parent 1966194 commit 96a1476
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 4 deletions.
@@ -0,0 +1,34 @@
<?php

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

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

?>
-----
<?php

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

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

?>
@@ -0,0 +1,34 @@
<?php

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

class MissingBracketOnElseIf
{
public function run($value, $value2)
{
if ($value) {
return 1;
} elseif ($value2)
return 2;
}
}

?>
-----
<?php

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

class MissingBracketOnElseIf
{
public function run($value, $value2)
{
if ($value) {
return 1;
} elseif ($value2) {
return 2;
}
}
}

?>
@@ -0,0 +1,17 @@
<?php

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

class SkipSpacedElseStmts
{
public function run($value, $value2)
{
if ($value) {
return 1;
} else {
$a = 1;

return $a;
}
}
}
Expand Up @@ -5,6 +5,8 @@
namespace Rector\CodeQuality\Rector\If_;

use PhpParser\Node;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\If_;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -52,11 +54,11 @@ public function run($value)
*/
public function getNodeTypes(): array
{
return [If_::class];
return [If_::class, ElseIf_::class, Else_::class];
}

/**
* @param If_ $node
* @param If_|ElseIf_|Else_ $node
*/
public function refactor(Node $node): ?Node
{
Expand All @@ -78,10 +80,15 @@ public function refactor(Node $node): ?Node
/**
* @param mixed[] $oldTokens
*/
private function isIfConditionFollowedByOpeningCurlyBracket(If_ $if, array $oldTokens): bool
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;
}

Expand All @@ -103,7 +110,7 @@ private function isIfConditionFollowedByOpeningCurlyBracket(If_ $if, array $oldT
return false;
}

private function isBareNewNode(If_ $if): bool
private function isBareNewNode(If_|ElseIf_|Else_ $if): bool
{
$originalNode = $if->getAttribute(AttributeKey::ORIGINAL_NODE);
if (! $originalNode instanceof Node) {
Expand Down

0 comments on commit 96a1476

Please sign in to comment.