Complex classes like CollectionLoader 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 CollectionLoader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class CollectionLoader implements LoggerAwareInterface |
||
| 28 | { |
||
| 29 | use LoggerAwareTrait; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The source to load objects from. |
||
| 33 | * |
||
| 34 | * @var SourceInterface |
||
| 35 | */ |
||
| 36 | private $source; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The model to load the collection from. |
||
| 40 | * |
||
| 41 | * @var ModelInterface |
||
| 42 | */ |
||
| 43 | private $model; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Store the factory instance for the current class. |
||
| 47 | * |
||
| 48 | * @var FactoryInterface |
||
| 49 | */ |
||
| 50 | private $factory; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The callback routine applied to every object added to the collection. |
||
| 54 | * |
||
| 55 | * @var callable|null |
||
| 56 | */ |
||
| 57 | private $callback; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The field which defines the data's model. |
||
| 61 | * |
||
| 62 | * @var string|null |
||
| 63 | */ |
||
| 64 | private $dynamicTypeField; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The class name of the collection to use. |
||
| 68 | * |
||
| 69 | * Must be a fully-qualified PHP namespace and an implementation of {@see ArrayAccess}. |
||
| 70 | * |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | private $collectionClass = Collection::class; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Return a new CollectionLoader object. |
||
| 77 | * |
||
| 78 | * @param array $data The loader's dependencies. |
||
| 79 | */ |
||
| 80 | public function __construct(array $data) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Set an object model factory. |
||
| 103 | * |
||
| 104 | * @param FactoryInterface $factory The model factory, to create objects. |
||
| 105 | * @return CollectionLoader Chainable |
||
| 106 | */ |
||
| 107 | public function setFactory(FactoryInterface $factory) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Retrieve the object model factory. |
||
| 116 | * |
||
| 117 | * @throws RuntimeException If the model factory was not previously set. |
||
| 118 | * @return FactoryInterface |
||
| 119 | */ |
||
| 120 | protected function factory() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Set the loader data. |
||
| 133 | * |
||
| 134 | * @param array $data Data to assign to the loader. |
||
| 135 | * @return CollectionLoader Chainable |
||
| 136 | */ |
||
| 137 | public function setData(array $data) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Retrieve the source to load objects from. |
||
| 154 | * |
||
| 155 | * @throws RuntimeException If no source has been defined. |
||
| 156 | * @return mixed |
||
| 157 | */ |
||
| 158 | public function source() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Set the source to load objects from. |
||
| 169 | * |
||
| 170 | * @param SourceInterface $source A data source. |
||
| 171 | * @return CollectionLoader Chainable |
||
| 172 | */ |
||
| 173 | public function setSource(SourceInterface $source) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Reset everything but the model. |
||
| 184 | * |
||
| 185 | * @return CollectionLoader Chainable |
||
| 186 | */ |
||
| 187 | public function reset() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Retrieve the object model. |
||
| 201 | * |
||
| 202 | * @throws RuntimeException If no model has been defined. |
||
| 203 | * @return Model |
||
| 204 | */ |
||
| 205 | public function model() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Determine if the loader has an object model. |
||
| 216 | * |
||
| 217 | * @return boolean |
||
| 218 | */ |
||
| 219 | public function hasModel() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Set the model to use for the loaded objects. |
||
| 226 | * |
||
| 227 | * @param string|ModelInterface $model An object model. |
||
| 228 | * @throws InvalidArgumentException If the given argument is not a model. |
||
| 229 | * @return CollectionLoader CHainable |
||
| 230 | */ |
||
| 231 | public function setModel($model) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param string $field The field to use for dynamic object type. |
||
| 255 | * @throws InvalidArgumentException If the field is not a string. |
||
| 256 | * @return CollectionLoader Chainable |
||
| 257 | */ |
||
| 258 | public function setDynamicTypeField($field) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Alias of {@see SourceInterface::properties()} |
||
| 273 | * |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | public function properties() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Alias of {@see SourceInterface::setProperties()} |
||
| 283 | * |
||
| 284 | * @param array $properties An array of property identifiers. |
||
| 285 | * @return CollectionLoader Chainable |
||
| 286 | */ |
||
| 287 | public function setProperties(array $properties) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Alias of {@see SourceInterface::addProperty()} |
||
| 296 | * |
||
| 297 | * @param string $property A property identifier. |
||
| 298 | * @return CollectionLoader Chainable |
||
| 299 | */ |
||
| 300 | public function addProperty($property) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Set "search" keywords to filter multiple properties. |
||
| 309 | * |
||
| 310 | * @param array $keywords An array of keywords and properties. |
||
| 311 | * @return CollectionLoader Chainable |
||
| 312 | */ |
||
| 313 | public function setKeywords(array $keywords) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Add a "search" keyword filter to multiple properties. |
||
| 326 | * |
||
| 327 | * @param string $keyword A value to match among $properties. |
||
| 328 | * @param array $properties An array of property identifiers. |
||
| 329 | * @return CollectionLoader Chainable |
||
| 330 | */ |
||
| 331 | public function addKeyword($keyword, array $properties = null) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Alias of {@see SourceInterface::filters()} |
||
| 352 | * |
||
| 353 | * @return array |
||
| 354 | */ |
||
| 355 | public function filters() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Alias of {@see SourceInterface::setFilters()} |
||
| 362 | * |
||
| 363 | * @param array $filters An array of filters. |
||
| 364 | * @return Collection Chainable |
||
| 365 | */ |
||
| 366 | public function setFilters(array $filters) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Alias of {@see SourceInterface::addFilters()} |
||
| 375 | * |
||
| 376 | * @param array $filters An array of filters. |
||
| 377 | * @return Collection Chainable |
||
| 378 | */ |
||
| 379 | public function addFilters(array $filters) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Alias of {@see SourceInterface::addFilter()} |
||
| 390 | * |
||
| 391 | * @param string|array|Filter $param A property identifier, filter array, or Filter object. |
||
| 392 | * @param mixed $val Optional. The value to match. Only used if the first argument is a string. |
||
| 393 | * @param array $options Optional. Filter options. Only used if the first argument is a string. |
||
| 394 | * @return CollectionLoader Chainable |
||
| 395 | */ |
||
| 396 | public function addFilter($param, $val = null, array $options = null) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Alias of {@see SourceInterface::orders()} |
||
| 405 | * |
||
| 406 | * @return array |
||
| 407 | */ |
||
| 408 | public function orders() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Alias of {@see SourceInterface::setOrders()} |
||
| 415 | * |
||
| 416 | * @param array $orders An array of orders. |
||
| 417 | * @return CollectionLoader Chainable |
||
| 418 | */ |
||
| 419 | public function setOrders(array $orders) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Alias of {@see SourceInterface::addOrders()} |
||
| 428 | * |
||
| 429 | * @param array $orders An array of orders. |
||
| 430 | * @return Collection Chainable |
||
| 431 | */ |
||
| 432 | public function addOrders(array $orders) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Alias of {@see SourceInterface::addOrder()} |
||
| 443 | * |
||
| 444 | * @param string|array|Order $param A property identifier, order array, or Order object. |
||
| 445 | * @param string $mode Optional. Sort order. Only used if the first argument is a string. |
||
| 446 | * @param array $orderOptions Optional. Filter options. Only used if the first argument is a string. |
||
| 447 | * @return CollectionLoader Chainable |
||
| 448 | */ |
||
| 449 | public function addOrder($param, $mode = 'asc', array $orderOptions = null) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Alias of {@see SourceInterface::pagination()} |
||
| 458 | * |
||
| 459 | * @return Pagination |
||
| 460 | */ |
||
| 461 | public function pagination() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Alias of {@see SourceInterface::setPagination()} |
||
| 468 | * |
||
| 469 | * @param mixed $param An associative array of pagination settings. |
||
| 470 | * @return CollectionLoader Chainable |
||
| 471 | */ |
||
| 472 | public function setPagination($param) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Alias of {@see PaginationInterface::page()} |
||
| 481 | * |
||
| 482 | * @return integer |
||
| 483 | */ |
||
| 484 | public function page() |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Alias of {@see PaginationInterface::pagination()} |
||
| 491 | * |
||
| 492 | * @param integer $page A page number. |
||
| 493 | * @return CollectionLoader Chainable |
||
| 494 | */ |
||
| 495 | public function setPage($page) |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Alias of {@see PaginationInterface::numPerPage()} |
||
| 504 | * |
||
| 505 | * @return integer |
||
| 506 | */ |
||
| 507 | public function numPerPage() |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Alias of {@see PaginationInterface::setNumPerPage()} |
||
| 514 | * |
||
| 515 | * @param integer $num The number of items to display per page. |
||
| 516 | * @return CollectionLoader Chainable |
||
| 517 | */ |
||
| 518 | public function setNumPerPage($num) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Set the callback routine applied to every object added to the collection. |
||
| 527 | * |
||
| 528 | * @param callable $callback The callback routine. |
||
| 529 | * @return CollectionLoader Chainable |
||
| 530 | */ |
||
| 531 | public function setCallback(callable $callback) |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Retrieve the callback routine applied to every object added to the collection. |
||
| 540 | * |
||
| 541 | * @return callable|null |
||
| 542 | */ |
||
| 543 | public function callback() |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Load a collection from source. |
||
| 550 | * |
||
| 551 | * @param string|null $ident Optional. A pre-defined list to use from the model. |
||
| 552 | * @param callable|null $callback Process each entity after applying raw data. |
||
| 553 | * Leave blank to use {@see CollectionLoader::callback()}. |
||
| 554 | * @param callable|null $before Process each entity before applying raw data. |
||
| 555 | * @throws Exception If the database connection fails. |
||
| 556 | * @return ModelInterface[]|ArrayAccess |
||
| 557 | */ |
||
| 558 | public function load($ident = null, callable $callback = null, callable $before = null) |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Get the total number of items for this collection query. |
||
| 570 | * |
||
| 571 | * @throws RuntimeException If the database connection fails. |
||
| 572 | * @return integer |
||
| 573 | */ |
||
| 574 | public function loadCount() |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Load list from query. |
||
| 595 | * |
||
| 596 | * **Example — Binding values to $query** |
||
| 597 | * |
||
| 598 | * ```php |
||
| 599 | * $this->loadFromQuery([ |
||
| 600 | * 'SELECT name, colour, calories FROM fruit WHERE calories < :calories AND colour = :colour', |
||
| 601 | * [ |
||
| 602 | * 'calories' => 150, |
||
| 603 | * 'colour' => 'red' |
||
| 604 | * ], |
||
| 605 | * [ 'calories' => PDO::PARAM_INT ] |
||
| 606 | * ]); |
||
| 607 | * ``` |
||
| 608 | * |
||
| 609 | * @param string|array $query The SQL query as a string or an array composed of the query, |
||
| 610 | * parameter binds, and types of parameter bindings. |
||
| 611 | * @param callable|null $callback Process each entity after applying raw data. |
||
| 612 | * Leave blank to use {@see CollectionLoader::callback()}. |
||
| 613 | * @param callable|null $before Process each entity before applying raw data. |
||
| 614 | * @throws RuntimeException If the database connection fails. |
||
| 615 | * @throws InvalidArgumentException If the SQL string/set is invalid. |
||
| 616 | * @return ModelInterface[]|ArrayAccess |
||
| 617 | */ |
||
| 618 | public function loadFromQuery($query, callable $callback = null, callable $before = null) |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Process the collection of raw data. |
||
| 656 | * |
||
| 657 | * @param mixed[]|Traversable $results The raw result set. |
||
| 658 | * @param callable|null $before Process each entity before applying raw data. |
||
| 659 | * @param callable|null $after Process each entity after applying raw data. |
||
| 660 | * @return ModelInterface[]|ArrayAccess |
||
| 661 | */ |
||
| 662 | protected function processCollection($results, callable $before = null, callable $after = null) |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Process the raw data for one model. |
||
| 678 | * |
||
| 679 | * @param mixed $objData The raw dataset. |
||
| 680 | * @param callable|null $before Process each entity before applying raw data. |
||
| 681 | * @param callable|null $after Process each entity after applying raw data. |
||
| 682 | * @return ModelInterface|ArrayAccess|null |
||
| 683 | */ |
||
| 684 | protected function processModel($objData, callable $before = null, callable $after = null) |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Create a collection class or array. |
||
| 709 | * |
||
| 710 | * @throws RuntimeException If the collection class is invalid. |
||
| 711 | * @return array|ArrayAccess |
||
| 712 | */ |
||
| 713 | public function createCollection() |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Set the class name of the collection. |
||
| 741 | * |
||
| 742 | * @param string $className The class name of the collection. |
||
| 743 | * @throws InvalidArgumentException If the class name is not a string. |
||
| 744 | * @return AbstractPropertyDisplay Chainable |
||
| 745 | */ |
||
| 746 | public function setCollectionClass($className) |
||
| 758 | |||
| 759 | /** |
||
| 760 | * Retrieve the class name of the collection. |
||
| 761 | * |
||
| 762 | * @return string |
||
| 763 | */ |
||
| 764 | public function collectionClass() |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Allow an object to define how the key getter are called. |
||
| 771 | * |
||
| 772 | * @param string $key The key to get the getter from. |
||
| 773 | * @return string The getter method name, for a given key. |
||
| 774 | */ |
||
| 775 | protected function getter($key) |
||
| 780 | |||
| 781 | /** |
||
| 782 | * Allow an object to define how the key setter are called. |
||
| 783 | * |
||
| 784 | * @param string $key The key to get the setter from. |
||
| 785 | * @return string The setter method name, for a given key. |
||
| 786 | */ |
||
| 787 | protected function setter($key) |
||
| 792 | |||
| 793 | /** |
||
| 794 | * Transform a snake_case string to camelCase. |
||
| 795 | * |
||
| 796 | * @param string $str The snake_case string to camelize. |
||
| 797 | * @return string The camelcase'd string. |
||
| 798 | */ |
||
| 799 | protected function camelize($str) |
||
| 803 | } |
||
| 804 |
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: