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