rector/rules/DowngradePhp71/Rector/TryCatch/DowngradePipeToMultiCatchExceptionRector.php
Tomas Votruba a389839c5c
[CI] Downgrade Rector to PHP 7.1 + prefix it (#5880)
* [downgrade] simplify to 2 configs

* rename downgrade_packages.sh to downgrade_rector_to_php71.sh

* add test case stub

* [CI] add linter for php code

* [CI] update tagged workflow

* update for covariance

* [ci-review] Rector Rectify

Co-authored-by: kaizen-ci <info@kaizen-ci.org>
2021-03-18 02:48:44 +01:00

84 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Rector\DowngradePhp71\Rector\TryCatch;
use PhpParser\Node;
use PhpParser\Node\Stmt\Catch_;
use PhpParser\Node\Stmt\TryCatch;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DowngradePhp71\Rector\TryCatch\DowngradePipeToMultiCatchExceptionRector\DowngradePipeToMultiCatchExceptionRectorTest
*/
final class DowngradePipeToMultiCatchExceptionRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Downgrade single one | separated to multi catch exception',
[
new CodeSample(
<<<'CODE_SAMPLE'
try {
// Some code...
} catch (ExceptionType1 | ExceptionType2 $exception) {
$sameCode;
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
try {
// Some code...
} catch (ExceptionType1 $exception) {
$sameCode;
} catch (ExceptionType2 $exception) {
$sameCode;
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [TryCatch::class];
}
/**
* @param TryCatch $node
*/
public function refactor(Node $node): ?Node
{
$originalCatches = $node->catches;
foreach ($node->catches as $key => $catch) {
if (count($catch->types) === 1) {
continue;
}
$types = $catch->types;
$node->catches[$key]->types = [$catch->types[0]];
foreach ($types as $keyCatchType => $catchType) {
if ($keyCatchType === 0) {
continue;
}
$this->addNodeAfterNode(new Catch_([$catchType], $catch->var, $catch->stmts), $node->catches[$key]);
}
}
if ($this->nodeComparator->areNodesEqual($originalCatches, $node->catches)) {
return null;
}
return $node;
}
}