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:
Complex classes like DoctrineDataSource often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DoctrineDataSource, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | 1 | class DoctrineDataSource extends FilterableDataSource implements IDataSource |
|
24 | { |
||
25 | |||
26 | /** |
||
27 | * Event called when datagrid data is loaded. |
||
28 | * @var callable[] |
||
29 | */ |
||
30 | public $onDataLoaded; |
||
31 | |||
32 | /** |
||
33 | * @var QueryBuilder |
||
34 | */ |
||
35 | protected $data_source; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $primary_key; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $root_alias; |
||
46 | |||
47 | /** |
||
48 | * @var int |
||
49 | */ |
||
50 | protected $placeholder; |
||
51 | |||
52 | |||
53 | /** |
||
54 | * @param QueryBuilder $data_source |
||
55 | * @param string $primary_key |
||
56 | */ |
||
57 | public function __construct(QueryBuilder $data_source, $primary_key) |
||
63 | |||
64 | |||
65 | /** |
||
66 | * @return \Doctrine\ORM\Query |
||
67 | */ |
||
68 | public function getQuery() |
||
72 | 1 | ||
73 | |||
74 | /** |
||
75 | * @param string $column |
||
76 | * @return string |
||
77 | */ |
||
78 | private function checkAliases($column) |
||
91 | |||
92 | |||
93 | /******************************************************************************** |
||
94 | * IDataSource implementation * |
||
95 | ********************************************************************************/ |
||
96 | |||
97 | |||
98 | /** |
||
99 | * Get count of data |
||
100 | * @return int |
||
101 | */ |
||
102 | public function getCount() |
||
106 | |||
107 | |||
108 | /** |
||
109 | * Get the data |
||
110 | * @return array |
||
111 | */ |
||
112 | public function getData() |
||
122 | |||
123 | |||
124 | /** |
||
125 | * Filter data |
||
126 | * @param array $filters |
||
127 | * @return static |
||
128 | */ |
||
129 | public function filter(array $filters) |
||
164 | |||
165 | |||
166 | /** |
||
167 | * Filter data - get one row |
||
168 | * @param array $condition |
||
169 | * @return static |
||
170 | */ |
||
171 | public function filterOne(array $condition) |
||
184 | |||
185 | |||
186 | /** |
||
187 | * Filter by date |
||
188 | * @param Filter\FilterDate $filter |
||
189 | */ |
||
190 | public function applyFilterDate(Filter\FilterDate $filter) |
||
206 | |||
207 | |||
208 | /** |
||
209 | * Filter by date range |
||
210 | * @param Filter\FilterDateRange $filter |
||
211 | */ |
||
212 | public function applyFilterDateRange(Filter\FilterDateRange $filter) |
||
238 | |||
239 | |||
240 | /** |
||
241 | * Filter by range |
||
242 | * @param Filter\FilterRange $filter |
||
243 | */ |
||
244 | public function applyFilterRange(Filter\FilterRange $filter) |
||
262 | |||
263 | |||
264 | /** |
||
265 | * Filter by keyword |
||
266 | * @param Filter\FilterText $filter |
||
267 | */ |
||
268 | public function applyFilterText(Filter\FilterText $filter) |
||
296 | |||
297 | |||
298 | /** |
||
299 | * Filter by multi select value |
||
300 | * @param Filter\FilterMultiSelect $filter |
||
301 | */ |
||
302 | public function applyFilterMultiSelect(Filter\FilterMultiSelect $filter) |
||
312 | |||
313 | |||
314 | /** |
||
315 | * Filter by select value |
||
316 | * @param Filter\FilterSelect $filter |
||
317 | */ |
||
318 | public function applyFilterSelect(Filter\FilterSelect $filter) |
||
329 | |||
330 | |||
331 | /** |
||
332 | * Apply limit and offset on data |
||
333 | * @param int $offset |
||
334 | * @param int $limit |
||
335 | * @return static |
||
336 | */ |
||
337 | public function limit($offset, $limit) |
||
343 | |||
344 | |||
345 | /** |
||
346 | * Sort data |
||
347 | * @param Sorting $sorting |
||
348 | * @return static |
||
349 | */ |
||
350 | View Code Duplication | public function sort(Sorting $sorting) |
|
379 | |||
380 | |||
381 | /** |
||
382 | * Get unique int value for each instance class (self) |
||
383 | * @return int |
||
384 | */ |
||
385 | public function getPlaceholder() |
||
389 | |||
390 | } |
||
391 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..