betterStandardPrinter = $betterStandardPrinter; } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Changes multi catch of same exception to single one | separated.', [new CodeSample(<<<'CODE_SAMPLE' try { // Some code... } catch (ExceptionType1 $exception) { $sameCode; } catch (ExceptionType2 $exception) { $sameCode; } CODE_SAMPLE , <<<'CODE_SAMPLE' try { // Some code... } catch (ExceptionType1 | ExceptionType2 $exception) { $sameCode; } CODE_SAMPLE )]); } /** * @return array> */ public function getNodeTypes() : array { return [TryCatch::class]; } /** * @param TryCatch $node */ public function refactor(Node $node) : ?Node { if (\count($node->catches) < 2) { return null; } $printedCatches = []; $hasChanged = \false; foreach ($node->catches as $key => $catch) { $currentPrintedCatch = $this->betterStandardPrinter->print($catch->stmts); // 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; continue; } $printedCatches[$key] = $currentPrintedCatch; } if ($hasChanged) { return $node; } return null; } public function provideMinPhpVersion() : int { return PhpVersionFeature::MULTI_EXCEPTION_CATCH; } }