Skip to content

Commit

Permalink
[Php52][Php70] Handle ContinueToBreakInSwitchRector + BreakNotInLoopO…
Browse files Browse the repository at this point in the history
…rSwitchToReturnRector (#5217)

* [Php52][Php70] Handle ContinueToBreakInSwitchRector + BreakNotInLoopOrSwitchToReturnRector

* Fixed 🎉

* Fixed 🎉

* Fixed 🎉

* Fixed 🎉

* rollback RectifiedAnalyzer

* Fix
  • Loading branch information
samsonasik committed Oct 31, 2023
1 parent 95d0331 commit 0d55d15
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\Node\Stmt\Unset_;
use PhpParser\Node\Stmt\While_;
use PhpParser\NodeTraverser;
Expand All @@ -41,7 +42,7 @@ public function __construct(

public function enterNode(Node $node): ?Node
{
if ($node instanceof For_ || $node instanceof Foreach_ || $node instanceof While_ || $node instanceof Do_) {
if ($node instanceof For_ || $node instanceof Foreach_ || $node instanceof While_ || $node instanceof Do_ || $node instanceof Switch_) {
$this->processContextInLoop($node);
return null;
}
Expand Down Expand Up @@ -130,7 +131,7 @@ private function processContextInIf(If_|Else_|ElseIf_ $node): void
}
}

private function processContextInLoop(For_|Foreach_|While_|Do_ $node): void
private function processContextInLoop(For_|Foreach_|While_|Do_|Switch_ $node): void
{
if ($node instanceof Foreach_) {
if ($node->keyVar instanceof Variable) {
Expand All @@ -140,8 +141,9 @@ private function processContextInLoop(For_|Foreach_|While_|Do_ $node): void
$node->valueVar->setAttribute(AttributeKey::IS_VARIABLE_LOOP, true);
}

$stmts = $node instanceof Switch_ ? $node->cases : $node->stmts;
$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
$node->stmts,
$stmts,
static function (Node $subNode): ?int {
if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) {
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
Expand Down
28 changes: 28 additions & 0 deletions tests/Issues/ContinueToBreakSwitch/ContinueToBreakSwitchTest.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\ContinueToBreakSwitch;

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

final class ContinueToBreakSwitchTest 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';
}
}
49 changes: 49 additions & 0 deletions tests/Issues/ContinueToBreakSwitch/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

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

final class Fixture
{
public function process() {
while ($something = \random_int(0, 4)) {
switch ($something) {
case 1:
return $something;
case 2:
continue;
default:
\error_log('error');
throw new \Exception('error');
}
}

return null;
}
}

?>
-----
<?php

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

final class Fixture
{
public function process() {
while ($something = \random_int(0, 4)) {
switch ($something) {
case 1:
return $something;
case 2:
break;
default:
\error_log('error');
throw new \Exception('error');
}
}

return null;
}
}

?>
14 changes: 14 additions & 0 deletions tests/Issues/ContinueToBreakSwitch/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\Php52\Rector\Switch_\ContinueToBreakInSwitchRector;
use Rector\Php70\Rector\Break_\BreakNotInLoopOrSwitchToReturnRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
BreakNotInLoopOrSwitchToReturnRector::class,
ContinueToBreakInSwitchRector::class,
]);
};

0 comments on commit 0d55d15

Please sign in to comment.