Skip to content

Commit

Permalink
[CodingStyle][EarlyReturn] Fix infinite if else on BinarySwitchToIfEl…
Browse files Browse the repository at this point in the history
…seRector+RemoveAlwaysElseRector (#5057)

* [CodingStyle][EarlyReturn] Fix infinite if else on BinarySwitchToIfElseRector+RemoveAlwaysElseRector

* [ci-review] Rector Rectify

* clean up

* clean up

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Sep 21, 2023
1 parent ad96b1f commit 69055b5
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/ProcessAnalyzer/RectifiedAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private function hasConsecutiveCreatedByRule(string $rectorClass, Node $node, ?N
$createdByRule = $createdByRuleNode->getAttribute(AttributeKey::CREATED_BY_RULE) ?? [];

if ($createdByRule === []) {
return false;
return ! $originalNode instanceof Node && count($node->getAttributes()) <= 1;
}

return end($createdByRule) === $rectorClass;
Expand Down
40 changes: 40 additions & 0 deletions tests/Issues/InfiniteIfElse/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\InfiniteIfElse\Fixture;

class Fixture
{
public static function getMethod(): int {
switch (mt_rand()) {
case 1:
return 10;
case 2:
return 20;
}
return 30;
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\InfiniteIfElse\Fixture;

class Fixture
{
public static function getMethod(): int {
if (mt_rand() == 1) {
return 10;
} elseif (mt_rand() == 2) {
return 20;
}
return 30;
}
}

?>
28 changes: 28 additions & 0 deletions tests/Issues/InfiniteIfElse/InfiniteIfElseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\InfiniteIfElse;

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

final class InfiniteIfElseTest 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';
}
}
14 changes: 14 additions & 0 deletions tests/Issues/InfiniteIfElse/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\CodingStyle\Rector\Switch_\BinarySwitchToIfElseRector;
use Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
BinarySwitchToIfElseRector::class,
RemoveAlwaysElseRector::class,
]);
};
2 changes: 1 addition & 1 deletion tests/Issues/SimplifyEmpty/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Fixture {

public function check(): bool
{
return $this->getString() !== null && $this->getString() !== '' && !(($this->getString2() === null || $this->getString2() === '') && !is_numeric($this->getString2()));
return !($this->getString() === null || $this->getString() === '' || (($this->getString2() === null || $this->getString2() === '') && !is_numeric($this->getString2())));
}
}

Expand Down

0 comments on commit 69055b5

Please sign in to comment.