2021-01-24 11:23:50 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\CodeQuality\Rector\Switch_;
|
2021-01-24 11:23:50 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
2023-03-23 23:21:34 +00:00
|
|
|
use PhpParser\Node\Expr;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\BinaryOp\Identical;
|
2024-01-11 15:31:55 +00:00
|
|
|
use PhpParser\Node\Stmt;
|
|
|
|
use PhpParser\Node\Stmt\Break_;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\If_;
|
|
|
|
use PhpParser\Node\Stmt\Switch_;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Renaming\NodeManipulator\SwitchManipulator;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2021-01-24 11:23:50 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\Switch_\SingularSwitchToIfRector\SingularSwitchToIfRectorTest
|
2021-01-24 11:23:50 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class SingularSwitchToIfRector extends AbstractRector
|
2021-01-24 11:23:50 +00:00
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Renaming\NodeManipulator\SwitchManipulator
|
2021-01-24 11:23:50 +00:00
|
|
|
*/
|
|
|
|
private $switchManipulator;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(SwitchManipulator $switchManipulator)
|
2021-01-24 11:23:50 +00:00
|
|
|
{
|
|
|
|
$this->switchManipulator = $switchManipulator;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2021-01-24 11:23:50 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change switch with only 1 check to if', [new CodeSample(<<<'CODE_SAMPLE'
|
2021-01-24 11:23:50 +00:00
|
|
|
class SomeObject
|
|
|
|
{
|
|
|
|
public function run($value)
|
|
|
|
{
|
|
|
|
$result = 1;
|
|
|
|
switch ($value) {
|
|
|
|
case 100:
|
|
|
|
$result = 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2021-01-24 11:23:50 +00:00
|
|
|
class SomeObject
|
|
|
|
{
|
|
|
|
public function run($value)
|
|
|
|
{
|
|
|
|
$result = 1;
|
|
|
|
if ($value === 100) {
|
|
|
|
$result = 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2021-01-24 11:23:50 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2021-01-24 11:23:50 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2021-01-24 11:23:50 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Switch_::class];
|
2021-01-24 11:23:50 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Switch_ $node
|
2021-05-06 08:23:37 +00:00
|
|
|
* @return Node\Stmt[]|If_|null
|
2021-01-24 11:23:50 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node)
|
2021-01-24 11:23:50 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\count($node->cases) !== 1) {
|
2021-01-24 11:23:50 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$onlyCase = $node->cases[0];
|
|
|
|
// only default → basically unwrap
|
2023-03-23 23:21:34 +00:00
|
|
|
if (!$onlyCase->cond instanceof Expr) {
|
2024-01-11 15:31:55 +00:00
|
|
|
// remove default clause because it cause syntax error
|
2024-01-13 01:44:47 +00:00
|
|
|
return \array_filter($onlyCase->stmts, static function (Stmt $stmt) : bool {
|
|
|
|
return !$stmt instanceof Break_;
|
2024-01-11 15:31:55 +00:00
|
|
|
});
|
2021-01-24 11:23:50 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$if = new If_(new Identical($node->cond, $onlyCase->cond));
|
2021-01-24 11:23:50 +00:00
|
|
|
$if->stmts = $this->switchManipulator->removeBreakNodes($onlyCase->stmts);
|
|
|
|
return $if;
|
|
|
|
}
|
|
|
|
}
|