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 DoctrineCollectionDataSource 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 DoctrineCollectionDataSource, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | final class DoctrineCollectionDataSource extends FilterableDataSource implements IDataSource |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * @var Collection |
||
23 | */ |
||
24 | private $data_source; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $primary_key; |
||
30 | |||
31 | /** |
||
32 | * @var Criteria |
||
33 | */ |
||
34 | private $criteria; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $aggregations = []; |
||
40 | |||
41 | /** |
||
42 | * @param Collection $collection |
||
43 | * @param string $primary_key |
||
44 | */ |
||
45 | public function __construct(Collection $collection, $primary_key) |
||
51 | |||
52 | |||
53 | /** |
||
54 | * @return Collection |
||
55 | */ |
||
56 | private function getFilteredCollection() |
||
60 | |||
61 | |||
62 | /******************************************************************************** |
||
63 | * IDataSource implementation * |
||
64 | ********************************************************************************/ |
||
65 | |||
66 | |||
67 | /** |
||
68 | * Get count of data |
||
69 | * @return int |
||
70 | */ |
||
71 | public function getCount() |
||
75 | |||
76 | |||
77 | /** |
||
78 | * Get the data |
||
79 | * @return array |
||
80 | */ |
||
81 | public function getData() |
||
85 | |||
86 | |||
87 | /** |
||
88 | * Filter data - get one row |
||
89 | * @param array $condition |
||
90 | * @return static |
||
91 | */ |
||
92 | public function filterOne(array $condition) |
||
101 | |||
102 | |||
103 | /** |
||
104 | * Filter by date |
||
105 | * @param Filter\FilterDate $filter |
||
106 | * @return void |
||
107 | */ |
||
108 | public function applyFilterDate(Filter\FilterDate $filter) |
||
119 | |||
120 | |||
121 | /** |
||
122 | * Filter by date range |
||
123 | * @param Filter\FilterDateRange $filter |
||
124 | * @return void |
||
125 | */ |
||
126 | public function applyFilterDateRange(Filter\FilterDateRange $filter) |
||
146 | |||
147 | |||
148 | /** |
||
149 | * Filter by range |
||
150 | * @param Filter\FilterRange $filter |
||
151 | * @return void |
||
152 | */ |
||
153 | public function applyFilterRange(Filter\FilterRange $filter) |
||
167 | |||
168 | |||
169 | /** |
||
170 | * Filter by keyword |
||
171 | * @param Filter\FilterText $filter |
||
172 | * @return void |
||
173 | */ |
||
174 | public function applyFilterText(Filter\FilterText $filter) |
||
198 | |||
199 | |||
200 | /** |
||
201 | * Filter by multi select value |
||
202 | * @param Filter\FilterMultiSelect $filter |
||
203 | * @return void |
||
204 | */ |
||
205 | public function applyFilterMultiSelect(Filter\FilterMultiSelect $filter) |
||
212 | |||
213 | |||
214 | /** |
||
215 | * Filter by select value |
||
216 | * @param Filter\FilterSelect $filter |
||
217 | * @return void |
||
218 | */ |
||
219 | public function applyFilterSelect(Filter\FilterSelect $filter) |
||
226 | |||
227 | |||
228 | /** |
||
229 | * Apply limit and offset on data |
||
230 | * @param int $offset |
||
231 | * @param int $limit |
||
232 | * @return static |
||
233 | */ |
||
234 | public function limit($offset, $limit) |
||
240 | |||
241 | |||
242 | /** |
||
243 | * Sort data |
||
244 | * @param Sorting $sorting |
||
245 | * @return static |
||
246 | */ |
||
247 | public function sort(Sorting $sorting) |
||
268 | |||
269 | /** |
||
270 | * @param string $aggregation_type |
||
271 | * @param string $column |
||
272 | * @return mixed |
||
273 | */ |
||
274 | public function addAggregationColumn($aggregation_type, $column) |
||
278 | |||
279 | /** |
||
280 | * get aggregation row |
||
281 | * @return array |
||
282 | */ |
||
283 | View Code Duplication | public function getAggregationData() |
|
317 | } |
||
318 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: