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 |
||
20 | class NextrasDataSource extends FilterableDataSource implements IDataSource |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * @var DbalCollection |
||
25 | */ |
||
26 | protected $data_source; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $data = []; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $aggregations = []; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $primary_key; |
||
42 | |||
43 | |||
44 | /** |
||
45 | * @param ICollection $data_source |
||
46 | * @param string $primary_key |
||
47 | */ |
||
48 | public function __construct(ICollection $data_source, $primary_key) |
||
53 | |||
54 | |||
55 | /******************************************************************************** |
||
56 | * IDataSource implementation * |
||
57 | ********************************************************************************/ |
||
58 | |||
59 | |||
60 | /** |
||
61 | * Get count of data |
||
62 | * @return int |
||
63 | */ |
||
64 | public function getCount() |
||
68 | |||
69 | /** |
||
70 | * Get the data |
||
71 | * @return array |
||
72 | */ |
||
73 | public function getData() |
||
80 | |||
81 | |||
82 | /** |
||
83 | * Filter data - get one row |
||
84 | * @param array $condition |
||
85 | * @return static |
||
86 | */ |
||
87 | public function filterOne(array $condition) |
||
97 | |||
98 | |||
99 | /** |
||
100 | * Filter by date |
||
101 | * @param Filter\FilterDate $filter |
||
102 | * @return static |
||
103 | */ |
||
104 | public function applyFilterDate(Filter\FilterDate $filter) |
||
118 | |||
119 | |||
120 | /** |
||
121 | * Filter by date range |
||
122 | * @param Filter\FilterDateRange $filter |
||
123 | * @return void |
||
124 | */ |
||
125 | public function applyFilterDateRange(Filter\FilterDateRange $filter) |
||
147 | |||
148 | |||
149 | /** |
||
150 | * Filter by range |
||
151 | * @param Filter\FilterRange $filter |
||
152 | * @return void |
||
153 | */ |
||
154 | public function applyFilterRange(Filter\FilterRange $filter) |
||
175 | |||
176 | |||
177 | /** |
||
178 | * Filter by keyword |
||
179 | * @param Filter\FilterText $filter |
||
180 | * @return void |
||
181 | */ |
||
182 | public function applyFilterText(Filter\FilterText $filter) |
||
215 | |||
216 | |||
217 | /** |
||
218 | * Filter by multi select value |
||
219 | * @param Filter\FilterMultiSelect $filter |
||
220 | * @return void |
||
221 | */ |
||
222 | public function applyFilterMultiSelect(Filter\FilterMultiSelect $filter) |
||
240 | |||
241 | |||
242 | /** |
||
243 | * Filter by select value |
||
244 | * @param Filter\FilterSelect $filter |
||
245 | * @return void |
||
246 | */ |
||
247 | public function applyFilterSelect(Filter\FilterSelect $filter) |
||
251 | |||
252 | |||
253 | /** |
||
254 | * Apply limit and offset on data |
||
255 | * @param int $offset |
||
256 | * @param int $limit |
||
257 | * @return static |
||
258 | */ |
||
259 | public function limit($offset, $limit) |
||
265 | |||
266 | |||
267 | /** |
||
268 | * Sort data |
||
269 | * @param Sorting $sorting |
||
270 | * @return static |
||
271 | */ |
||
272 | public function sort(Sorting $sorting) |
||
303 | |||
304 | /** |
||
305 | * Adjust column from DataGrid 'foreignKey.column' to Nextras 'this->foreignKey->column' |
||
306 | * @param string $column |
||
307 | * @return string |
||
308 | */ |
||
309 | private function prepareColumn($column) |
||
316 | |||
317 | /** |
||
318 | * @param string $aggregation_type |
||
319 | * @param string $column |
||
320 | * @return mixed |
||
321 | */ |
||
322 | public function addAggregationColumn($aggregation_type, $column) |
||
326 | |||
327 | /** |
||
328 | * get aggregation row |
||
329 | * @return array |
||
330 | */ |
||
331 | public function getAggregationData() |
||
354 | } |
||
355 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.