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 Collection 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 Collection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Collection extends Paged |
||
23 | { |
||
24 | /** @var string Entity manager instance */ |
||
25 | protected $managerEntity = '\samsoncms\api\query\Generic'; |
||
26 | |||
27 | /** @var array Collection for current filtered material identifiers */ |
||
28 | protected $materialIDs = array(); |
||
29 | |||
30 | /** @var array Collection of navigation filters */ |
||
31 | protected $navigation = array(); |
||
32 | |||
33 | /** @var array Collection of field filters */ |
||
34 | protected $field = array(); |
||
35 | |||
36 | /** @var array Search string collection */ |
||
37 | protected $search = array(); |
||
38 | |||
39 | /** @var array Collection of query handlers */ |
||
40 | protected $idHandlers = array(); |
||
41 | |||
42 | /** @var array External material handler and params array */ |
||
43 | protected $entityHandlers = array(); |
||
44 | |||
45 | /** @var array Base material entity handler callbacks array */ |
||
46 | protected $baseEntityHandlers = array(); |
||
47 | |||
48 | /** @var string Collection entities class name */ |
||
49 | protected $entityName = 'samson\cms\CMSMaterial'; |
||
50 | |||
51 | /** @var array Sorter parameters collection */ |
||
52 | protected $sorter = array(); |
||
53 | |||
54 | /** |
||
55 | * Generic collection constructor |
||
56 | * @param RenderInterface $renderer View render object |
||
57 | * @param QueryInterface $query Query object |
||
58 | */ |
||
59 | public function __construct(RenderInterface $renderer, QueryInterface $query, PagerInterface $pager) |
||
64 | |||
65 | /** |
||
66 | * Render products collection block |
||
67 | * @param string $prefix Prefix for view variables |
||
68 | * @param array $restricted Collection of ignored keys |
||
69 | * @return array Collection key => value |
||
70 | */ |
||
71 | public function toView($prefix = null, array $restricted = array()) |
||
79 | |||
80 | /** |
||
81 | * Add external identifier filter handler |
||
82 | * @param callback $handler |
||
83 | * @param array $params |
||
84 | * @return self Chaining |
||
85 | */ |
||
86 | public function handler($handler, array $params = array()) |
||
93 | |||
94 | /** |
||
95 | * Set external entity handler |
||
96 | * @param callback $handler |
||
97 | * @param array $params |
||
98 | * @return self Chaining |
||
99 | */ |
||
100 | public function baseEntityHandler($handler, array $params = array()) |
||
107 | |||
108 | /** |
||
109 | * Set external entity handler |
||
110 | * @param callback $handler |
||
111 | * @param array $params |
||
112 | * @return self Chaining |
||
113 | */ |
||
114 | public function entityHandler($handler, array $params = array()) |
||
121 | |||
122 | /** |
||
123 | * Set collection sorter parameters |
||
124 | * @param string|integer $field Field identifier or name |
||
125 | * @param string $destination ASC|DESC |
||
126 | * @return void |
||
127 | */ |
||
128 | public function sorter($field, $destination = 'ASC') |
||
147 | |||
148 | /** |
||
149 | * Filter collection using navigation entity or collection of them. |
||
150 | * If collection of navigation Url or Ids is passed then this group will be |
||
151 | * applied as single navigation filter to retrieve materials. |
||
152 | * |
||
153 | * @param string|integer|array $navigation Navigation URL or identifier for filtering |
||
154 | * @return self Chaining |
||
155 | */ |
||
156 | public function navigation($navigation) |
||
175 | |||
176 | /** |
||
177 | * Filter collection using additional field entity. |
||
178 | * |
||
179 | * @param string|integer|Field $field Additional field identifier or name |
||
180 | * @param mixed $value Additional field value for filtering |
||
181 | * @param string $relation Additional field relation for filtering |
||
182 | * @return self Chaining |
||
183 | */ |
||
184 | public function field($field, $value, $relation = dbRelation::EQUAL) |
||
205 | |||
206 | /** |
||
207 | * Filter collection using additional field entity values and LIKE relation. |
||
208 | * If this method is called more then once, it will use materials, previously filtered by this method. |
||
209 | * |
||
210 | * @param string $search Search string |
||
211 | * @return self Chaining |
||
212 | */ |
||
213 | public function search($search) |
||
223 | |||
224 | /** |
||
225 | * Filter collection of numeric field in range from min to max values |
||
226 | * @param string|integer|Field $field Additional field identifier or name |
||
227 | * @param integer $minValue Min value for range filter |
||
228 | * @param integer $maxValue Max value for range filter |
||
229 | * @return self Chaining |
||
230 | */ |
||
231 | public function ranged($field, $minValue, $maxValue) |
||
255 | |||
256 | /** |
||
257 | * Try to find additional field record |
||
258 | * @param string|integer $field Additional field identifier or name |
||
259 | * @return bool True if field record has been found |
||
260 | */ |
||
261 | protected function isFieldObject(&$field) |
||
276 | |||
277 | /** |
||
278 | * Try to get all material identifiers filtered by navigation |
||
279 | * if no navigation filtering is set - nothing will happen. |
||
280 | * |
||
281 | * @param array $filteredIds Collection of filtered material identifiers |
||
282 | * @return bool True if ALL navigation filtering succeeded or there was no filtering at all otherwise false |
||
283 | */ |
||
284 | View Code Duplication | protected function applyNavigationFilter(& $filteredIds = array()) |
|
310 | |||
311 | /** |
||
312 | * Try to get all material identifiers filtered by additional field |
||
313 | * if no field filtering is set - nothing will happen. |
||
314 | * |
||
315 | * @param array $filteredIds Collection of filtered material identifiers |
||
316 | * @return bool True if ALL field filtering succeeded or there was no filtering at all otherwise false |
||
317 | */ |
||
318 | View Code Duplication | protected function applyFieldFilter(& $filteredIds = array()) |
|
343 | |||
344 | /** |
||
345 | * Try to find all materials which have fields similar to search strings |
||
346 | * |
||
347 | * @param array $filteredIds Collection of filtered material identifiers |
||
348 | * @return bool True if ALL field filtering succeeded or there was no filtering at all otherwise false |
||
349 | */ |
||
350 | protected function applySearchFilter(& $filteredIds = array()) |
||
419 | |||
420 | /** |
||
421 | * Apply all possible material filters |
||
422 | * @param array $filteredIds Collection of material identifiers |
||
423 | * @return bool True if ALL filtering succeeded or there was no filtering at all otherwise false |
||
424 | */ |
||
425 | protected function applyFilter(& $filteredIds = array()) |
||
431 | |||
432 | /** |
||
433 | * Perform material identifiers collection sorting |
||
434 | * @param array $materialIDs Variable to return sorted collection |
||
435 | */ |
||
436 | protected function applyFieldSorter(&$materialIDs = array()) |
||
451 | |||
452 | /** |
||
453 | * Call handlers stack |
||
454 | * @param array $handlers Collection of callbacks with their parameters |
||
455 | * @param array $params External parameters to pass to callback at first |
||
456 | * @return bool True if all handlers succeeded |
||
457 | */ |
||
458 | protected function callHandlers(& $handlers = array(), $params = array()) |
||
474 | |||
475 | /** |
||
476 | * Perform filtering on base material entity |
||
477 | * @param array $materialIDs Variable to return sorted collection |
||
478 | */ |
||
479 | protected function applyBaseEntityFilter(& $materialIDs = array()) |
||
507 | |||
508 | /** |
||
509 | * Perform collection database retrieval using set filters |
||
510 | * |
||
511 | * @return self Chaining |
||
512 | */ |
||
513 | public function fill() |
||
575 | } |
||
576 |
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: