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 |
||
18 | class HasAndBelongsToMany extends HasOneOrMany |
||
19 | { |
||
20 | use HasPivotTable; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $type = 'hasAndBelongsToMany'; |
||
26 | |||
27 | /** |
||
28 | * @var null |
||
29 | */ |
||
30 | protected $joinFields = null; |
||
31 | |||
32 | /** |
||
33 | * @inheritdoc |
||
34 | */ |
||
35 | public function addParams($params) |
||
40 | |||
41 | /** @noinspection PhpMissingParentCallCommonInspection |
||
42 | * @return SelectQuery |
||
43 | * @throws \Exception |
||
44 | */ |
||
45 | 1 | public function newQuery() |
|
71 | |||
72 | /** |
||
73 | * @return null|array |
||
74 | */ |
||
75 | 1 | protected function getJoinFields() |
|
83 | |||
84 | /** |
||
85 | * @param null $joinFields |
||
86 | */ |
||
87 | 1 | public function setJoinFields($joinFields) |
|
91 | |||
92 | protected function initJoinFields() |
||
97 | |||
98 | /** |
||
99 | * @inheritdoc |
||
100 | */ |
||
101 | View Code Duplication | public function populateQuerySpecific(AbstractQuery $query) |
|
110 | |||
111 | /** |
||
112 | * Simple select query from the link table |
||
113 | * @param bool $specific |
||
114 | * @return SelectQuery |
||
115 | */ |
||
116 | public function getLinkQuery($specific = true) |
||
127 | |||
128 | /** @noinspection PhpMissingParentCallCommonInspection |
||
129 | * @param RecordCollection $collection |
||
130 | * @return RecordCollection |
||
131 | */ |
||
132 | public function getEagerResults($collection) |
||
154 | |||
155 | /** @noinspection PhpMissingParentCallCommonInspection |
||
156 | * @return $this |
||
157 | */ |
||
158 | public function save() |
||
165 | |||
166 | protected function deleteConnections() |
||
176 | |||
177 | /** |
||
178 | * @return DeleteQuery |
||
179 | */ |
||
180 | protected function newDeleteQuery() |
||
186 | |||
187 | protected function saveConnections() |
||
196 | |||
197 | /** |
||
198 | * @return InsertQuery |
||
199 | */ |
||
200 | protected function newInsertQuery() |
||
206 | |||
207 | /** |
||
208 | * @param InsertQuery $query |
||
209 | * @param $records |
||
210 | */ |
||
211 | protected function queryAttachRecords($query, $records) |
||
225 | |||
226 | /** |
||
227 | * @param $record |
||
228 | * @return array |
||
229 | */ |
||
230 | protected function formatAttachData($record) |
||
238 | |||
239 | /** |
||
240 | * @param $model |
||
241 | */ |
||
242 | public function attach($model) |
||
249 | |||
250 | /** |
||
251 | * @param Record $model |
||
252 | */ |
||
253 | public function detach($model) |
||
260 | |||
261 | /** |
||
262 | * @param DeleteQuery $query |
||
263 | * @param $records |
||
264 | */ |
||
265 | protected function queryDetachRecords($query, $records) |
||
278 | |||
279 | |||
280 | /** @noinspection PhpMissingParentCallCommonInspection |
||
281 | * @return mixed |
||
282 | */ |
||
283 | public function getWithClass() |
||
287 | |||
288 | /** @noinspection PhpMissingParentCallCommonInspection |
||
289 | * @return string |
||
290 | */ |
||
291 | protected function getDictionaryKey() |
||
295 | } |
||
296 |
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.