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 |
||
23 | 1 | class DoctrineDataSource extends FilterableDataSource implements IDataSource, IAggregatable |
|
24 | { |
||
25 | /** |
||
26 | * Event called when datagrid data is loaded. |
||
27 | * @var callable[] |
||
28 | */ |
||
29 | public $onDataLoaded; |
||
30 | |||
31 | /** |
||
32 | * @var QueryBuilder |
||
33 | */ |
||
34 | protected $data_source; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $primary_key; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $root_alias; |
||
45 | |||
46 | /** |
||
47 | * @var int |
||
48 | */ |
||
49 | protected $placeholder; |
||
50 | |||
51 | |||
52 | /** |
||
53 | * @param QueryBuilder $data_source |
||
54 | * @param string $primary_key |
||
55 | */ |
||
56 | public function __construct(QueryBuilder $data_source, $primary_key) |
||
62 | |||
63 | |||
64 | /** |
||
65 | * @return \Doctrine\ORM\Query |
||
66 | */ |
||
67 | public function getQuery() |
||
71 | |||
72 | |||
73 | /** |
||
74 | * @param string $column |
||
75 | * @return string |
||
76 | */ |
||
77 | private function checkAliases($column) |
||
90 | |||
91 | |||
92 | /** |
||
93 | * @return bool |
||
94 | */ |
||
95 | private function usePaginator() |
||
99 | |||
100 | |||
101 | /******************************************************************************** |
||
102 | * IDataSource implementation * |
||
103 | ********************************************************************************/ |
||
104 | |||
105 | |||
106 | /** |
||
107 | * Get count of data |
||
108 | * @return int |
||
109 | */ |
||
110 | public function getCount() |
||
120 | |||
121 | |||
122 | /** |
||
123 | * Get the data |
||
124 | * @return array |
||
125 | */ |
||
126 | public function getData() |
||
140 | |||
141 | |||
142 | /** |
||
143 | * Filter data - get one row |
||
144 | * @param array $condition |
||
145 | * @return static |
||
146 | */ |
||
147 | public function filterOne(array $condition) |
||
160 | |||
161 | |||
162 | /** |
||
163 | * Filter by date |
||
164 | * @param Filter\FilterDate $filter |
||
165 | */ |
||
166 | public function applyFilterDate(Filter\FilterDate $filter) |
||
180 | |||
181 | |||
182 | /** |
||
183 | * Filter by date range |
||
184 | * @param Filter\FilterDateRange $filter |
||
185 | */ |
||
186 | public function applyFilterDateRange(Filter\FilterDateRange $filter) |
||
212 | |||
213 | |||
214 | /** |
||
215 | * Filter by range |
||
216 | * @param Filter\FilterRange $filter |
||
217 | */ |
||
218 | public function applyFilterRange(Filter\FilterRange $filter) |
||
236 | |||
237 | |||
238 | /** |
||
239 | * Filter by keyword |
||
240 | * @param Filter\FilterText $filter |
||
241 | */ |
||
242 | public function applyFilterText(Filter\FilterText $filter) |
||
270 | |||
271 | |||
272 | /** |
||
273 | * Filter by multi select value |
||
274 | * @param Filter\FilterMultiSelect $filter |
||
275 | */ |
||
276 | public function applyFilterMultiSelect(Filter\FilterMultiSelect $filter) |
||
286 | |||
287 | |||
288 | /** |
||
289 | * Filter by select value |
||
290 | * @param Filter\FilterSelect $filter |
||
291 | */ |
||
292 | public function applyFilterSelect(Filter\FilterSelect $filter) |
||
303 | |||
304 | |||
305 | /** |
||
306 | * Apply limit and offset on data |
||
307 | * @param int $offset |
||
308 | * @param int $limit |
||
309 | * @return static |
||
310 | */ |
||
311 | public function limit($offset, $limit) |
||
317 | |||
318 | |||
319 | /** |
||
320 | * Sort data |
||
321 | * @param Sorting $sorting |
||
322 | * @return static |
||
323 | */ |
||
324 | View Code Duplication | public function sort(Sorting $sorting) |
|
353 | |||
354 | |||
355 | /** |
||
356 | * Get unique int value for each instance class (self) |
||
357 | * @return int |
||
358 | */ |
||
359 | public function getPlaceholder() |
||
363 | |||
364 | |||
365 | /** |
||
366 | * @param callable $aggregationCallback |
||
367 | * @return void |
||
368 | */ |
||
369 | public function processAggregation(callable $aggregationCallback) |
||
373 | } |
||
374 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.