Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 25 | class ExceptionHandler extends AbstractExceptionNormalizer implements SubscribingHandlerInterface |
||
| 26 | { |
||
| 27 | 2 | public static function getSubscribingMethods(): array |
|
| 28 | { |
||
| 29 | return [ |
||
| 30 | [ |
||
| 31 | 2 | 'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
|
| 32 | 'format' => 'json', |
||
| 33 | 'type' => \Error::class, |
||
| 34 | 'method' => 'serializeErrorToJson', |
||
| 35 | ], |
||
| 36 | [ |
||
| 37 | 'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
||
| 38 | 'format' => 'json', |
||
| 39 | 'type' => \Exception::class, |
||
| 40 | 'method' => 'serializeToJson', |
||
| 41 | ], |
||
| 42 | [ |
||
| 43 | 'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
||
| 44 | 'format' => 'xml', |
||
| 45 | 'type' => \Error::class, |
||
| 46 | 'method' => 'serializeErrorToXml', |
||
| 47 | ], |
||
| 48 | [ |
||
| 49 | 'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
||
| 50 | 'format' => 'xml', |
||
| 51 | 'type' => \Exception::class, |
||
| 52 | 'method' => 'serializeToXml', |
||
| 53 | ], |
||
| 54 | ]; |
||
| 55 | } |
||
| 56 | |||
| 57 | public function serializeToJson( |
||
| 58 | JsonSerializationVisitor $visitor, |
||
| 59 | \Exception $exception, |
||
| 60 | array $type, |
||
| 61 | Context $context |
||
| 62 | ) { |
||
| 63 | $data = $this->convertToArray($exception, $context); |
||
| 64 | |||
| 65 | return $visitor->visitArray($data, $type, $context); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function serializeErrorToJson( |
||
| 78 | |||
| 79 | View Code Duplication | public function serializeToXml( |
|
| 80 | XmlSerializationVisitor $visitor, |
||
| 81 | \Exception $exception, |
||
| 82 | array $type, |
||
| 83 | Context $context |
||
| 84 | ) { |
||
| 85 | $data = $this->convertToArray($exception, $context); |
||
| 86 | |||
| 87 | $document = $visitor->getDocument(true); |
||
| 88 | |||
| 89 | if (!$visitor->getCurrentNode()) { |
||
| 90 | $visitor->createRoot(); |
||
| 91 | } |
||
| 92 | |||
| 93 | foreach ($data as $key => $value) { |
||
| 94 | $entryNode = $document->createElement($key); |
||
| 95 | $visitor->getCurrentNode()->appendChild($entryNode); |
||
| 96 | $visitor->setCurrentNode($entryNode); |
||
| 97 | |||
| 98 | $node = $context->getNavigator()->accept($value, null, $context); |
||
| 99 | if (null !== $node) { |
||
| 100 | $visitor->getCurrentNode()->appendChild($node); |
||
| 101 | } |
||
| 102 | |||
| 103 | $visitor->revertCurrentNode(); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | View Code Duplication | public function serializeErrorToXml( |
|
| 134 | |||
| 135 | private function convertToArray(\Throwable $throwable, Context $context): array |
||
| 136 | { |
||
| 137 | $data = []; |
||
| 147 | } |
||
| 148 |
If you suppress an error, we recommend checking for the error condition explicitly: