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 |
||
17 | 1 | class NetteDatabaseTableDataSource extends FilterableDataSource implements IDataSource |
|
18 | { |
||
19 | |||
20 | /** |
||
21 | * @var Selection |
||
22 | */ |
||
23 | protected $data_source; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $data = []; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $primary_key; |
||
34 | |||
35 | |||
36 | /** |
||
37 | * @param Selection $data_source |
||
38 | * @param string $primary_key |
||
39 | */ |
||
40 | public function __construct(Selection $data_source, $primary_key) |
||
45 | |||
46 | |||
47 | /******************************************************************************** |
||
48 | * IDataSource implementation * |
||
49 | ********************************************************************************/ |
||
50 | |||
51 | |||
52 | /** |
||
53 | * Get count of data |
||
54 | * @return int |
||
55 | */ |
||
56 | public function getCount() |
||
83 | |||
84 | |||
85 | /** |
||
86 | * Get the data |
||
87 | * @return array |
||
88 | */ |
||
89 | public function getData() |
||
93 | |||
94 | |||
95 | /** |
||
96 | * Filter data - get one row |
||
97 | * @param array $condition |
||
98 | * @return static |
||
99 | */ |
||
100 | public function filterOne(array $condition) |
||
106 | |||
107 | |||
108 | /** |
||
109 | * Filter by date |
||
110 | * @param Filter\FilterDate $filter |
||
111 | * @return void |
||
112 | */ |
||
113 | View Code Duplication | public function applyFilterDate(Filter\FilterDate $filter) |
|
121 | |||
122 | |||
123 | /** |
||
124 | * Filter by date range |
||
125 | * @param Filter\FilterDateRange $filter |
||
126 | * @return void |
||
127 | */ |
||
128 | View Code Duplication | public function applyFilterDateRange(Filter\FilterDateRange $filter) |
|
149 | |||
150 | |||
151 | /** |
||
152 | * Filter by range |
||
153 | * @param Filter\FilterRange $filter |
||
154 | * @return void |
||
155 | */ |
||
156 | View Code Duplication | public function applyFilterRange(Filter\FilterRange $filter) |
|
171 | |||
172 | |||
173 | /** |
||
174 | * Filter by keyword |
||
175 | * @param Filter\FilterText $filter |
||
176 | * @return void |
||
177 | */ |
||
178 | public function applyFilterText(Filter\FilterText $filter) |
||
223 | |||
224 | |||
225 | /** |
||
226 | * Filter by multi select value |
||
227 | * @param Filter\FilterMultiSelect $filter |
||
228 | * @return void |
||
229 | */ |
||
230 | public function applyFilterMultiSelect(Filter\FilterMultiSelect $filter) |
||
257 | |||
258 | |||
259 | /** |
||
260 | * Filter by select value |
||
261 | * @param Filter\FilterSelect $filter |
||
262 | * @return void |
||
263 | */ |
||
264 | public function applyFilterSelect(Filter\FilterSelect $filter) |
||
268 | |||
269 | |||
270 | /** |
||
271 | * Apply limit and offset on data |
||
272 | * @param int $offset |
||
273 | * @param int $limit |
||
274 | * @return static |
||
275 | */ |
||
276 | public function limit($offset, $limit) |
||
282 | |||
283 | |||
284 | /** |
||
285 | * Sort data |
||
286 | * @param Sorting $sorting |
||
287 | * @return static |
||
288 | */ |
||
289 | View Code Duplication | public function sort(Sorting $sorting) |
|
320 | |||
321 | |||
322 | /** |
||
323 | * @param callable $aggregationCallback |
||
324 | * @return void |
||
325 | */ |
||
326 | public function processAggregation(callable $aggregationCallback) |
||
330 | } |
||
331 |
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.