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 |
||
| 23 | class Collection extends Paged |
||
| 24 | { |
||
| 25 | /** @var string Entity manager instance */ |
||
| 26 | protected $managerEntity = '\samsoncms\api\query\Generic'; |
||
| 27 | |||
| 28 | /** @var array Collection for current filtered material identifiers */ |
||
| 29 | protected $materialIDs = array(); |
||
| 30 | |||
| 31 | /** @var array Collection of navigation filters */ |
||
| 32 | protected $navigation = array(); |
||
| 33 | |||
| 34 | /** @var array Collection of field filters */ |
||
| 35 | protected $field = array(); |
||
| 36 | |||
| 37 | /** @var array Collection of query handlers */ |
||
| 38 | protected $idHandlers = array(); |
||
| 39 | |||
| 40 | /** @var array External material handler and params array */ |
||
| 41 | protected $entityHandlers = array(); |
||
| 42 | |||
| 43 | /** @var array Base material entity handler callbacks array */ |
||
| 44 | protected $baseEntityHandlers = array(); |
||
| 45 | |||
| 46 | /** @var string Collection entities class name */ |
||
| 47 | protected $entityName = 'samson\cms\CMSMaterial'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Generic collection constructor |
||
| 51 | * @param RenderInterface $renderer View render object |
||
| 52 | * @param QueryInterface $query Query object |
||
| 53 | */ |
||
| 54 | public function __construct(RenderInterface $renderer, QueryInterface $query, PagerInterface $pager) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Render products collection block |
||
| 62 | * @param string $prefix Prefix for view variables |
||
| 63 | * @param array $restricted Collection of ignored keys |
||
| 64 | * @return array Collection key => value |
||
| 65 | */ |
||
| 66 | public function toView($prefix = null, array $restricted = array()) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Add external identifier filter handler |
||
| 77 | * @param callback $handler |
||
| 78 | * @param array $params |
||
| 79 | * @return self Chaining |
||
| 80 | */ |
||
| 81 | public function handler($handler, array $params = array()) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Set external entity handler |
||
| 91 | * @param callback $handler |
||
| 92 | * @param array $params |
||
| 93 | * @return self Chaining |
||
| 94 | */ |
||
| 95 | public function baseEntityHandler($handler, array $params = array()) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Set external entity handler |
||
| 105 | * @param callback $handler |
||
| 106 | * @param array $params |
||
| 107 | * @return self Chaining |
||
| 108 | */ |
||
| 109 | public function entityHandler($handler, array $params = array()) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Set collection sorter parameters |
||
| 119 | * @param string|integer $field Field identifier or name |
||
| 120 | * @param string $destination ASC|DESC |
||
| 121 | * @return void |
||
| 122 | */ |
||
| 123 | public function sorter($field, $destination = 'ASC') |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Filter collection using navigation entity or collection of them. |
||
| 145 | * If collection of navigation Url or Ids is passed then this group will be |
||
| 146 | * applied as single navigation filter to retrieve materials. |
||
| 147 | * |
||
| 148 | * @param string|integer|array $navigation Navigation URL or identifier for filtering |
||
| 149 | * @return self Chaining |
||
| 150 | */ |
||
| 151 | public function navigation($navigation) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Filter collection using additional field entity. |
||
| 173 | * |
||
| 174 | * @param string|integer|Field $field Additional field identifier or name |
||
| 175 | * @param mixed $value Additional field value for filtering |
||
| 176 | * @param string $relation Additional field relation for filtering |
||
| 177 | * @return self Chaining |
||
| 178 | */ |
||
| 179 | public function field($field, $value, $relation = Relation::EQUAL) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Filter collection using additional field entity values and LIKE relation. |
||
| 203 | * If this method is called more then once, it will use materials, previously filtered by this method. |
||
| 204 | * |
||
| 205 | * @param string $search Search string |
||
| 206 | * @return self Chaining |
||
| 207 | */ |
||
| 208 | public function search($search) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Filter collection of numeric field in range from min to max values |
||
| 221 | * @param string|integer|Field $field Additional field identifier or name |
||
| 222 | * @param integer $minValue Min value for range filter |
||
| 223 | * @param integer $maxValue Max value for range filter |
||
| 224 | * @return self Chaining |
||
| 225 | */ |
||
| 226 | public function ranged($field, $minValue, $maxValue) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Try to find additional field record |
||
| 253 | * @param string|integer $field Additional field identifier or name |
||
| 254 | * @return bool True if field record has been found |
||
| 255 | */ |
||
| 256 | protected function isFieldObject(&$field) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Try to get all material identifiers filtered by navigation |
||
| 274 | * if no navigation filtering is set - nothing will happen. |
||
| 275 | * |
||
| 276 | * @param array $filteredIds Collection of filtered material identifiers |
||
| 277 | * @return bool True if ALL navigation filtering succeeded or there was no filtering at all otherwise false |
||
| 278 | */ |
||
| 279 | View Code Duplication | protected function applyNavigationFilter(& $filteredIds = array()) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Try to get all material identifiers filtered by additional field |
||
| 308 | * if no field filtering is set - nothing will happen. |
||
| 309 | * |
||
| 310 | * @param array $filteredIds Collection of filtered material identifiers |
||
| 311 | * @return bool True if ALL field filtering succeeded or there was no filtering at all otherwise false |
||
| 312 | */ |
||
| 313 | View Code Duplication | protected function applyFieldFilter(& $filteredIds = array()) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Try to find all materials which have fields similar to search strings |
||
| 341 | * |
||
| 342 | * @param array $filteredIds Collection of filtered material identifiers |
||
| 343 | * @return bool True if ALL field filtering succeeded or there was no filtering at all otherwise false |
||
| 344 | */ |
||
| 345 | protected function applySearchFilter(& $filteredIds = array()) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Apply all possible material filters |
||
| 417 | * @param array $filteredIds Collection of material identifiers |
||
| 418 | * @return bool True if ALL filtering succeeded or there was no filtering at all otherwise false |
||
| 419 | */ |
||
| 420 | protected function applyFilter(& $filteredIds = array()) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Perform material identifiers collection sorting |
||
| 429 | * @param array $materialIDs Variable to return sorted collection |
||
| 430 | */ |
||
| 431 | protected function applyFieldSorter(&$materialIDs = array()) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Call handlers stack |
||
| 449 | * @param array $handlers Collection of callbacks with their parameters |
||
| 450 | * @param array $params External parameters to pass to callback at first |
||
| 451 | * @return bool True if all handlers succeeded |
||
| 452 | */ |
||
| 453 | protected function callHandlers(& $handlers = array(), $params = array()) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Perform filtering on base material entity |
||
| 472 | * @param array $materialIDs Variable to return sorted collection |
||
| 473 | */ |
||
| 474 | protected function applyBaseEntityFilter(& $materialIDs = array()) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Perform collection database retrieval using set filters |
||
| 505 | * |
||
| 506 | * @return self Chaining |
||
| 507 | */ |
||
| 508 | public function fill() |
||
| 570 | } |
||
| 571 |
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: