Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\CodingStyle\Rector\If_\AlternativeIfToBracketRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class AlternativeIfToBracketRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\If_\AlternativeIfToBracketRector\Fixture;

function ifElseIf($a, $b)
{
if ($a) :
$x = 1;
elseif ($b) :
$x = 2;
else :
$x = 3;
endif;

return $x;
}
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\If_\AlternativeIfToBracketRector\Fixture;

function ifElseIf($a, $b)
{
if ($a) {
$x = 1;
} elseif ($b) {
$x = 2;
} else {
$x = 3;
}

return $x;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\If_\AlternativeIfToBracketRector\Fixture;

function nestedIf($a, $b)
{
if ($a) :
$x = 1;
else :
if ($b) :
$x = 2;
endif;
endif;

return $x;
}
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\If_\AlternativeIfToBracketRector\Fixture;

function nestedIf($a, $b)
{
if ($a) {
$x = 1;
} else if ($b) {
$x = 2;
}

return $x;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\If_\AlternativeIfToBracketRector\Fixture;

function simpleIf($value)
{
if ($value) :
$result = 1;
endif;

return $result;
}
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\If_\AlternativeIfToBracketRector\Fixture;

function simpleIf($value)
{
if ($value) {
$result = 1;
}

return $result;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\If_\AlternativeIfToBracketRector\Fixture;

function skipBracket($a, $b)
{
if ($a) {
$x = 1;
} elseif ($b) {
$x = 2;
} else {
$x = 3;
}

return $x;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\CodingStyle\Rector\If_\AlternativeIfToBracketRector;
use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withRules([AlternativeIfToBracketRector::class]);
102 changes: 102 additions & 0 deletions rules/CodingStyle/Rector/If_/AlternativeIfToBracketRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

declare(strict_types=1);

namespace Rector\CodingStyle\Rector\If_;

use PhpParser\Node;
use PhpParser\Node\Stmt\If_;
use PhpParser\Token;
use Rector\Contract\Rector\HTMLAverseRectorInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\CodingStyle\Rector\If_\AlternativeIfToBracketRector\AlternativeIfToBracketRectorTest
*/
final class AlternativeIfToBracketRector extends AbstractRector implements HTMLAverseRectorInterface
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change alternative if/elseif/else/endif syntax to bracket syntax', [
new CodeSample(
<<<'CODE_SAMPLE'
if ($value) :
$result = 1;
else :
$result = 2;
endif;
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
if ($value) {
$result = 1;
} else {
$result = 2;
}
CODE_SAMPLE
),
]);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [If_::class];
}

/**
* @param If_ $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isAlternativeSyntax($node)) {
return null;
}

// force reprint of the whole if/elseif/else chain with bracket syntax
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);

foreach ($node->elseifs as $elseif) {
$elseif->setAttribute(AttributeKey::ORIGINAL_NODE, null);
}

if ($node->else instanceof Node) {
$node->else->setAttribute(AttributeKey::ORIGINAL_NODE, null);
}

return $node;
}

private function isAlternativeSyntax(If_ $if): bool
{
$startTokenPos = $if->cond->getEndTokenPos();
if ($startTokenPos < 0) {
return false;
}

$oldTokens = $this->getFile()
->getOldTokens();

for ($i = $startTokenPos + 1; isset($oldTokens[$i]); ++$i) {
$token = $oldTokens[$i];
if (! $token instanceof Token) {
continue;
}

if ($token->text === ':') {
return true;
}

if ($token->text === '{') {
return false;
}
}

return false;
}
}
2 changes: 2 additions & 0 deletions src/Config/Level/CodeQualityLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
use Rector\CodeQuality\Rector\Ternary\TernaryEmptyArrayArrayDimFetchToCoalesceRector;
use Rector\CodeQuality\Rector\Ternary\TernaryImplodeToImplodeRector;
use Rector\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector;
use Rector\CodingStyle\Rector\If_\AlternativeIfToBracketRector;
use Rector\Contract\Rector\RectorInterface;
use Rector\Php52\Rector\Property\VarToPublicPropertyRector;
use Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector;
Expand Down Expand Up @@ -186,6 +187,7 @@ final class CodeQualityLevel
DisallowedEmptyRuleFixerRector::class,
LocallyCalledStaticMethodToNonStaticRector::class,
NumberCompareToMaxFuncCallRector::class,
AlternativeIfToBracketRector::class,
CompleteMissingIfElseBracketRector::class,
RemoveUselessIsObjectCheckRector::class,
ConvertStaticToSelfRector::class,
Expand Down
Loading