Complex classes like EavBehavior 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 EavBehavior, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 76 | class EavBehavior extends Behavior implements PropertyMarshalInterface |
||
| 77 | { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Instance of EavToolbox. |
||
| 81 | * |
||
| 82 | * @var \Eav\Model\Behavior\EavToolbox |
||
| 83 | */ |
||
| 84 | protected $_toolbox = null; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Represents an entity that should be removed from the collection. |
||
| 88 | * |
||
| 89 | * @var int |
||
| 90 | */ |
||
| 91 | const NULL_ENTITY = -1; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Default configuration. |
||
| 95 | * |
||
| 96 | * - enabled: Whether this behavior is active or not. Defaults true. |
||
| 97 | * |
||
| 98 | * - cache: EAV cache feature, see documentation. Defaults false. |
||
| 99 | * |
||
| 100 | * - hydrator: Callable function responsible of hydrate an entity with its |
||
| 101 | * virtual values, callable receives two arguments: the entity to hydrate and |
||
| 102 | * an array of virtual values, where each virtual value is an array composed |
||
| 103 | * of `property_name` and `value` keys. |
||
| 104 | * |
||
| 105 | * @var array |
||
| 106 | */ |
||
| 107 | protected $_defaultConfig = [ |
||
| 108 | 'status' => true, |
||
| 109 | 'cache' => false, |
||
| 110 | 'hydrator' => null, |
||
| 111 | 'queryScope' => [ |
||
| 112 | 'Eav\\Model\\Behavior\\QueryScope\\SelectScope', |
||
| 113 | 'Eav\\Model\\Behavior\\QueryScope\\WhereScope', |
||
| 114 | 'Eav\\Model\\Behavior\\QueryScope\\OrderScope', |
||
| 115 | ], |
||
| 116 | 'implementedMethods' => [ |
||
| 117 | 'eav' => 'eav', |
||
| 118 | 'updateEavCache' => 'updateEavCache', |
||
| 119 | 'addColumn' => 'addColumn', |
||
| 120 | 'dropColumn' => 'dropColumn', |
||
| 121 | 'listColumns' => 'listColumns', |
||
| 122 | ], |
||
| 123 | ]; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Query scopes objects to be applied indexed by unique ID. |
||
| 127 | * |
||
| 128 | * @var array |
||
| 129 | */ |
||
| 130 | protected $_queryScopes = []; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Constructor. |
||
| 134 | * |
||
| 135 | * @param \Cake\ORM\Table $table The table this behavior is attached to |
||
| 136 | * @param array $config Configuration array for this behavior |
||
| 137 | */ |
||
| 138 | public function __construct(Table $table, array $config = []) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Gets/sets EAV status. |
||
| 171 | * |
||
| 172 | * - TRUE: Enables EAV behavior so virtual columns WILL be fetched from database. |
||
| 173 | * - FALSE: Disables EAV behavior so virtual columns WLL NOT be fetched from database. |
||
| 174 | * |
||
| 175 | * @param bool|null $status EAV status to set, or null to get current state |
||
| 176 | * @return void|bool Current status if `$status` is set to null |
||
| 177 | */ |
||
| 178 | public function eav($status = null) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Defines a new virtual-column, or update if already defined. |
||
| 189 | * |
||
| 190 | * ### Usage: |
||
| 191 | * |
||
| 192 | * ```php |
||
| 193 | * $errors = $this->Users->addColumn('user-age', [ |
||
| 194 | * 'type' => 'integer', |
||
| 195 | * 'bundle' => 'some-bundle-name', |
||
| 196 | * 'extra' => [ |
||
| 197 | * 'option1' => 'value1' |
||
| 198 | * ] |
||
| 199 | * ], true); |
||
| 200 | * |
||
| 201 | * if (empty($errors)) { |
||
| 202 | * // OK |
||
| 203 | * } else { |
||
| 204 | * // ERROR |
||
| 205 | * debug($errors); |
||
| 206 | * } |
||
| 207 | * ``` |
||
| 208 | * |
||
| 209 | * The third argument can be set to FALSE to get a boolean response: |
||
| 210 | * |
||
| 211 | * ```php |
||
| 212 | * $success = $this->Users->addColumn('user-age', [ |
||
| 213 | * 'type' => 'integer', |
||
| 214 | * 'bundle' => 'some-bundle-name', |
||
| 215 | * 'extra' => [ |
||
| 216 | * 'option1' => 'value1' |
||
| 217 | * ] |
||
| 218 | * ]); |
||
| 219 | * |
||
| 220 | * if ($success) { |
||
| 221 | * // OK |
||
| 222 | * } else { |
||
| 223 | * // ERROR |
||
| 224 | * } |
||
| 225 | * ``` |
||
| 226 | * |
||
| 227 | * @param string $name Column name. e.g. `user-age` |
||
| 228 | * @param array $options Column configuration options |
||
| 229 | * @param bool $errors If set to true will return an array list of errors |
||
| 230 | * instead of boolean response. Defaults to TRUE |
||
| 231 | * @return bool|array True on success or array of error messages, depending on |
||
| 232 | * $error argument |
||
| 233 | * @throws \Cake\Error\FatalErrorException When provided column name collides |
||
| 234 | * with existing column names. And when an invalid type is provided |
||
| 235 | */ |
||
| 236 | public function addColumn($name, array $options = [], $errors = true) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Drops an existing column. |
||
| 287 | * |
||
| 288 | * @param string $name Name of the column to drop |
||
| 289 | * @param string|null $bundle Removes the column within a particular bundle |
||
| 290 | * @return bool True on success, false otherwise |
||
| 291 | */ |
||
| 292 | public function dropColumn($name, $bundle = null) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Gets a list of virtual columns attached to this table. |
||
| 313 | * |
||
| 314 | * @param string|null $bundle Get attributes within given bundle, or all of them |
||
| 315 | * regardless of the bundle if not provided |
||
| 316 | * @return array Columns information indexed by column name |
||
| 317 | */ |
||
| 318 | public function listColumns($bundle = null) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Update EAV cache for the specified $entity. |
||
| 337 | * |
||
| 338 | * @param \Cake\Datasource\EntityInterface $entity The entity to update |
||
| 339 | * @return bool Success |
||
| 340 | */ |
||
| 341 | public function updateEavCache(EntityInterface $entity) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Attaches virtual properties to entities. |
||
| 409 | * |
||
| 410 | * This method is also responsible of looking for virtual columns in SELECT and |
||
| 411 | * WHERE clauses (if applicable) and properly scope the Query object. Query |
||
| 412 | * scoping is performed by the `_scopeQuery()` method. |
||
| 413 | * |
||
| 414 | * EAV can be enabled or disabled on the fly using `eav` finder option, or |
||
| 415 | * `eav()` method. When mixing, `eav` option has the highest priority: |
||
| 416 | * |
||
| 417 | * ```php |
||
| 418 | * $this->Articles->eav(false); |
||
| 419 | * $articlesNoVirtual = $this->Articles->find('all'); |
||
| 420 | * $articlesWithVirtual = $this->Articles->find('all', ['eav' => true]); |
||
| 421 | * ``` |
||
| 422 | * |
||
| 423 | * @param \Cake\Event\Event $event The beforeFind event that was triggered |
||
| 424 | * @param \Cake\ORM\Query $query The original query to modify |
||
| 425 | * @param \ArrayObject $options Additional options given as an array |
||
| 426 | * @param bool $primary Whether this find is a primary query or not |
||
| 427 | * @return bool|null |
||
| 428 | */ |
||
| 429 | public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Attach EAV attributes for every entity in the provided result-set. |
||
| 453 | * |
||
| 454 | * This method iterates over each retrieved entity and invokes the |
||
| 455 | * `hydrateEntity()` method. This last should return the altered entity object |
||
| 456 | * with all its virtual properties, however if this method returns NULL the |
||
| 457 | * entity will be removed from the resulting collection. |
||
| 458 | * |
||
| 459 | * @param \Cake\Collection\CollectionInterface $entities Set of entities to be |
||
| 460 | * processed |
||
| 461 | * @param array $args Contains three keys: "options" and "primary" given to the |
||
| 462 | * originating beforeFind(), and "selectedVirtual", a list of virtual columns |
||
| 463 | * selected in the originating find query |
||
| 464 | * @return \Cake\Collection\CollectionInterface New set with altered entities |
||
| 465 | */ |
||
| 466 | protected function _hydrateEntities(CollectionInterface $entities, array $args) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Hydrates a single entity and returns it. |
||
| 494 | * |
||
| 495 | * Returning NULL indicates the entity should be removed from the resulting |
||
| 496 | * collection. |
||
| 497 | * |
||
| 498 | * @param \Cake\Datasource\EntityInterface $entity The entity to hydrate |
||
| 499 | * @param array $values Holds stored virtual values for this particular entity |
||
| 500 | * @return bool|null|\Cake\Datasource\EntityInterface |
||
| 501 | */ |
||
| 502 | public function hydrateEntity(EntityInterface $entity, array $values) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Retrieves all virtual values of all the entities within the given result-set. |
||
| 526 | * |
||
| 527 | * @param \Cake\Collection\CollectionInterface $entities Set of entities |
||
| 528 | * @param array $args Contains two keys: "options" and "primary" given to the |
||
| 529 | * originating beforeFind(), and "selectedVirtual" a list of virtual columns |
||
| 530 | * selected in the originating find query |
||
| 531 | * @return array Virtual values indexed by entity ID |
||
| 532 | */ |
||
| 533 | protected function _prepareSetValues(CollectionInterface $entities, array $args) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Triggered before data is converted into entities. |
||
| 612 | * |
||
| 613 | * Converts incoming POST data to its corresponding types. |
||
| 614 | * |
||
| 615 | * @param \Cake\Event\Event $event The event that was triggered |
||
| 616 | * @param \ArrayObject $data The POST data to be merged with entity |
||
| 617 | * @param \ArrayObject $options The options passed to the marshaller |
||
| 618 | * @return void |
||
| 619 | */ |
||
| 620 | public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options) |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Ensures that virtual properties are included in the marshalling process. |
||
| 636 | * |
||
| 637 | * @param \Cake\ORM\Marhshaller $marshaller The marhshaller of the table the behavior is attached to. |
||
| 638 | * @param array $map The property map being built. |
||
| 639 | * @param array $options The options array used in the marshalling call. |
||
| 640 | * @return array A map of `[property => callable]` of additional properties to marshal. |
||
| 641 | */ |
||
| 642 | public function buildMarshalMap($marshaller, $map, $options) |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Save virtual values after an entity's real values were saved. |
||
| 659 | * |
||
| 660 | * @param \Cake\Event\Event $event The event that was triggered |
||
| 661 | * @param \Cake\Datasource\EntityInterface $entity The entity that was saved |
||
| 662 | * @param \ArrayObject $options Additional options given as an array |
||
| 663 | * @return bool True always |
||
| 664 | */ |
||
| 665 | public function afterSave(Event $event, EntityInterface $entity, ArrayObject $options) |
||
| 737 | |||
| 738 | /** |
||
| 739 | * After an entity was removed from database. Here is when EAV values are |
||
| 740 | * removed from DB. |
||
| 741 | * |
||
| 742 | * @param \Cake\Event\Event $event The event that was triggered |
||
| 743 | * @param \Cake\Datasource\EntityInterface $entity The entity that was deleted |
||
| 744 | * @param \ArrayObject $options Additional options given as an array |
||
| 745 | * @throws \Cake\Error\FatalErrorException When using this behavior in non-atomic mode |
||
| 746 | * @return void |
||
| 747 | */ |
||
| 748 | public function afterDelete(Event $event, EntityInterface $entity, ArrayObject $options) |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Prepares entity's cache-columns (those defined using `cache` option). |
||
| 769 | * |
||
| 770 | * @param \Cake\Datasource\EntityInterface $entity The entity to prepare |
||
| 771 | * @return \Cake\Datasource\EntityInterfa Modified entity |
||
| 772 | */ |
||
| 773 | protected function _prepareCachedColumns(EntityInterface $entity) |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Look for virtual columns in some query's clauses. |
||
| 793 | * |
||
| 794 | * @param \Cake\ORM\Query $query The query to scope |
||
| 795 | * @param string|null $bundle Consider attributes only for a specific bundle |
||
| 796 | * @return \Cake\ORM\Query The modified query object |
||
| 797 | */ |
||
| 798 | protected function _scopeQuery(Query $query, $bundle = null) |
||
| 809 | |||
| 810 | /** |
||
| 811 | * Initializes the scope objects |
||
| 812 | * |
||
| 813 | * @return void |
||
| 814 | */ |
||
| 815 | protected function _initScopes() |
||
| 830 | } |
||
| 831 |