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\Php71\Rector\TryCatch;
|
2018-10-08 04:39:37 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Stmt\TryCatch;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\PhpParser\Printer\BetterStandardPrinter;
|
|
|
|
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-10-08 04:39:37 +00:00
|
|
|
/**
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog https://wiki.php.net/rfc/multiple-catch
|
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php71\Rector\TryCatch\MultiExceptionCatchRector\MultiExceptionCatchRectorTest
|
2018-10-08 04:39:37 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class MultiExceptionCatchRector extends AbstractRector implements MinPhpVersionInterface
|
2018-10-08 04:39:37 +00:00
|
|
|
{
|
2022-04-09 22:42:24 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Printer\BetterStandardPrinter
|
2022-04-09 22:42:24 +00:00
|
|
|
*/
|
2023-06-08 14:23:03 +00:00
|
|
|
private $betterStandardPrinter;
|
|
|
|
public function __construct(BetterStandardPrinter $betterStandardPrinter)
|
2022-04-09 22:42:24 +00:00
|
|
|
{
|
2023-06-08 14:23:03 +00:00
|
|
|
$this->betterStandardPrinter = $betterStandardPrinter;
|
2022-04-09 22:42:24 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-10-08 04:39:37 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Changes multi catch of same exception to single one | separated.', [new CodeSample(<<<'CODE_SAMPLE'
|
2018-10-08 04:39:37 +00:00
|
|
|
try {
|
2020-09-08 13:27:17 +00:00
|
|
|
// Some code...
|
2018-10-08 04:39:37 +00:00
|
|
|
} catch (ExceptionType1 $exception) {
|
2020-09-08 13:27:17 +00:00
|
|
|
$sameCode;
|
2018-10-08 04:39:37 +00:00
|
|
|
} catch (ExceptionType2 $exception) {
|
2020-09-08 13:27:17 +00:00
|
|
|
$sameCode;
|
2018-10-08 04:39:37 +00:00
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-10-08 04:39:37 +00:00
|
|
|
try {
|
2021-07-28 12:55:48 +00:00
|
|
|
// Some code...
|
2018-10-08 04:39:37 +00:00
|
|
|
} catch (ExceptionType1 | ExceptionType2 $exception) {
|
2021-07-28 12:55:48 +00:00
|
|
|
$sameCode;
|
2018-10-08 04:39:37 +00:00
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2018-10-08 04:39:37 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-10-08 04:39:37 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-10-08 04:39:37 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [TryCatch::class];
|
2018-10-08 04:39:37 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param TryCatch $node
|
2018-10-08 04:39:37 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2018-10-08 04:39:37 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\count($node->catches) < 2) {
|
2018-10-21 10:19:14 +00:00
|
|
|
return null;
|
2018-10-08 04:39:37 +00:00
|
|
|
}
|
2023-06-04 17:44:36 +00:00
|
|
|
$printedCatches = [];
|
|
|
|
$hasChanged = \false;
|
|
|
|
foreach ($node->catches as $key => $catch) {
|
2023-06-08 14:23:03 +00:00
|
|
|
$currentPrintedCatch = $this->betterStandardPrinter->print($catch->stmts);
|
2023-06-04 17:44:36 +00:00
|
|
|
// already duplicated catch → remove it and join the type
|
|
|
|
if (\in_array($currentPrintedCatch, $printedCatches, \true)) {
|
|
|
|
// merge type to existing type
|
|
|
|
$existingCatchKey = \array_search($currentPrintedCatch, $printedCatches, \true);
|
|
|
|
$node->catches[$existingCatchKey]->types[] = $catch->types[0];
|
|
|
|
unset($node->catches[$key]);
|
|
|
|
$hasChanged = \true;
|
2018-10-08 04:39:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-06-04 17:44:36 +00:00
|
|
|
$printedCatches[$key] = $currentPrintedCatch;
|
2018-10-08 04:39:37 +00:00
|
|
|
}
|
2023-06-04 17:44:36 +00:00
|
|
|
if ($hasChanged) {
|
|
|
|
return $node;
|
2022-05-23 03:50:09 +00:00
|
|
|
}
|
2023-06-04 17:44:36 +00:00
|
|
|
return null;
|
2018-10-08 04:39:37 +00:00
|
|
|
}
|
2021-07-21 09:35:57 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::MULTI_EXCEPTION_CATCH;
|
2021-07-21 09:35:57 +00:00
|
|
|
}
|
2018-10-08 04:39:37 +00:00
|
|
|
}
|