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 | * Default configuration. |
||
| 85 | * |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | protected $_defaultConfig = [ |
||
| 89 | 'enabled' => true, |
||
| 90 | 'cache' => false, |
||
| 91 | 'queryScope' => [ |
||
| 92 | 'Eav\\Model\\Behavior\\QueryScope\\SelectScope', |
||
| 93 | 'Eav\\Model\\Behavior\\QueryScope\\WhereScope', |
||
| 94 | 'Eav\\Model\\Behavior\\QueryScope\\OrderScope', |
||
| 95 | ], |
||
| 96 | 'implementedMethods' => [ |
||
| 97 | 'enableEav' => 'enableEav', |
||
| 98 | 'disableEav' => 'disableEav', |
||
| 99 | 'updateEavCache' => 'updateEavCache', |
||
| 100 | 'addColumn' => 'addColumn', |
||
| 101 | 'dropColumn' => 'dropColumn', |
||
| 102 | 'listColumns' => 'listColumns', |
||
| 103 | ], |
||
| 104 | ]; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Query scopes objects to be applied indexed by unique ID. |
||
| 108 | * |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | protected $_queryScopes = []; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Constructor. |
||
| 115 | * |
||
| 116 | * @param \Cake\ORM\Table $table The table this behavior is attached to |
||
| 117 | * @param array $config Configuration array for this behavior |
||
| 118 | */ |
||
| 119 | public function __construct(Table $table, array $config = []) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Enables EAV behavior so virtual columns WILL be fetched from database. |
||
| 147 | * |
||
| 148 | * @return void |
||
| 149 | */ |
||
| 150 | public function enableEav() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Disables EAV behavior so virtual columns WLL NOT be fetched from database. |
||
| 157 | * |
||
| 158 | * @return void |
||
| 159 | */ |
||
| 160 | public function disableEav() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Defines a new virtual-column, or update if already defined. |
||
| 167 | * |
||
| 168 | * ### Usage: |
||
| 169 | * |
||
| 170 | * ```php |
||
| 171 | * $errors = $this->Users->addColumn('user-age', [ |
||
| 172 | * 'type' => 'integer', |
||
| 173 | * 'bundle' => 'some-bundle-name', |
||
| 174 | * 'extra' => [ |
||
| 175 | * 'option1' => 'value1' |
||
| 176 | * ] |
||
| 177 | * ], true); |
||
| 178 | * |
||
| 179 | * if (empty($errors)) { |
||
| 180 | * // OK |
||
| 181 | * } else { |
||
| 182 | * // ERROR |
||
| 183 | * debug($errors); |
||
| 184 | * } |
||
| 185 | * ``` |
||
| 186 | * |
||
| 187 | * The third argument can be set to FALSE to get a boolean response: |
||
| 188 | * |
||
| 189 | * ```php |
||
| 190 | * $success = $this->Users->addColumn('user-age', [ |
||
| 191 | * 'type' => 'integer', |
||
| 192 | * 'bundle' => 'some-bundle-name', |
||
| 193 | * 'extra' => [ |
||
| 194 | * 'option1' => 'value1' |
||
| 195 | * ] |
||
| 196 | * ]); |
||
| 197 | * |
||
| 198 | * if ($success) { |
||
| 199 | * // OK |
||
| 200 | * } else { |
||
| 201 | * // ERROR |
||
| 202 | * } |
||
| 203 | * ``` |
||
| 204 | * |
||
| 205 | * @param string $name Column name. e.g. `user-age` |
||
| 206 | * @param array $options Column configuration options |
||
| 207 | * @param bool $errors If set to true will return an array list of errors |
||
| 208 | * instead of boolean response. Defaults to TRUE |
||
| 209 | * @return bool|array True on success or array of error messages, depending on |
||
| 210 | * $error argument |
||
| 211 | * @throws \Cake\Error\FatalErrorException When provided column name collides |
||
| 212 | * with existing column names. And when an invalid type is provided |
||
| 213 | */ |
||
| 214 | public function addColumn($name, array $options = [], $errors = true) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Drops an existing column. |
||
| 260 | * |
||
| 261 | * @param string $name Name of the column to drop |
||
| 262 | * @param string|null $bundle Removes the column within a particular bundle |
||
| 263 | * @return bool True on success, false otherwise |
||
| 264 | */ |
||
| 265 | public function dropColumn($name, $bundle = null) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Gets a list of virtual columns attached to this table. |
||
| 286 | * |
||
| 287 | * @param string|null $bundle Get attributes within given bundle, or all of them |
||
| 288 | * regardless of the bundle if not provided |
||
| 289 | * @return array Columns information indexed by column name |
||
| 290 | */ |
||
| 291 | public function listColumns($bundle = null) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Update EAV cache for the specified $entity. |
||
| 310 | * |
||
| 311 | * @return bool Success |
||
| 312 | */ |
||
| 313 | public function updateEavCache(EntityInterface $entity) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Attaches virtual properties to entities. |
||
| 380 | * |
||
| 381 | * This method iterates over each retrieved entity and invokes the |
||
| 382 | * `attachEntityAttributes()` method. This method should return the altered |
||
| 383 | * entity object with its virtual properties, however if this method returns |
||
| 384 | * NULL the entity will be removed from the resulting collection. And if this |
||
| 385 | * method returns FALSE will stop the find() operation. |
||
| 386 | * |
||
| 387 | * This method is also responsible of looking for virtual columns in SELECT and |
||
| 388 | * WHERE clauses (if applicable) and properly scope the Query object. Query |
||
| 389 | * scoping is performed by the `_scopeQuery()` method. |
||
| 390 | * |
||
| 391 | * @param \Cake\Event\Event $event The beforeFind event that was triggered |
||
| 392 | * @param \Cake\ORM\Query $query The original query to modify |
||
| 393 | * @param \ArrayObject $options Additional options given as an array |
||
| 394 | * @param bool $primary Whether this find is a primary query or not |
||
| 395 | * @return bool|null |
||
| 396 | */ |
||
| 397 | public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Triggered before data is converted into entities. |
||
| 418 | * |
||
| 419 | * Converts incoming POST data to its corresponding types. |
||
| 420 | * |
||
| 421 | * @param \Cake\Event\Event $event The event that was triggered |
||
| 422 | * @param \ArrayObject $data The POST data to be merged with entity |
||
| 423 | * @param \ArrayObject $options The options passed to the marshaller |
||
| 424 | * @return void |
||
| 425 | */ |
||
| 426 | public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * After an entity is saved. |
||
| 442 | * |
||
| 443 | * @param \Cake\Event\Event $event The event that was triggered |
||
| 444 | * @param \Cake\Datasource\EntityInterface $entity The entity that was saved |
||
| 445 | * @param \ArrayObject $options Additional options given as an array |
||
| 446 | * @return bool True always |
||
| 447 | */ |
||
| 448 | public function afterSave(Event $event, EntityInterface $entity, ArrayObject $options) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * After an entity was removed from database. Here is when EAV values are |
||
| 511 | * removed from DB. |
||
| 512 | * |
||
| 513 | * @param \Cake\Event\Event $event The event that was triggered |
||
| 514 | * @param \Cake\Datasource\EntityInterface $entity The entity that was deleted |
||
| 515 | * @param \ArrayObject $options Additional options given as an array |
||
| 516 | * @throws \Cake\Error\FatalErrorException When using this behavior in non-atomic mode |
||
| 517 | * @return void |
||
| 518 | */ |
||
| 519 | public function afterDelete(Event $event, EntityInterface $entity, ArrayObject $options) |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Attach EAV attributes for every entity in the provided result-set. |
||
| 541 | * |
||
| 542 | * @param \Cake\Collection\CollectionInterface $entities Set of entities to be |
||
| 543 | * processed |
||
| 544 | * @param array $options Arguments given to `beforeFind()` method, possible keys |
||
| 545 | * are "event", "query", "options", "primary" |
||
| 546 | * @return \Cake\Collection\CollectionInterface New set with altered entities |
||
| 547 | */ |
||
| 548 | public function hydrateEntities(CollectionInterface $entities, array $options) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Retrieves all virtual values of all the entities within the given result-set. |
||
| 589 | * |
||
| 590 | * @param \Cake\Collection\CollectionInterface $entities Set of entities |
||
| 591 | * @param array $options Arguments given to `beforeFind()` method, possible keys |
||
| 592 | * are "event", "query", "options", "primary" |
||
| 593 | * @return array Virtual values indexed by entity ID |
||
| 594 | */ |
||
| 595 | protected function _prepareSetValues(CollectionInterface $entities, array $options) |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Prepares entity's cache-columns (those defined using `cache` option). |
||
| 648 | * |
||
| 649 | * @param \Cake\Datasource\EntityInterface $entity The entity to prepare |
||
| 650 | * @return \Cake\Datasource\EntityInterfa Modified entity |
||
| 651 | */ |
||
| 652 | protected function _prepareCachedColumns(EntityInterface $entity) |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Look for virtual columns in some query's clauses. |
||
| 672 | * |
||
| 673 | * @param \Cake\ORM\Query $query The query to scope |
||
| 674 | * @param string|null $bundle Consider attributes only for a specific bundle |
||
| 675 | * @return \Cake\ORM\Query The modified query object |
||
| 676 | */ |
||
| 677 | protected function _scopeQuery(Query $query, $bundle = null) |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Initializes the scope objects |
||
| 691 | * |
||
| 692 | * @return void |
||
| 693 | */ |
||
| 694 | protected function _initScopes() |
||
| 709 | } |
||
| 710 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.