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 |
||
| 23 | class ExceptionHandler extends AbstractExceptionNormalizer implements SubscribingHandlerInterface |
||
| 24 | { |
||
| 25 | 3 | public static function getSubscribingMethods() |
|
| 54 | |||
| 55 | 2 | public function serializeToJson( |
|
| 56 | JsonSerializationVisitor $visitor, |
||
| 57 | \Exception $exception, |
||
| 58 | array $type, |
||
| 59 | Context $context |
||
| 60 | ) { |
||
| 61 | 2 | $data = $this->convertToArray($exception, $context); |
|
| 62 | |||
| 63 | 2 | return $visitor->visitArray($data, $type, $context); |
|
|
|
|||
| 64 | } |
||
| 65 | |||
| 66 | public function serializeErrorToJson( |
||
| 76 | |||
| 77 | 1 | View Code Duplication | public function serializeToXml( |
| 78 | XmlSerializationVisitor $visitor, |
||
| 79 | \Exception $exception, |
||
| 80 | array $type, |
||
| 81 | Context $context |
||
| 82 | ) { |
||
| 83 | 1 | $data = $this->convertToArray($exception, $context); |
|
| 84 | |||
| 85 | 1 | $document = $visitor->getDocument(true); |
|
| 86 | |||
| 87 | 1 | if (!$visitor->getCurrentNode()) { |
|
| 88 | 1 | $visitor->createRoot(); |
|
| 89 | } |
||
| 90 | |||
| 91 | 1 | foreach ($data as $key => $value) { |
|
| 92 | 1 | $entryNode = $document->createElement($key); |
|
| 93 | 1 | $visitor->getCurrentNode()->appendChild($entryNode); |
|
| 94 | 1 | $visitor->setCurrentNode($entryNode); |
|
| 95 | |||
| 96 | 1 | $node = $context->getNavigator()->accept($value, null, $context); |
|
| 97 | 1 | if (null !== $node) { |
|
| 98 | 1 | $visitor->getCurrentNode()->appendChild($node); |
|
| 99 | } |
||
| 100 | |||
| 101 | 1 | $visitor->revertCurrentNode(); |
|
| 102 | } |
||
| 103 | 1 | } |
|
| 104 | |||
| 105 | View Code Duplication | public function serializeErrorToXml( |
|
| 132 | |||
| 133 | 3 | private function convertToArray(\Throwable $throwable, Context $context): array |
|
| 134 | { |
||
| 145 | } |
||
| 146 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.