Complex classes like EagerLoader 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 EagerLoader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class EagerLoader { |
||
|
|
|||
| 8 | |||
| 9 | private static $handlers = array(); // @codingStandardsIgnoreLine |
||
| 10 | |||
| 11 | private $id; // @codingStandardsIgnoreLine |
||
| 12 | |||
| 13 | private $metas = array(); // @codingStandardsIgnoreLine |
||
| 14 | |||
| 15 | private $containOptions = array( // @codingStandardsIgnoreLine |
||
| 16 | 'conditions' => 1, |
||
| 17 | 'fields' => 1, |
||
| 18 | 'order' => 1, |
||
| 19 | 'limit' => 1, |
||
| 20 | 'offset' => 1, |
||
| 21 | ); |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Constructor |
||
| 25 | */ |
||
| 26 | public function __construct() { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Handles beforeFind event |
||
| 33 | * |
||
| 34 | * @param Model $model Model |
||
| 35 | * @param array $query Query |
||
| 36 | * @return array Modified query |
||
| 37 | */ |
||
| 38 | public static function handleBeforeFind(Model $model, $query) { |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Handles afterFind event |
||
| 60 | * |
||
| 61 | * @param Model $model Model |
||
| 62 | * @param array $results Results |
||
| 63 | * @return array Modified results |
||
| 64 | * @throws UnexpectedValueException |
||
| 65 | */ |
||
| 66 | public static function handleAfterFind(Model $model, $results) { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Returns object ids |
||
| 85 | * |
||
| 86 | * @return array |
||
| 87 | */ |
||
| 88 | private static function ids() { // @codingStandardsIgnoreLine |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Modifies the passed query to fetch the top level attachable associations. |
||
| 98 | * |
||
| 99 | * @param Model $model Model |
||
| 100 | * @param array $query Query |
||
| 101 | * @return array Modified query |
||
| 102 | */ |
||
| 103 | private function transformQuery(Model $model, array $query) { // @codingStandardsIgnoreLine |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Modifies the results |
||
| 124 | * |
||
| 125 | * @param Model $model Model |
||
| 126 | * @param array $results Results |
||
| 127 | * @return array Modified results |
||
| 128 | */ |
||
| 129 | private function transformResults(Model $model, array $results) { // @codingStandardsIgnoreLine |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Modifies the query to fetch attachable associations. |
||
| 138 | * |
||
| 139 | * @param Model $model Model |
||
| 140 | * @param string $path The target path of the model, such as 'User.Article' |
||
| 141 | * @param array $query Query |
||
| 142 | * @return array Modified query |
||
| 143 | */ |
||
| 144 | private function attachAssociations(Model $model, $path, array $query) { // @codingStandardsIgnoreLine |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Fetches meta data |
||
| 164 | * |
||
| 165 | * @param string $path Path of the association |
||
| 166 | * @return array |
||
| 167 | */ |
||
| 168 | private function metas($path) { // @codingStandardsIgnoreLine |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Fetches external associations |
||
| 177 | * |
||
| 178 | * @param Model $model Model |
||
| 179 | * @param string $path The target path of the external primary model, such as 'User.Article' |
||
| 180 | * @param array $results The results of the parent model |
||
| 181 | * @return array |
||
| 182 | */ |
||
| 183 | protected function loadExternal(Model $model, $path, array $results) { // @codingStandardsIgnoreLine |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Merges results of external associations of an external association |
||
| 199 | * |
||
| 200 | * @param Model $model Model |
||
| 201 | * @param array $results Results |
||
| 202 | * @param array $meta Meta data to be used for eager loading |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | private function mergeExternalExternal(Model $model, array $results, array $meta) { // @codingStandardsIgnoreLine |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Merges results of external associations of an internal association |
||
| 284 | * |
||
| 285 | * @param Model $model Model |
||
| 286 | * @param array $results Results |
||
| 287 | * @param array $meta Meta data to be used for eager loading |
||
| 288 | * @return array |
||
| 289 | */ |
||
| 290 | private function mergeInternalExternal(Model $model, array $results, array $meta) { // @codingStandardsIgnoreLine |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Merges associated result |
||
| 325 | * |
||
| 326 | * @param array $result Results |
||
| 327 | * @param array $assoc Associated results |
||
| 328 | * @param string $propertyPath Path of the results |
||
| 329 | * @return array |
||
| 330 | */ |
||
| 331 | private function mergeAssocResult(array $result, array $assoc, $propertyPath) { // @codingStandardsIgnoreLine |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Reformat `contain` array |
||
| 337 | * |
||
| 338 | * @param array|string $contain The value of `contain` option of the query |
||
| 339 | * @return array |
||
| 340 | */ |
||
| 341 | private function reformatContain($contain) { // @codingStandardsIgnoreLine |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Normalizes the query |
||
| 371 | * |
||
| 372 | * @param Model $model Model |
||
| 373 | * @param array $query Query |
||
| 374 | * @return array Normalized query |
||
| 375 | */ |
||
| 376 | private function normalizeQuery(Model $model, array $query) { // @codingStandardsIgnoreLine |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Normalize field |
||
| 429 | * |
||
| 430 | * @param Model $model Model |
||
| 431 | * @param string $field Name of the field |
||
| 432 | * @return string |
||
| 433 | */ |
||
| 434 | private function normalizeField(Model $model, $field) { // @codingStandardsIgnoreLine |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Modifies the query to apply joins. |
||
| 448 | * |
||
| 449 | * @param Model $target Model to be joined |
||
| 450 | * @param array $query Query |
||
| 451 | * @param string $joinType The type for join |
||
| 452 | * @param array $keys Key fields being used for join |
||
| 453 | * @param array $options Extra options for join |
||
| 454 | * @return array Modified query |
||
| 455 | */ |
||
| 456 | private function buildJoinQuery(Model $target, array $query, $joinType, array $keys, array $options) { // @codingStandardsIgnoreLine |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Adds a field into the `fields` option of the query |
||
| 480 | * |
||
| 481 | * @param array $query Query |
||
| 482 | * @param string $field Name of the field |
||
| 483 | * @return Modified query |
||
| 484 | */ |
||
| 485 | private function addField(array $query, $field) { // @codingStandardsIgnoreLine |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Parse the `contain` option of the query recursively |
||
| 494 | * |
||
| 495 | * @param Model $parent Parent model of the contained model |
||
| 496 | * @param string $alias Alias of the contained model |
||
| 497 | * @param array $contain Reformatted `contain` option for the deep associations |
||
| 498 | * @param array|null $context Context |
||
| 499 | * @return array |
||
| 500 | * @throws InvalidArgumentException |
||
| 501 | */ |
||
| 502 | private function parseContain(Model $parent, $alias, array $contain, $context = null) { // @codingStandardsIgnoreLine |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Returns whether the target is external or not |
||
| 595 | * |
||
| 596 | * @param array $context Context |
||
| 597 | * @param array $meta Meta data to be used for eager loading |
||
| 598 | * @return bool |
||
| 599 | */ |
||
| 600 | private function isExternal(array $context, array $meta) { // @codingStandardsIgnoreLine |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Returns where `limit` or `offset` option exists |
||
| 633 | * |
||
| 634 | * @param array $options Options |
||
| 635 | * @return bool |
||
| 636 | */ |
||
| 637 | private function hasLimitOffset($options) { // @codingStandardsIgnoreLine |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Triggers afterFind() method |
||
| 643 | * |
||
| 644 | * @param Model $parent Model |
||
| 645 | * @param string $alias Alias |
||
| 646 | * @param array $results Results |
||
| 647 | * @return array |
||
| 648 | */ |
||
| 649 | private function filterResults(Model $parent, $alias, array $results) { // @codingStandardsIgnoreLine |
||
| 654 | } |
||
| 655 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.