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 |
||
| 22 | class Notification extends AggregateRoot implements EventSourcedAggregateRoot |
||
| 23 | { |
||
| 24 | private $id; |
||
| 25 | private $body; |
||
| 26 | private $publishedOn; |
||
| 27 | private $readOn; |
||
| 28 | private $status; |
||
| 29 | private $userId; |
||
| 30 | |||
| 31 | private function __construct(NotificationId $id) |
||
| 35 | |||
| 36 | public static function broadcast(NotificationId $id, UserId $userId, NotificationBody $body) : self |
||
| 51 | |||
| 52 | public function markAsRead() : void |
||
| 61 | |||
| 62 | public function markAsUnread() : void |
||
| 71 | |||
| 72 | protected function applyNotificationPublished(NotificationPublished $event) : void |
||
| 79 | |||
| 80 | protected function applyNotificationMarkedAsRead(NotificationMarkedAsRead $event) : void |
||
| 85 | |||
| 86 | protected function applyNotificationMarkedAsUnread(NotificationMarkedAsUnread $event) : void |
||
| 91 | |||
| 92 | View Code Duplication | public static function reconstitute(EventStream $stream) : EventSourcedAggregateRoot |
|
| 102 | |||
| 103 | public function id() : NotificationId |
||
| 107 | |||
| 108 | public function __toString() : string |
||
| 112 | } |
||
| 113 |
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.