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 |
||
| 73 | class EavBehavior extends Behavior |
||
| 74 | { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Instance of EavToolbox. |
||
| 78 | * |
||
| 79 | * @var \Eav\Model\Behavior\EavToolbox |
||
| 80 | */ |
||
| 81 | protected $_toolbox = null; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Represents an entity that should be removed from the collection. |
||
| 85 | * |
||
| 86 | * @var int |
||
| 87 | */ |
||
| 88 | const NULL_ENTITY = -1; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Default configuration. |
||
| 92 | * |
||
| 93 | * - enabled: Whether this behavior is active or not. Defaults true. |
||
| 94 | * |
||
| 95 | * - cache: EAV cache feature, see documentation. Defaults false. |
||
| 96 | * |
||
| 97 | * - hydrator: Callable function responsible of hydrate an entity with its |
||
| 98 | * virtual values, callable receives two arguments: the entity to hydrate and |
||
| 99 | * an array of virtual values, where each virtual value is an arrray composed |
||
| 100 | * of `property_name` and `value` keys. |
||
| 101 | * |
||
| 102 | * @var array |
||
| 103 | */ |
||
| 104 | protected $_defaultConfig = [ |
||
| 105 | 'status' => true, |
||
| 106 | 'cache' => false, |
||
| 107 | 'hydrator' => null, |
||
| 108 | 'queryScope' => [ |
||
| 109 | 'Eav\\Model\\Behavior\\QueryScope\\SelectScope', |
||
| 110 | 'Eav\\Model\\Behavior\\QueryScope\\WhereScope', |
||
| 111 | 'Eav\\Model\\Behavior\\QueryScope\\OrderScope', |
||
| 112 | ], |
||
| 113 | 'implementedMethods' => [ |
||
| 114 | 'eav' => 'eav', |
||
| 115 | 'updateEavCache' => 'updateEavCache', |
||
| 116 | 'addColumn' => 'addColumn', |
||
| 117 | 'dropColumn' => 'dropColumn', |
||
| 118 | 'listColumns' => 'listColumns', |
||
| 119 | ], |
||
| 120 | ]; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Query scopes objects to be applied indexed by unique ID. |
||
| 124 | * |
||
| 125 | * @var array |
||
| 126 | */ |
||
| 127 | protected $_queryScopes = []; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Constructor. |
||
| 131 | * |
||
| 132 | * @param \Cake\ORM\Table $table The table this behavior is attached to |
||
| 133 | * @param array $config Configuration array for this behavior |
||
| 134 | */ |
||
| 135 | public function __construct(Table $table, array $config = []) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Gets/sets EAV status. |
||
| 168 | * |
||
| 169 | * - TRUE: Enables EAV behavior so virtual columns WILL be fetched from database. |
||
| 170 | * - FALSE: Disables EAV behavior so virtual columns WLL NOT be fetched from database. |
||
| 171 | * |
||
| 172 | * @param bool|null $status EAV status to set, or null to get current state |
||
| 173 | * @return void|bool Current status if `$status` is set to null |
||
| 174 | */ |
||
| 175 | public function eav($status = null) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Defines a new virtual-column, or update if already defined. |
||
| 186 | * |
||
| 187 | * ### Usage: |
||
| 188 | * |
||
| 189 | * ```php |
||
| 190 | * $errors = $this->Users->addColumn('user-age', [ |
||
| 191 | * 'type' => 'integer', |
||
| 192 | * 'bundle' => 'some-bundle-name', |
||
| 193 | * 'extra' => [ |
||
| 194 | * 'option1' => 'value1' |
||
| 195 | * ] |
||
| 196 | * ], true); |
||
| 197 | * |
||
| 198 | * if (empty($errors)) { |
||
| 199 | * // OK |
||
| 200 | * } else { |
||
| 201 | * // ERROR |
||
| 202 | * debug($errors); |
||
| 203 | * } |
||
| 204 | * ``` |
||
| 205 | * |
||
| 206 | * The third argument can be set to FALSE to get a boolean response: |
||
| 207 | * |
||
| 208 | * ```php |
||
| 209 | * $success = $this->Users->addColumn('user-age', [ |
||
| 210 | * 'type' => 'integer', |
||
| 211 | * 'bundle' => 'some-bundle-name', |
||
| 212 | * 'extra' => [ |
||
| 213 | * 'option1' => 'value1' |
||
| 214 | * ] |
||
| 215 | * ]); |
||
| 216 | * |
||
| 217 | * if ($success) { |
||
| 218 | * // OK |
||
| 219 | * } else { |
||
| 220 | * // ERROR |
||
| 221 | * } |
||
| 222 | * ``` |
||
| 223 | * |
||
| 224 | * @param string $name Column name. e.g. `user-age` |
||
| 225 | * @param array $options Column configuration options |
||
| 226 | * @param bool $errors If set to true will return an array list of errors |
||
| 227 | * instead of boolean response. Defaults to TRUE |
||
| 228 | * @return bool|array True on success or array of error messages, depending on |
||
| 229 | * $error argument |
||
| 230 | * @throws \Cake\Error\FatalErrorException When provided column name collides |
||
| 231 | * with existing column names. And when an invalid type is provided |
||
| 232 | */ |
||
| 233 | public function addColumn($name, array $options = [], $errors = true) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Drops an existing column. |
||
| 284 | * |
||
| 285 | * @param string $name Name of the column to drop |
||
| 286 | * @param string|null $bundle Removes the column within a particular bundle |
||
| 287 | * @return bool True on success, false otherwise |
||
| 288 | */ |
||
| 289 | public function dropColumn($name, $bundle = null) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Gets a list of virtual columns attached to this table. |
||
| 310 | * |
||
| 311 | * @param string|null $bundle Get attributes within given bundle, or all of them |
||
| 312 | * regardless of the bundle if not provided |
||
| 313 | * @return array Columns information indexed by column name |
||
| 314 | */ |
||
| 315 | public function listColumns($bundle = null) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Update EAV cache for the specified $entity. |
||
| 334 | * |
||
| 335 | * @return bool Success |
||
| 336 | */ |
||
| 337 | public function updateEavCache(EntityInterface $entity) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Attaches virtual properties to entities. |
||
| 405 | * |
||
| 406 | * This method is also responsible of looking for virtual columns in SELECT and |
||
| 407 | * WHERE clauses (if applicable) and properly scope the Query object. Query |
||
| 408 | * scoping is performed by the `_scopeQuery()` method. |
||
| 409 | * |
||
| 410 | * @param \Cake\Event\Event $event The beforeFind event that was triggered |
||
| 411 | * @param \Cake\ORM\Query $query The original query to modify |
||
| 412 | * @param \ArrayObject $options Additional options given as an array |
||
| 413 | * @param bool $primary Whether this find is a primary query or not |
||
| 414 | * @return bool|null |
||
| 415 | */ |
||
| 416 | public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Attach EAV attributes for every entity in the provided result-set. |
||
| 442 | * |
||
| 443 | * This method iterates over each retrieved entity and invokes the |
||
| 444 | * `hydrateEntity()` method. This last should return the altered entity object |
||
| 445 | * with all its virtual properties, however if this method returns NULL the |
||
| 446 | * entity will be removed from the resulting collection. |
||
| 447 | * |
||
| 448 | * @param \Cake\Collection\CollectionInterface $entities Set of entities to be |
||
| 449 | * processed |
||
| 450 | * @param array $args Contains three keys: "options" and "primary" given to the |
||
| 451 | * originating beforeFind(), and "selectedVirtual", a list of virtual columns |
||
| 452 | * selected in the originating find query |
||
| 453 | * @return \Cake\Collection\CollectionInterface New set with altered entities |
||
| 454 | */ |
||
| 455 | protected function _hydrateEntities(CollectionInterface $entities, array $args) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Hydrates a single entity and returns it. |
||
| 483 | * |
||
| 484 | * Returning NULL indicates the entity should be removed from the resulting |
||
| 485 | * collection. |
||
| 486 | * |
||
| 487 | * @param \Cake\Datasource\EntityInterface $entity The entity to hydrate |
||
| 488 | * @param array $values Holds stored virtual values for this particular entity |
||
| 489 | * @return bool|null|\Cake\Datasource\EntityInterface |
||
| 490 | */ |
||
| 491 | public function hydrateEntity(EntityInterface $entity, array $values) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Retrieves all virtual values of all the entities within the given result-set. |
||
| 523 | * |
||
| 524 | * @param \Cake\Collection\CollectionInterface $entities Set of entities |
||
| 525 | * @param array $args Contains two keys: "options" and "primary" given to the |
||
| 526 | * originating beforeFind(), and "selectedVirtual" a list of virtual columns |
||
| 527 | * selected in the originating find query |
||
| 528 | * @return array Virtual values indexed by entity ID |
||
| 529 | */ |
||
| 530 | protected function _prepareSetValues(CollectionInterface $entities, array $args) |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Triggered before data is converted into entities. |
||
| 578 | * |
||
| 579 | * Converts incoming POST data to its corresponding types. |
||
| 580 | * |
||
| 581 | * @param \Cake\Event\Event $event The event that was triggered |
||
| 582 | * @param \ArrayObject $data The POST data to be merged with entity |
||
| 583 | * @param \ArrayObject $options The options passed to the marshaller |
||
| 584 | * @return void |
||
| 585 | */ |
||
| 586 | public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options) |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Save virtual values after an entity's real values were saved. |
||
| 602 | * |
||
| 603 | * @param \Cake\Event\Event $event The event that was triggered |
||
| 604 | * @param \Cake\Datasource\EntityInterface $entity The entity that was saved |
||
| 605 | * @param \ArrayObject $options Additional options given as an array |
||
| 606 | * @return bool True always |
||
| 607 | */ |
||
| 608 | public function afterSave(Event $event, EntityInterface $entity, ArrayObject $options) |
||
| 668 | |||
| 669 | /** |
||
| 670 | * After an entity was removed from database. Here is when EAV values are |
||
| 671 | * removed from DB. |
||
| 672 | * |
||
| 673 | * @param \Cake\Event\Event $event The event that was triggered |
||
| 674 | * @param \Cake\Datasource\EntityInterface $entity The entity that was deleted |
||
| 675 | * @param \ArrayObject $options Additional options given as an array |
||
| 676 | * @throws \Cake\Error\FatalErrorException When using this behavior in non-atomic mode |
||
| 677 | * @return void |
||
| 678 | */ |
||
| 679 | public function afterDelete(Event $event, EntityInterface $entity, ArrayObject $options) |
||
| 697 | |||
| 698 | /** |
||
| 699 | * Prepares entity's cache-columns (those defined using `cache` option). |
||
| 700 | * |
||
| 701 | * @param \Cake\Datasource\EntityInterface $entity The entity to prepare |
||
| 702 | * @return \Cake\Datasource\EntityInterfa Modified entity |
||
| 703 | */ |
||
| 704 | protected function _prepareCachedColumns(EntityInterface $entity) |
||
| 721 | |||
| 722 | /** |
||
| 723 | * Look for virtual columns in some query's clauses. |
||
| 724 | * |
||
| 725 | * @param \Cake\ORM\Query $query The query to scope |
||
| 726 | * @param string|null $bundle Consider attributes only for a specific bundle |
||
| 727 | * @return \Cake\ORM\Query The modified query object |
||
| 728 | */ |
||
| 729 | protected function _scopeQuery(Query $query, $bundle = null) |
||
| 740 | |||
| 741 | /** |
||
| 742 | * Initializes the scope objects |
||
| 743 | * |
||
| 744 | * @return void |
||
| 745 | */ |
||
| 746 | protected function _initScopes() |
||
| 761 | } |
||
| 762 |