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 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 |
||
| 74 | class EavBehavior extends Behavior |
||
| 75 | { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Instance of EavToolbox. |
||
| 79 | * |
||
| 80 | * @var \Eav\Model\Behavior\EavToolbox |
||
| 81 | */ |
||
| 82 | protected $_toolbox = null; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Represents an entity that should be removed from the collection. |
||
| 86 | * |
||
| 87 | * @var int |
||
| 88 | */ |
||
| 89 | const NULL_ENTITY = -1; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Default configuration. |
||
| 93 | * |
||
| 94 | * - enabled: Whether this behavior is active or not. Defaults true. |
||
| 95 | * |
||
| 96 | * - cache: EAV cache feature, see documentation. Defaults false. |
||
| 97 | * |
||
| 98 | * - hydrator: Callable function responsible of hydrate an entity with its |
||
| 99 | * virtual values, callable receives two arguments: the entity to hydrate and |
||
| 100 | * an array of virtual values, where each virtual value is an arrray composed |
||
| 101 | * of `property_name` and `value` keys. |
||
| 102 | * |
||
| 103 | * @var array |
||
| 104 | */ |
||
| 105 | protected $_defaultConfig = [ |
||
| 106 | 'status' => true, |
||
| 107 | 'cache' => false, |
||
| 108 | 'hydrator' => null, |
||
| 109 | 'queryScope' => [ |
||
| 110 | 'Eav\\Model\\Behavior\\QueryScope\\SelectScope', |
||
| 111 | 'Eav\\Model\\Behavior\\QueryScope\\WhereScope', |
||
| 112 | 'Eav\\Model\\Behavior\\QueryScope\\OrderScope', |
||
| 113 | ], |
||
| 114 | 'implementedMethods' => [ |
||
| 115 | 'eav' => 'eav', |
||
| 116 | 'updateEavCache' => 'updateEavCache', |
||
| 117 | 'addColumn' => 'addColumn', |
||
| 118 | 'dropColumn' => 'dropColumn', |
||
| 119 | 'listColumns' => 'listColumns', |
||
| 120 | ], |
||
| 121 | ]; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Query scopes objects to be applied indexed by unique ID. |
||
| 125 | * |
||
| 126 | * @var array |
||
| 127 | */ |
||
| 128 | protected $_queryScopes = []; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Constructor. |
||
| 132 | * |
||
| 133 | * @param \Cake\ORM\Table $table The table this behavior is attached to |
||
| 134 | * @param array $config Configuration array for this behavior |
||
| 135 | */ |
||
| 136 | public function __construct(Table $table, array $config = []) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Gets/sets EAV status. |
||
| 169 | * |
||
| 170 | * - TRUE: Enables EAV behavior so virtual columns WILL be fetched from database. |
||
| 171 | * - FALSE: Disables EAV behavior so virtual columns WLL NOT be fetched from database. |
||
| 172 | * |
||
| 173 | * @param bool|null $status EAV status to set, or null to get current state |
||
| 174 | * @return void|bool Current status if `$status` is set to null |
||
| 175 | */ |
||
| 176 | public function eav($status = null) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Defines a new virtual-column, or update if already defined. |
||
| 187 | * |
||
| 188 | * ### Usage: |
||
| 189 | * |
||
| 190 | * ```php |
||
| 191 | * $errors = $this->Users->addColumn('user-age', [ |
||
| 192 | * 'type' => 'integer', |
||
| 193 | * 'bundle' => 'some-bundle-name', |
||
| 194 | * 'extra' => [ |
||
| 195 | * 'option1' => 'value1' |
||
| 196 | * ] |
||
| 197 | * ], true); |
||
| 198 | * |
||
| 199 | * if (empty($errors)) { |
||
| 200 | * // OK |
||
| 201 | * } else { |
||
| 202 | * // ERROR |
||
| 203 | * debug($errors); |
||
| 204 | * } |
||
| 205 | * ``` |
||
| 206 | * |
||
| 207 | * The third argument can be set to FALSE to get a boolean response: |
||
| 208 | * |
||
| 209 | * ```php |
||
| 210 | * $success = $this->Users->addColumn('user-age', [ |
||
| 211 | * 'type' => 'integer', |
||
| 212 | * 'bundle' => 'some-bundle-name', |
||
| 213 | * 'extra' => [ |
||
| 214 | * 'option1' => 'value1' |
||
| 215 | * ] |
||
| 216 | * ]); |
||
| 217 | * |
||
| 218 | * if ($success) { |
||
| 219 | * // OK |
||
| 220 | * } else { |
||
| 221 | * // ERROR |
||
| 222 | * } |
||
| 223 | * ``` |
||
| 224 | * |
||
| 225 | * @param string $name Column name. e.g. `user-age` |
||
| 226 | * @param array $options Column configuration options |
||
| 227 | * @param bool $errors If set to true will return an array list of errors |
||
| 228 | * instead of boolean response. Defaults to TRUE |
||
| 229 | * @return bool|array True on success or array of error messages, depending on |
||
| 230 | * $error argument |
||
| 231 | * @throws \Cake\Error\FatalErrorException When provided column name collides |
||
| 232 | * with existing column names. And when an invalid type is provided |
||
| 233 | */ |
||
| 234 | public function addColumn($name, array $options = [], $errors = true) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Drops an existing column. |
||
| 285 | * |
||
| 286 | * @param string $name Name of the column to drop |
||
| 287 | * @param string|null $bundle Removes the column within a particular bundle |
||
| 288 | * @return bool True on success, false otherwise |
||
| 289 | */ |
||
| 290 | public function dropColumn($name, $bundle = null) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Gets a list of virtual columns attached to this table. |
||
| 311 | * |
||
| 312 | * @param string|null $bundle Get attributes within given bundle, or all of them |
||
| 313 | * regardless of the bundle if not provided |
||
| 314 | * @return array Columns information indexed by column name |
||
| 315 | */ |
||
| 316 | public function listColumns($bundle = null) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Update EAV cache for the specified $entity. |
||
| 335 | * |
||
| 336 | * @return bool Success |
||
| 337 | */ |
||
| 338 | public function updateEavCache(EntityInterface $entity) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Attaches virtual properties to entities. |
||
| 406 | * |
||
| 407 | * This method is also responsible of looking for virtual columns in SELECT and |
||
| 408 | * WHERE clauses (if applicable) and properly scope the Query object. Query |
||
| 409 | * scoping is performed by the `_scopeQuery()` method. |
||
| 410 | * |
||
| 411 | * @param \Cake\Event\Event $event The beforeFind event that was triggered |
||
| 412 | * @param \Cake\ORM\Query $query The original query to modify |
||
| 413 | * @param \ArrayObject $options Additional options given as an array |
||
| 414 | * @param bool $primary Whether this find is a primary query or not |
||
| 415 | * @return bool|null |
||
| 416 | */ |
||
| 417 | public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Attach EAV attributes for every entity in the provided result-set. |
||
| 443 | * |
||
| 444 | * This method iterates over each retrieved entity and invokes the |
||
| 445 | * `hydrateEntity()` method. This last should return the altered entity object |
||
| 446 | * with all its virtual properties, however if this method returns NULL the |
||
| 447 | * entity will be removed from the resulting collection. |
||
| 448 | * |
||
| 449 | * @param \Cake\Collection\CollectionInterface $entities Set of entities to be |
||
| 450 | * processed |
||
| 451 | * @param array $args Contains three keys: "options" and "primary" given to the |
||
| 452 | * originating beforeFind(), and "selectedVirtual", a list of virtual columns |
||
| 453 | * selected in the originating find query |
||
| 454 | * @return \Cake\Collection\CollectionInterface New set with altered entities |
||
| 455 | */ |
||
| 456 | protected function _hydrateEntities(CollectionInterface $entities, array $args) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Hydrates a single entity and returns it. |
||
| 484 | * |
||
| 485 | * Returning NULL indicates the entity should be removed from the resulting |
||
| 486 | * collection. |
||
| 487 | * |
||
| 488 | * @param \Cake\Datasource\EntityInterface $entity The entity to hydrate |
||
| 489 | * @param array $values Holds stored virtual values for this particular entity |
||
| 490 | * @return bool|null|\Cake\Datasource\EntityInterface |
||
| 491 | */ |
||
| 492 | public function hydrateEntity(EntityInterface $entity, array $values) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Retrieves all virtual values of all the entities within the given result-set. |
||
| 516 | * |
||
| 517 | * @param \Cake\Collection\CollectionInterface $entities Set of entities |
||
| 518 | * @param array $args Contains two keys: "options" and "primary" given to the |
||
| 519 | * originating beforeFind(), and "selectedVirtual" a list of virtual columns |
||
| 520 | * selected in the originating find query |
||
| 521 | * @return array Virtual values indexed by entity ID |
||
| 522 | */ |
||
| 523 | protected function _prepareSetValues(CollectionInterface $entities, array $args) |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Triggered before data is converted into entities. |
||
| 590 | * |
||
| 591 | * Converts incoming POST data to its corresponding types. |
||
| 592 | * |
||
| 593 | * @param \Cake\Event\Event $event The event that was triggered |
||
| 594 | * @param \ArrayObject $data The POST data to be merged with entity |
||
| 595 | * @param \ArrayObject $options The options passed to the marshaller |
||
| 596 | * @return void |
||
| 597 | */ |
||
| 598 | public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options) |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Save virtual values after an entity's real values were saved. |
||
| 614 | * |
||
| 615 | * @param \Cake\Event\Event $event The event that was triggered |
||
| 616 | * @param \Cake\Datasource\EntityInterface $entity The entity that was saved |
||
| 617 | * @param \ArrayObject $options Additional options given as an array |
||
| 618 | * @return bool True always |
||
| 619 | */ |
||
| 620 | public function afterSave(Event $event, EntityInterface $entity, ArrayObject $options) |
||
| 680 | |||
| 681 | /** |
||
| 682 | * After an entity was removed from database. Here is when EAV values are |
||
| 683 | * removed from DB. |
||
| 684 | * |
||
| 685 | * @param \Cake\Event\Event $event The event that was triggered |
||
| 686 | * @param \Cake\Datasource\EntityInterface $entity The entity that was deleted |
||
| 687 | * @param \ArrayObject $options Additional options given as an array |
||
| 688 | * @throws \Cake\Error\FatalErrorException When using this behavior in non-atomic mode |
||
| 689 | * @return void |
||
| 690 | */ |
||
| 691 | public function afterDelete(Event $event, EntityInterface $entity, ArrayObject $options) |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Prepares entity's cache-columns (those defined using `cache` option). |
||
| 712 | * |
||
| 713 | * @param \Cake\Datasource\EntityInterface $entity The entity to prepare |
||
| 714 | * @return \Cake\Datasource\EntityInterfa Modified entity |
||
| 715 | */ |
||
| 716 | protected function _prepareCachedColumns(EntityInterface $entity) |
||
| 733 | |||
| 734 | /** |
||
| 735 | * Look for virtual columns in some query's clauses. |
||
| 736 | * |
||
| 737 | * @param \Cake\ORM\Query $query The query to scope |
||
| 738 | * @param string|null $bundle Consider attributes only for a specific bundle |
||
| 739 | * @return \Cake\ORM\Query The modified query object |
||
| 740 | */ |
||
| 741 | protected function _scopeQuery(Query $query, $bundle = null) |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Initializes the scope objects |
||
| 755 | * |
||
| 756 | * @return void |
||
| 757 | */ |
||
| 758 | protected function _initScopes() |
||
| 773 | } |