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 |
||
17 | final class DoctrineCollectionDataSource extends FilterableDataSource implements IDataSource |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * @var Collection |
||
22 | */ |
||
23 | private $data_source; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $primary_key; |
||
29 | |||
30 | /** |
||
31 | * @var Criteria |
||
32 | */ |
||
33 | private $criteria; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $aggregations = []; |
||
39 | |||
40 | /** |
||
41 | * @param Collection $collection |
||
42 | * @param string $primary_key |
||
43 | */ |
||
44 | public function __construct(Collection $collection, $primary_key) |
||
50 | |||
51 | |||
52 | /** |
||
53 | * @return Collection |
||
54 | */ |
||
55 | private function getFilteredCollection() |
||
59 | |||
60 | |||
61 | /******************************************************************************** |
||
62 | * IDataSource implementation * |
||
63 | ********************************************************************************/ |
||
64 | |||
65 | |||
66 | /** |
||
67 | * Get count of data |
||
68 | * @return int |
||
69 | */ |
||
70 | public function getCount() |
||
74 | |||
75 | |||
76 | /** |
||
77 | * Get the data |
||
78 | * @return array |
||
79 | */ |
||
80 | public function getData() |
||
84 | |||
85 | |||
86 | /** |
||
87 | * Filter data - get one row |
||
88 | * @param array $condition |
||
89 | * @return static |
||
90 | */ |
||
91 | public function filterOne(array $condition) |
||
100 | |||
101 | |||
102 | /** |
||
103 | * Filter by date |
||
104 | * @param Filter\FilterDate $filter |
||
105 | * @return void |
||
106 | */ |
||
107 | 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) |
||
148 | |||
149 | |||
150 | /** |
||
151 | * Filter by range |
||
152 | * @param Filter\FilterRange $filter |
||
153 | * @return void |
||
154 | */ |
||
155 | public function applyFilterRange(Filter\FilterRange $filter) |
||
169 | |||
170 | |||
171 | /** |
||
172 | * Filter by keyword |
||
173 | * @param Filter\FilterText $filter |
||
174 | * @return void |
||
175 | */ |
||
176 | public function applyFilterText(Filter\FilterText $filter) |
||
200 | |||
201 | |||
202 | /** |
||
203 | * Filter by multi select value |
||
204 | * @param Filter\FilterMultiSelect $filter |
||
205 | * @return void |
||
206 | */ |
||
207 | public function applyFilterMultiSelect(Filter\FilterMultiSelect $filter) |
||
214 | |||
215 | |||
216 | /** |
||
217 | * Filter by select value |
||
218 | * @param Filter\FilterSelect $filter |
||
219 | * @return void |
||
220 | */ |
||
221 | public function applyFilterSelect(Filter\FilterSelect $filter) |
||
228 | |||
229 | |||
230 | /** |
||
231 | * Apply limit and offset on data |
||
232 | * @param int $offset |
||
233 | * @param int $limit |
||
234 | * @return static |
||
235 | */ |
||
236 | public function limit($offset, $limit) |
||
242 | |||
243 | |||
244 | /** |
||
245 | * Sort data |
||
246 | * @param Sorting $sorting |
||
247 | * @return static |
||
248 | */ |
||
249 | public function sort(Sorting $sorting) |
||
270 | |||
271 | /** |
||
272 | * @param string $aggregation_type |
||
273 | * @param string $column |
||
274 | * @return mixed |
||
275 | */ |
||
276 | public function addAggregationColumn($aggregation_type, $column) |
||
280 | |||
281 | /** |
||
282 | * get aggregation row |
||
283 | * @return array |
||
284 | */ |
||
285 | View Code Duplication | public function getAggregationData() |
|
319 | } |
||
320 |
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: