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 |
||
10 | class File400 extends Remittance |
||
11 | { |
||
12 | /** |
||
13 | * File schema file. |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $schemaFile = '/schemas/400.json'; |
||
18 | |||
19 | /** |
||
20 | * Mutates an account on detail. |
||
21 | * |
||
22 | * @param mixed $value |
||
23 | * @return mixed |
||
24 | */ |
||
25 | protected function mutateDetailAccount($value) |
||
29 | |||
30 | /** |
||
31 | * Mutates an account DV on detail. |
||
32 | * |
||
33 | * @param mixed $value |
||
34 | * @param array $data |
||
35 | * @return mixed |
||
36 | */ |
||
37 | protected function mutateDetailAccountDv( |
||
45 | |||
46 | /** |
||
47 | * Mutates a branch on detail. |
||
48 | * |
||
49 | * @param mixed $value |
||
50 | * @param array $data |
||
51 | * @param array $meta |
||
52 | * @return mixed |
||
53 | */ |
||
54 | protected function mutateDetailBranch( |
||
61 | |||
62 | /** |
||
63 | * Mutates a branch DV on detail. |
||
64 | * |
||
65 | * @param mixed $value |
||
66 | * @param array $data |
||
67 | * @param array $meta |
||
68 | * @return mixed |
||
69 | */ |
||
70 | protected function mutateDetailBranchDv( |
||
77 | |||
78 | /** |
||
79 | * Mutates a company document type. |
||
80 | * |
||
81 | * @param mixed $value |
||
82 | * @param array $data |
||
83 | * @return mixed |
||
84 | */ |
||
85 | protected function mutateDetailCompanyDocumentType( |
||
91 | |||
92 | /** |
||
93 | * Mutates a discount to date. |
||
94 | * |
||
95 | * @param mixed $value |
||
96 | * @param array $data |
||
97 | * @return mixed |
||
98 | */ |
||
99 | protected function mutateDetailDiscountTo( |
||
107 | |||
108 | /** |
||
109 | * Mutates a document type. |
||
110 | * |
||
111 | * @param mixed $value |
||
112 | * @param array $data |
||
113 | * @return mixed |
||
114 | */ |
||
115 | protected function mutateDetailDocumentType( |
||
121 | |||
122 | /** |
||
123 | * Mutates a guarantee contract on detail. |
||
124 | * |
||
125 | * @param mixed $value |
||
126 | * @return mixed |
||
127 | */ |
||
128 | protected function mutateDetailGuaranteeContract($value) |
||
132 | |||
133 | /** |
||
134 | * Mutates a guarantee contract DV on detail. |
||
135 | * |
||
136 | * @param mixed $value |
||
137 | * @param array $data |
||
138 | * @return mixed |
||
139 | */ |
||
140 | protected function mutateDetailGuaranteeContractDv( |
||
148 | |||
149 | /** |
||
150 | * Mutates a receive branch on detail. |
||
151 | * |
||
152 | * @param mixed $value |
||
153 | * @param array $data |
||
154 | * @param array $meta |
||
155 | * @return mixed |
||
156 | */ |
||
157 | View Code Duplication | protected function mutateDetailReceiveBranch( |
|
166 | |||
167 | /** |
||
168 | * Mutates a receive branch DV on detail. |
||
169 | * |
||
170 | * @param mixed $value |
||
171 | * @param array $data |
||
172 | * @param array $meta |
||
173 | * @return mixed |
||
174 | */ |
||
175 | View Code Duplication | protected function mutateDetailReceiveBranchDv( |
|
184 | |||
185 | /** |
||
186 | * Mutates a branch on header. |
||
187 | * |
||
188 | * @param mixed $value |
||
189 | * @return mixed |
||
190 | */ |
||
191 | protected function mutateHeaderBranch($value) |
||
195 | |||
196 | /** |
||
197 | * Mutates a branch DV on header. |
||
198 | * |
||
199 | * @param mixed $value |
||
200 | * @param array $data |
||
201 | * @return mixed |
||
202 | */ |
||
203 | View Code Duplication | protected function mutateHeaderBranchDv( |
|
211 | |||
212 | /** |
||
213 | * Mutates a company code on header. |
||
214 | * |
||
215 | * @param mixed $value |
||
216 | * @param array $data |
||
217 | * @return mixed |
||
218 | */ |
||
219 | View Code Duplication | protected function mutateHeaderCompanyCode( |
|
227 | |||
228 | /** |
||
229 | * Mutates a company code DV on header. |
||
230 | * |
||
231 | * @param mixed $value |
||
232 | * @param array $data |
||
233 | * @return mixed |
||
234 | */ |
||
235 | View Code Duplication | protected function mutateHeaderCompanyCodeDv( |
|
243 | } |
||
244 |
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.