Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Php52] Handle in deep else on ContinueToBreakInSwitchRector #6123

Merged
merged 1 commit into from
Jul 6, 2024
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,73 @@
<?php

class InElseStmt
{
function processNotification(array $notifications, string $tableType)
{
foreach ($notifications as $notification) {
switch ($tableType) {
case ($tableType == 'type1'):
{
if (rand(0,1)) {

if (rand(0,1)) {

if (rand(0,1)) {

}
}
} else {
if (rand(0,1)) {

if (rand(0,1)) {
continue;
}
}
}
}
case ($tableType == 'type2'):
{
}
}
}
}
}

?>
-----
<?php

class InElseStmt
{
function processNotification(array $notifications, string $tableType)
{
foreach ($notifications as $notification) {
switch ($tableType) {
case ($tableType == 'type1'):
{
if (rand(0,1)) {

if (rand(0,1)) {

if (rand(0,1)) {

}
}
} else {
if (rand(0,1)) {

if (rand(0,1)) {
break;
}
}
}
}
case ($tableType == 'type2'):
{
}
}
}
}
}

?>
76 changes: 32 additions & 44 deletions rules/Php52/Rector/Switch_/ContinueToBreakInSwitchRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt;
Expand All @@ -18,6 +19,7 @@
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\Node\Stmt\While_;
use PhpParser\NodeTraverser;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\ConstantType;
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
Expand Down Expand Up @@ -108,58 +110,44 @@ public function refactor(Node $node): ?Switch_

private function processContinueStatement(Stmt|StmtsAwareInterface $stmt): void
{
if ($stmt instanceof Class_
|| $stmt instanceof Function_
|| $stmt instanceof While_
|| $stmt instanceof Do_
|| $stmt instanceof Foreach_
|| $stmt instanceof For_
) {
return;
}

if (! $stmt instanceof StmtsAwareInterface) {
return;
}

if ($stmt->stmts === null) {
return;
}

foreach ($stmt->stmts as $key => $stmtStmt) {
if ($stmtStmt instanceof StmtsAwareInterface) {
$this->processContinueStatement($stmtStmt);
continue;
}
$this->traverseNodesWithCallable(
$stmt,
function (Node $subNode): null|int|Break_ {
if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) {
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}

if (! $stmtStmt instanceof Continue_) {
continue;
}
// continue is belong to loop
if ($subNode instanceof Foreach_ || $subNode instanceof While_ || $subNode instanceof Do_ || $subNode instanceof For_) {
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}

if (! $stmtStmt->num instanceof Expr) {
$this->hasChanged = true;
$stmt->stmts[$key] = new Break_();
continue;
}
if (! $subNode instanceof Continue_) {
return null;
}

if ($stmtStmt->num instanceof LNumber) {
$continueNumber = $this->valueResolver->getValue($stmtStmt->num);
if ($continueNumber <= 1) {
if (! $subNode->num instanceof Expr) {
$this->hasChanged = true;
$stmt->stmts[$key] = new Break_();

continue;
return new Break_();
}
} elseif ($stmtStmt->num instanceof Variable) {
$continue = $this->processVariableNum($stmtStmt, $stmtStmt->num);
if ($continue instanceof Continue_) {
continue;

if ($subNode->num instanceof LNumber) {
$continueNumber = $this->valueResolver->getValue($subNode->num);
if ($continueNumber <= 1) {
$this->hasChanged = true;
return new Break_();
}
} elseif ($subNode->num instanceof Variable) {
$processVariableNum = $this->processVariableNum($subNode, $subNode->num);
if ($processVariableNum instanceof Break_) {
$this->hasChanged = true;
return $processVariableNum;
}
}

$this->hasChanged = true;
$stmt->stmts[$key] = new Break_();
return null;
}
}
);
}

private function processVariableNum(Continue_ $continue, Variable $numVariable): Continue_ | Break_
Expand Down
Loading