From ecdb3812fe0e68dc24da3136c5741d463768880e Mon Sep 17 00:00:00 2001 From: Felix Sokoliuk Date: Tue, 19 Nov 2019 22:16:37 +0100 Subject: [PATCH] skip continue with argument >1 --- .../Switch_/ContinueToBreakInSwitchRector.php | 36 ++++++++++++++++++- .../skip_continue_with_argument.php.inc | 23 ++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 packages/Php52/tests/Rector/Switch_/ContinueToBreakInSwitchRector/Fixture/skip_continue_with_argument.php.inc diff --git a/packages/Php52/src/Rector/Switch_/ContinueToBreakInSwitchRector.php b/packages/Php52/src/Rector/Switch_/ContinueToBreakInSwitchRector.php index 9f8a1fe3c8e..0a70035319b 100644 --- a/packages/Php52/src/Rector/Switch_/ContinueToBreakInSwitchRector.php +++ b/packages/Php52/src/Rector/Switch_/ContinueToBreakInSwitchRector.php @@ -5,9 +5,13 @@ declare(strict_types=1); namespace Rector\Php52\Rector\Switch_; use PhpParser\Node; +use PhpParser\Node\Expr\Variable; +use PhpParser\Node\Scalar\LNumber; use PhpParser\Node\Stmt\Break_; use PhpParser\Node\Stmt\Continue_; use PhpParser\Node\Stmt\Switch_; +use PHPStan\Type\Constant\ConstantIntegerType; +use PHPStan\Type\ConstantType; use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; use Rector\RectorDefinition\RectorDefinition; @@ -69,7 +73,37 @@ PHP foreach ($node->cases as $case) { foreach ($case->stmts as $key => $caseStmt) { if ($caseStmt instanceof Continue_) { - $case->stmts[$key] = new Break_(); + $case->stmts[$key] = $this->processContinueStatement($caseStmt); + } + } + } + + return $node; + } + + private function processContinueStatement(Continue_ $node): Node + { + if ($node->num === null) { + return new Break_(); + } elseif ($node->num instanceof LNumber) { + if ($this->getValue($node->num) <= 1) { + return new Break_(); + } + } elseif ($node->num instanceof Variable) { + return $this->processVariableNum($node, $node->num); + } + + return $node; + } + + private function processVariableNum(Continue_ $node, Variable $numVariable): Node + { + $staticType = $this->getStaticType($numVariable); + + if ($staticType instanceof ConstantType) { + if ($staticType instanceof ConstantIntegerType) { + if ($staticType->getValue() <= 1) { + return new Break_(); } } } diff --git a/packages/Php52/tests/Rector/Switch_/ContinueToBreakInSwitchRector/Fixture/skip_continue_with_argument.php.inc b/packages/Php52/tests/Rector/Switch_/ContinueToBreakInSwitchRector/Fixture/skip_continue_with_argument.php.inc new file mode 100644 index 00000000000..91300980a45 --- /dev/null +++ b/packages/Php52/tests/Rector/Switch_/ContinueToBreakInSwitchRector/Fixture/skip_continue_with_argument.php.inc @@ -0,0 +1,23 @@ +