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 |
||
17 | class SenderFactory |
||
18 | { |
||
19 | /** |
||
20 | * @var NotifynderGroup |
||
21 | */ |
||
22 | protected $notifynderGroup; |
||
23 | |||
24 | /** |
||
25 | * @var NotifynderCategory |
||
26 | */ |
||
27 | private $notifynderCategory; |
||
28 | |||
29 | /** |
||
30 | * @param NotifynderGroup $notifynderGroup |
||
31 | * @param NotifynderCategory $notifynderCategory |
||
32 | */ |
||
33 | public function __construct(NotifynderGroup $notifynderGroup, |
||
39 | |||
40 | /** |
||
41 | * Get the right sender when the data is |
||
42 | * passed. |
||
43 | * |
||
44 | * @param array $infoNotifications |
||
45 | * @param $category |
||
46 | * @return SendMultiple|SendOne |
||
47 | */ |
||
48 | public function getSender($infoNotifications, $category = null) |
||
63 | |||
64 | /** |
||
65 | * Send Single Notification Sender. |
||
66 | * |
||
67 | * @param array $infoNotifications |
||
68 | * @param $category |
||
69 | * @return SendOne |
||
70 | */ |
||
71 | public function sendSingle(array $infoNotifications, $category) |
||
75 | |||
76 | /** |
||
77 | * Send Multiple Notification Sender. |
||
78 | * |
||
79 | * @param array $infoNotifications |
||
80 | * @return SendMultiple |
||
81 | */ |
||
82 | public function sendMultiple(array $infoNotifications) |
||
86 | |||
87 | /** |
||
88 | * Get the the send group instance. |
||
89 | * |
||
90 | * @param string $groupName |
||
91 | * @param array | \Closure $info |
||
92 | * @return SendGroup |
||
93 | */ |
||
94 | public function sendGroup($groupName, array $info) |
||
103 | |||
104 | /** |
||
105 | * Check if the array passed is |
||
106 | * multidimensional. |
||
107 | * |
||
108 | * @param $arr |
||
109 | * @return bool |
||
110 | */ |
||
111 | View Code Duplication | protected function isMultiArray(array $arr) |
|
120 | } |
||
121 |
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
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.