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\Php70\Rector\Switch_;
|
2018-12-15 10:27:49 +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\Stmt\Switch_;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\ValueObject\PhpVersionFeature;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2018-12-15 10:27:49 +00:00
|
|
|
/**
|
2022-05-12 19:17:07 +00:00
|
|
|
* @changelog https://3v4l.org/iGDVW
|
2021-04-10 18:47:17 +00:00
|
|
|
*
|
|
|
|
* @changelog https://wiki.php.net/rfc/switch.default.multiple https://stackoverflow.com/a/44000794/1348344 https://github.com/franzliedke/wp-mpdf/commit/9dc489215fbd1adcb514810653a73dea71db8e99#diff-2f1f4a51a2dd3a73ca034a48a67a2320L1373
|
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php70\Rector\Switch_\ReduceMultipleDefaultSwitchRector\ReduceMultipleDefaultSwitchRectorTest
|
2018-12-15 10:27:49 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class ReduceMultipleDefaultSwitchRector extends AbstractRector implements MinPhpVersionInterface
|
2018-12-15 10:27:49 +00:00
|
|
|
{
|
2021-09-24 14:11:08 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::NO_MULTIPLE_DEFAULT_SWITCH;
|
2021-09-24 14:11:08 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-12-15 10:27:49 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove first default switch, that is ignored', [new CodeSample(<<<'CODE_SAMPLE'
|
2018-12-15 10:27:49 +00:00
|
|
|
switch ($expr) {
|
|
|
|
default:
|
|
|
|
echo "Hello World";
|
|
|
|
|
|
|
|
default:
|
|
|
|
echo "Goodbye Moon!";
|
|
|
|
break;
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-12-15 10:27:49 +00:00
|
|
|
switch ($expr) {
|
|
|
|
default:
|
|
|
|
echo "Goodbye Moon!";
|
|
|
|
break;
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2018-12-15 10:27:49 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-12-15 10:27:49 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-12-15 10:27:49 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Switch_::class];
|
2018-12-15 10:27:49 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Switch_ $node
|
2018-12-15 10:27:49 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2018-12-15 10:27:49 +00:00
|
|
|
{
|
|
|
|
$defaultCases = [];
|
2023-03-29 20:33:15 +00:00
|
|
|
foreach ($node->cases as $key => $case) {
|
2023-03-23 23:21:34 +00:00
|
|
|
if ($case->cond instanceof Expr) {
|
2018-12-15 10:27:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-03-29 20:33:15 +00:00
|
|
|
$defaultCases[$key] = $case;
|
2018-12-15 10:27:49 +00:00
|
|
|
}
|
2023-06-05 13:42:47 +00:00
|
|
|
$defaultCaseCount = \count($defaultCases);
|
|
|
|
if ($defaultCaseCount < 2) {
|
2018-12-15 10:27:49 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-06-05 13:42:47 +00:00
|
|
|
foreach ($node->cases as $key => $case) {
|
|
|
|
if ($case->cond instanceof Expr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// remove previous default cases
|
|
|
|
if ($defaultCaseCount > 1) {
|
|
|
|
unset($node->cases[$key]);
|
|
|
|
--$defaultCaseCount;
|
|
|
|
}
|
2018-12-15 10:27:49 +00:00
|
|
|
}
|
2023-06-05 13:42:47 +00:00
|
|
|
return $node;
|
2018-12-15 10:27:49 +00:00
|
|
|
}
|
|
|
|
}
|