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 HLQuery extends BaseQuery |
||
11 | { |
||
12 | public $keyBy = 'ID'; |
||
13 | |||
14 | /** |
||
15 | * Query sort. |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | public $select = false; |
||
20 | public $sort = ['ID' => 'ASC']; |
||
21 | public $limit; |
||
22 | public $page; |
||
23 | public $ttl = 0; |
||
24 | |||
25 | /** |
||
26 | * Query group by. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | public $groupBy = false; |
||
31 | |||
32 | /** |
||
33 | * Iblock id. |
||
34 | * |
||
35 | * @var int |
||
36 | */ |
||
37 | protected $tableName; |
||
38 | |||
39 | /** |
||
40 | * Constructor. |
||
41 | * |
||
42 | * @param object $bxObject |
||
43 | * @param string $modelName |
||
44 | */ |
||
45 | public function __construct($bxObject, $modelName) |
||
51 | |||
52 | /** |
||
53 | * Setter for groupBy. |
||
54 | * |
||
55 | * @param $value |
||
56 | * |
||
57 | * @return $this |
||
58 | */ |
||
59 | public function groupBy($value) |
||
65 | |||
66 | public function limit($value) |
||
72 | |||
73 | public function page($num) |
||
79 | |||
80 | public function cache($ttl) |
||
86 | |||
87 | /** |
||
88 | * Get list of items. |
||
89 | * |
||
90 | * @return Collection |
||
91 | */ |
||
92 | public function getList() |
||
120 | |||
121 | View Code Duplication | protected function addItemToResultsUsingKeyBy(&$results, HLModel $object) |
|
164 | |||
165 | /** |
||
166 | * Get count of elements that match $filter. |
||
167 | * |
||
168 | * @return int |
||
169 | */ |
||
170 | public function count() |
||
178 | |||
179 | /** |
||
180 | * Normalize filter before sending it to getList. |
||
181 | * This prevents some inconsistency. |
||
182 | * |
||
183 | * @return array |
||
184 | */ |
||
185 | protected function normalizeFilter() |
||
189 | |||
190 | /** |
||
191 | * Normalize select before sending it to getList. |
||
192 | * This prevents some inconsistency. |
||
193 | * |
||
194 | * @return array |
||
195 | */ |
||
196 | protected function normalizeSelect() |
||
200 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.