mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-08 12:20:52 +00:00
a389839c5c
* [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>
84 lines
2.1 KiB
PHP
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;
|
|
}
|
|
}
|