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 |
||
| 24 | class Collection extends Paged |
||
| 25 | { |
||
| 26 | /** @var string Entity manager instance */ |
||
| 27 | protected $managerEntity = Generic::class; |
||
| 28 | |||
| 29 | /** @var array Collection for current filtered material identifiers */ |
||
| 30 | protected $materialIDs = array(); |
||
| 31 | |||
| 32 | /** @var array Collection of navigation filters */ |
||
| 33 | protected $navigation = array(); |
||
| 34 | |||
| 35 | /** @var array Collection of field filters */ |
||
| 36 | protected $field = array(); |
||
| 37 | |||
| 38 | /** @var array Collection of query handlers */ |
||
| 39 | protected $idHandlers = array(); |
||
| 40 | |||
| 41 | /** @var array External material handler and params array */ |
||
| 42 | protected $entityHandlers = array(); |
||
| 43 | |||
| 44 | /** @var array Base material entity handler callbacks array */ |
||
| 45 | protected $baseEntityHandlers = array(); |
||
| 46 | |||
| 47 | /** @var string Collection entities class name */ |
||
| 48 | protected $entityName = Material::class; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Generic collection constructor |
||
| 52 | * |
||
| 53 | * @param RenderInterface $renderer View render object |
||
| 54 | * @param QueryInterface $query Query object |
||
| 55 | */ |
||
| 56 | public function __construct(RenderInterface $renderer, QueryInterface $query, PagerInterface $pager) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Render products collection block |
||
| 64 | * |
||
| 65 | * @param string $prefix Prefix for view variables |
||
| 66 | * @param array $restricted Collection of ignored keys |
||
| 67 | * |
||
| 68 | * @return array Collection key => value |
||
| 69 | */ |
||
| 70 | public function toView($prefix = null, array $restricted = array()) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Add external identifier filter handler |
||
| 81 | * |
||
| 82 | * @param callback $handler |
||
| 83 | * @param array $params |
||
| 84 | * |
||
| 85 | * @return $this Chaining |
||
| 86 | */ |
||
| 87 | public function handler($handler, array $params = array()) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Set external entity handler |
||
| 97 | * |
||
| 98 | * @param callback $handler |
||
| 99 | * @param array $params |
||
| 100 | * |
||
| 101 | * @return $this Chaining |
||
| 102 | */ |
||
| 103 | public function baseEntityHandler($handler, array $params = array()) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Set external entity handler |
||
| 113 | * |
||
| 114 | * @param callback $handler |
||
| 115 | * @param array $params |
||
| 116 | * |
||
| 117 | * @return $this Chaining |
||
| 118 | */ |
||
| 119 | public function entityHandler($handler, array $params = array()) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Set collection sorter parameters |
||
| 129 | * |
||
| 130 | * @param string|integer $field Field identifier or name |
||
| 131 | * @param string $destination ASC|DESC |
||
| 132 | * |
||
| 133 | * @return void |
||
| 134 | */ |
||
| 135 | public function sorter($field, $destination = 'ASC') |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Filter collection using navigation entity or collection of them. |
||
| 157 | * If collection of navigation Url or Ids is passed then this group will be |
||
| 158 | * applied as single navigation filter to retrieve materials. |
||
| 159 | * |
||
| 160 | * @param string|integer|array $navigation Navigation URL or identifier for filtering |
||
| 161 | * |
||
| 162 | * @return $this Chaining |
||
| 163 | */ |
||
| 164 | public function navigation($navigation) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Filter collection using additional field entity. |
||
| 186 | * |
||
| 187 | * @param string|integer|Field $field Additional field identifier or name |
||
| 188 | * @param mixed $value Additional field value for filtering |
||
| 189 | * @param string $relation Additional field relation for filtering |
||
| 190 | * |
||
| 191 | * @return $this Chaining |
||
| 192 | */ |
||
| 193 | public function field($field, $value, $relation = Relation::EQUAL) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Filter collection using additional field entity values and LIKE relation. |
||
| 217 | * If this method is called more then once, it will use materials, previously filtered by this method. |
||
| 218 | * |
||
| 219 | * @param string $search Search string |
||
| 220 | * |
||
| 221 | * @return $this Chaining |
||
| 222 | */ |
||
| 223 | public function search($search) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Filter collection of numeric field in range from min to max values |
||
| 236 | * |
||
| 237 | * @param string|integer|Field $field Additional field identifier or name |
||
| 238 | * @param integer $minValue Min value for range filter |
||
| 239 | * @param integer $maxValue Max value for range filter |
||
| 240 | * |
||
| 241 | * @return $this Chaining |
||
| 242 | */ |
||
| 243 | public function ranged($field, $minValue, $maxValue) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Try to find additional field record |
||
| 270 | * |
||
| 271 | * @param string|integer $field Additional field identifier or name |
||
| 272 | * |
||
| 273 | * @return bool True if field record has been found |
||
| 274 | */ |
||
| 275 | protected function isFieldObject(&$field) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Try to get all material identifiers filtered by navigation |
||
| 293 | * if no navigation filtering is set - nothing will happen. |
||
| 294 | * |
||
| 295 | * @param array $filteredIds Collection of filtered material identifiers |
||
| 296 | * |
||
| 297 | * @return bool True if ALL navigation filtering succeeded or there was no filtering at all otherwise false |
||
| 298 | */ |
||
| 299 | View Code Duplication | protected function applyNavigationFilter(&$filteredIds = array()) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Try to get all material identifiers filtered by additional field |
||
| 326 | * if no field filtering is set - nothing will happen. |
||
| 327 | * |
||
| 328 | * @param array $filteredIds Collection of filtered material identifiers |
||
| 329 | * |
||
| 330 | * @return bool True if ALL field filtering succeeded or there was no filtering at all otherwise false |
||
| 331 | */ |
||
| 332 | View Code Duplication | protected function applyFieldFilter(&$filteredIds = array()) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Try to find all materials which have fields similar to search strings |
||
| 359 | * |
||
| 360 | * @param array $filteredIds Collection of filtered material identifiers |
||
| 361 | * |
||
| 362 | * @return bool True if ALL field filtering succeeded or there was no filtering at all otherwise false |
||
| 363 | */ |
||
| 364 | protected function applySearchFilter(&$filteredIds = array()) |
||
| 365 | { |
||
| 366 | /** @var array $fields Variable to store all fields related to set navigation */ |
||
| 367 | $fields = array(); |
||
| 368 | /** @var array $navigationArray Array of set navigation identifiers */ |
||
| 369 | $navigationArray = array(); |
||
| 370 | /** @var array $fieldFilter Array of filtered material identifiers via materialfield table */ |
||
| 371 | $fieldFilter = array(); |
||
| 372 | /** @var array $materialFilter Array of filtered material identifiers via material table */ |
||
| 373 | $materialFilter = array(); |
||
| 374 | |||
| 375 | // If there are at least one search string |
||
| 376 | if (!empty($this->search)) { |
||
| 377 | // Create array containing all navigation identifiers |
||
| 378 | foreach ($this->navigation as $navigation) { |
||
| 379 | // Navigation hook for searching action |
||
| 380 | $navigation = is_array($navigation) ? $navigation : array($navigation); |
||
| 381 | $navigationArray = array_merge($navigationArray, $navigation); |
||
| 382 | } |
||
| 383 | |||
| 384 | // Get all related fields |
||
| 385 | $this->query->entity('\samson\activerecord\structurefield') |
||
| 386 | ->where('StructureID', $navigationArray) |
||
| 387 | ->groupBy('FieldID') |
||
| 388 | ->fields('FieldID', $fields); |
||
| 389 | |||
| 390 | // Iterate over search strings |
||
| 391 | foreach ($this->search as $searchString) { |
||
| 392 | // Try to find search value in materialfield table |
||
| 393 | $this->query->entity('\samson\activerecord\materialfield') |
||
| 394 | ->where('FieldID', $fields) |
||
| 395 | ->where('MaterialID', $filteredIds) |
||
| 396 | ->where('Value', '%' . $searchString . '%', Relation::LIKE) |
||
| 397 | ->where('Active', 1) |
||
| 398 | ->groupBy('MaterialID') |
||
| 399 | ->fields('MaterialID', $fieldFilter); |
||
| 400 | |||
| 401 | // TODO: Add generic support for all native fields or their configuration |
||
| 402 | // Condition to search in material table by Name and URL |
||
| 403 | $materialCondition = new Condition('OR'); |
||
| 404 | $materialCondition->add('Name', '%' . $searchString . '%', Relation::LIKE) |
||
| 405 | ->add('Url', '%' . $searchString . '%', Relation::LIKE); |
||
| 406 | |||
| 407 | |||
| 408 | // Try to find search value in material table |
||
| 409 | $this->query->entity('\samson\activerecord\material') |
||
| 410 | ->whereCondition($materialCondition) |
||
| 411 | ->where('Active', 1); |
||
| 412 | |||
| 413 | // If we have not empty collection of filtering identifiers |
||
| 414 | if (count($filteredIds)) { |
||
| 415 | $this->query->where('MaterialID', $filteredIds); |
||
| 416 | } |
||
| 417 | |||
| 418 | $materialFilter = $this->query->fields('MaterialID'); |
||
| 419 | |||
| 420 | // If there are no materials with specified conditions |
||
| 421 | if (empty($materialFilter) && empty($fieldFilter) && count($materialFilter) != 0 && count($fieldFilter != 0)) { |
||
| 422 | // Filter applying failed |
||
| 423 | return false; |
||
| 424 | } else {// Otherwise set filtered material identifiers |
||
| 425 | $filteredIds = array_unique(array_merge($materialFilter, $fieldFilter)); |
||
| 426 | } |
||
| 427 | } |
||
| 428 | } |
||
| 429 | |||
| 430 | // We have no search collection filters |
||
| 431 | return true; |
||
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Apply all possible material filters |
||
| 436 | * |
||
| 437 | * @param array $filteredIds Collection of material identifiers |
||
| 438 | * |
||
| 439 | * @return bool True if ALL filtering succeeded or there was no filtering at all otherwise false |
||
| 440 | */ |
||
| 441 | protected function applyFilter(& $filteredIds = array()) |
||
| 442 | { |
||
| 443 | return $this->applyNavigationFilter($filteredIds) |
||
| 444 | && $this->applyFieldFilter($filteredIds) |
||
| 445 | && $this->applySearchFilter($filteredIds) |
||
| 446 | && $this->applyMaterialSorter($filteredIds); |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Perform material identifiers collection sorting |
||
| 451 | * |
||
| 452 | * @param array $materialIDs Variable to return sorted collection |
||
| 453 | */ |
||
| 454 | protected function applyFieldSorter(&$materialIDs = array()) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Perform material own fields sorting |
||
| 472 | * |
||
| 473 | * @param array $materialIDs Variable to return sorted collection |
||
| 474 | * |
||
| 475 | * @return bool Always true as we are just sorting |
||
| 476 | */ |
||
| 477 | protected function applyMaterialSorter(&$materialIDs = array()) |
||
| 478 | { |
||
| 479 | // Check if sorter is configured |
||
| 480 | if (count($this->sorter)) { |
||
| 481 | // If we need to sort by entity additional field(column) |
||
| 482 | if (in_array($this->sorter['field'], \samson\activerecord\material::$_attributes)) { |
||
| 483 | // Sort material identifiers by its additional fields |
||
| 484 | $this->query->entity('\samson\activerecord\material') |
||
| 485 | ->where('MaterialID', $materialIDs) |
||
| 486 | ->orderBy($this->sorter['field'], $this->sorter['destination']) |
||
| 487 | ->fields('MaterialID', $materialIDs); |
||
| 488 | } |
||
| 489 | } |
||
| 490 | |||
| 491 | return true; |
||
| 492 | } |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Call handlers stack |
||
| 496 | * |
||
| 497 | * @param array $handlers Collection of callbacks with their parameters |
||
| 498 | * @param array $params External parameters to pass to callback at first |
||
| 499 | * |
||
| 500 | * @return bool True if all handlers succeeded |
||
| 501 | */ |
||
| 502 | protected function callHandlers(&$handlers = array(), $params = array()) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Perform filtering on base material entity |
||
| 518 | * |
||
| 519 | * @param array $materialIDs Variable to return sorted collection |
||
| 520 | */ |
||
| 521 | protected function applyBaseEntityFilter(&$materialIDs = array()) |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Perform collection database retrieval using set filters |
||
| 551 | * |
||
| 552 | * @return $this Chaining |
||
| 553 | */ |
||
| 554 | public function fill() |
||
| 620 | } |
||
| 621 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: