2019-10-13 05:59:52 +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\CodingStyle\Rector\Switch_;
|
2018-10-16 03:06:26 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Equal;
|
|
|
|
use PhpParser\Node\Stmt\Case_;
|
|
|
|
use PhpParser\Node\Stmt\Else_;
|
|
|
|
use PhpParser\Node\Stmt\ElseIf_;
|
|
|
|
use PhpParser\Node\Stmt\If_;
|
|
|
|
use PhpParser\Node\Stmt\Switch_;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\Renaming\NodeManipulator\SwitchManipulator;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodingStyle\Rector\Switch_\BinarySwitchToIfElseRector\BinarySwitchToIfElseRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class BinarySwitchToIfElseRector extends AbstractRector
|
2018-10-16 03:06:26 +00:00
|
|
|
{
|
2021-01-24 11:23:50 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +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
|
2018-10-16 03:06:26 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Changes switch with 2 options to if-else', [new CodeSample(<<<'CODE_SAMPLE'
|
2018-10-16 03:06:26 +00:00
|
|
|
switch ($foo) {
|
|
|
|
case 'my string':
|
|
|
|
$result = 'ok';
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
$result = 'not ok';
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-10-16 03:06:26 +00:00
|
|
|
if ($foo == 'my string') {
|
2021-07-28 12:55:48 +00:00
|
|
|
$result = 'ok';
|
2018-10-16 03:06:26 +00:00
|
|
|
} else {
|
|
|
|
$result = 'not ok';
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2018-10-16 03:06:26 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-10-16 03:06:26 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-10-16 03:06:26 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Switch_::class];
|
2018-10-16 03:06:26 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Switch_ $node
|
2018-10-16 03:06:26 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2018-10-16 03:06:26 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\count($node->cases) > 2) {
|
2018-10-16 03:06:26 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
/** @var Case_ $firstCase */
|
2021-05-09 20:15:43 +00:00
|
|
|
$firstCase = \array_shift($node->cases);
|
2019-01-14 18:21:23 +00:00
|
|
|
if ($firstCase->cond === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
$secondCase = \array_shift($node->cases);
|
2019-03-09 12:43:30 +00:00
|
|
|
// special case with empty first case → ||
|
|
|
|
$isFirstCaseEmpty = $firstCase->stmts === [];
|
|
|
|
if ($isFirstCaseEmpty && $secondCase !== null && $secondCase->cond !== null) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$else = new BooleanOr(new Equal($node->cond, $firstCase->cond), new Equal($node->cond, $secondCase->cond));
|
|
|
|
$ifNode = new If_($else);
|
2021-01-24 11:23:50 +00:00
|
|
|
$ifNode->stmts = $this->switchManipulator->removeBreakNodes($secondCase->stmts);
|
2019-03-09 12:43:30 +00:00
|
|
|
return $ifNode;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$ifNode = new If_(new Equal($node->cond, $firstCase->cond));
|
2021-01-24 11:23:50 +00:00
|
|
|
$ifNode->stmts = $this->switchManipulator->removeBreakNodes($firstCase->stmts);
|
2018-10-16 03:06:26 +00:00
|
|
|
// just one condition
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$secondCase instanceof Case_) {
|
2018-10-16 03:06:26 +00:00
|
|
|
return $ifNode;
|
|
|
|
}
|
2019-02-17 14:12:47 +00:00
|
|
|
if ($secondCase->cond !== null) {
|
2018-10-16 03:06:26 +00:00
|
|
|
// has condition
|
2022-06-07 08:22:29 +00:00
|
|
|
$equal = new Equal($node->cond, $secondCase->cond);
|
|
|
|
$ifNode->elseifs[] = new ElseIf_($equal, $this->switchManipulator->removeBreakNodes($secondCase->stmts));
|
2018-10-16 03:06:26 +00:00
|
|
|
} else {
|
|
|
|
// defaults
|
2022-06-07 08:22:29 +00:00
|
|
|
$ifNode->else = new Else_($this->switchManipulator->removeBreakNodes($secondCase->stmts));
|
2018-10-16 03:06:26 +00:00
|
|
|
}
|
|
|
|
return $ifNode;
|
|
|
|
}
|
|
|
|
}
|