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 |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Merges results of external associations of an internal association |
||
| 280 | * |
||
| 281 | * @param Model $model Model |
||
| 282 | * @param array $results Results |
||
| 283 | * @param array $meta Meta data to be used for eager loading |
||
| 284 | * @return array |
||
| 285 | */ |
||
| 286 | private function mergeInternalExternal(Model $model, array $results, array $meta) { // @codingStandardsIgnoreLine |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Merges associated result |
||
| 321 | * |
||
| 322 | * @param array $result Results |
||
| 323 | * @param array $assoc Associated results |
||
| 324 | * @param string $propertyPath Path of the results |
||
| 325 | * @return array |
||
| 326 | */ |
||
| 327 | private function mergeAssocResult(array $result, array $assoc, $propertyPath) { // @codingStandardsIgnoreLine |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Reformat `contain` array |
||
| 333 | * |
||
| 334 | * @param array|string $contain The value of `contain` option of the query |
||
| 335 | * @return array |
||
| 336 | */ |
||
| 337 | private function reformatContain($contain) { // @codingStandardsIgnoreLine |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Normalizes the query |
||
| 367 | * |
||
| 368 | * @param Model $model Model |
||
| 369 | * @param array $query Query |
||
| 370 | * @return array Normalized query |
||
| 371 | */ |
||
| 372 | private function normalizeQuery(Model $model, array $query) { // @codingStandardsIgnoreLine |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Normalize field |
||
| 425 | * |
||
| 426 | * @param Model $model Model |
||
| 427 | * @param string $field Name of the field |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | private function normalizeField(Model $model, $field) { // @codingStandardsIgnoreLine |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Modifies the query to apply joins. |
||
| 444 | * |
||
| 445 | * @param Model $target Model to be joined |
||
| 446 | * @param array $query Query |
||
| 447 | * @param string $joinType The type for join |
||
| 448 | * @param array $keys Key fields being used for join |
||
| 449 | * @param array $options Extra options for join |
||
| 450 | * @return array Modified query |
||
| 451 | */ |
||
| 452 | private function buildJoinQuery(Model $target, array $query, $joinType, array $keys, array $options) { // @codingStandardsIgnoreLine |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Adds a field into the `fields` option of the query |
||
| 476 | * |
||
| 477 | * @param array $query Query |
||
| 478 | * @param string $field Name of the field |
||
| 479 | * @return Modified query |
||
| 480 | */ |
||
| 481 | private function addField(array $query, $field) { // @codingStandardsIgnoreLine |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Parse the `contain` option of the query recursively |
||
| 490 | * |
||
| 491 | * @param Model $parent Parent model of the contained model |
||
| 492 | * @param string $alias Alias of the contained model |
||
| 493 | * @param array $contain Reformatted `contain` option for the deep associations |
||
| 494 | * @param array|null $context Context |
||
| 495 | * @return array |
||
| 496 | * @throws InvalidArgumentException |
||
| 497 | */ |
||
| 498 | private function parseContain(Model $parent, $alias, array $contain, $context = null) { // @codingStandardsIgnoreLine |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Returns whether the target is external or not |
||
| 590 | * |
||
| 591 | * @param array $context Context |
||
| 592 | * @param array $meta Meta data to be used for eager loading |
||
| 593 | * @return bool |
||
| 594 | */ |
||
| 595 | private function isExternal(array $context, array $meta) { // @codingStandardsIgnoreLine |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Returns where `limit` or `offset` option exists |
||
| 628 | * |
||
| 629 | * @param array $options Options |
||
| 630 | * @return bool |
||
| 631 | */ |
||
| 632 | private function hasLimitOffset($options) { // @codingStandardsIgnoreLine |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Triggers afterFind() method |
||
| 638 | * |
||
| 639 | * @param Model $parent Model |
||
| 640 | * @param string $alias Alias |
||
| 641 | * @param array $results Results |
||
| 642 | * @return array |
||
| 643 | */ |
||
| 644 | private function filterResults(Model $parent, $alias, array $results) { // @codingStandardsIgnoreLine |
||
| 649 | } |
||
| 650 |
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.