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 = 2; |
||
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 | 'enabled' => 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 | 'enableEav' => 'enableEav', |
||
115 | 'disableEav' => 'disableEav', |
||
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 = []) |
||
165 | |||
166 | /** |
||
167 | * Enables EAV behavior so virtual columns WILL be fetched from database. |
||
168 | * |
||
169 | * @return void |
||
170 | */ |
||
171 | public function enableEav() |
||
175 | |||
176 | /** |
||
177 | * Disables EAV behavior so virtual columns WLL NOT be fetched from database. |
||
178 | * |
||
179 | * @return void |
||
180 | */ |
||
181 | public function disableEav() |
||
185 | |||
186 | /** |
||
187 | * Defines a new virtual-column, or update if already defined. |
||
188 | * |
||
189 | * ### Usage: |
||
190 | * |
||
191 | * ```php |
||
192 | * $errors = $this->Users->addColumn('user-age', [ |
||
193 | * 'type' => 'integer', |
||
194 | * 'bundle' => 'some-bundle-name', |
||
195 | * 'extra' => [ |
||
196 | * 'option1' => 'value1' |
||
197 | * ] |
||
198 | * ], true); |
||
199 | * |
||
200 | * if (empty($errors)) { |
||
201 | * // OK |
||
202 | * } else { |
||
203 | * // ERROR |
||
204 | * debug($errors); |
||
205 | * } |
||
206 | * ``` |
||
207 | * |
||
208 | * The third argument can be set to FALSE to get a boolean response: |
||
209 | * |
||
210 | * ```php |
||
211 | * $success = $this->Users->addColumn('user-age', [ |
||
212 | * 'type' => 'integer', |
||
213 | * 'bundle' => 'some-bundle-name', |
||
214 | * 'extra' => [ |
||
215 | * 'option1' => 'value1' |
||
216 | * ] |
||
217 | * ]); |
||
218 | * |
||
219 | * if ($success) { |
||
220 | * // OK |
||
221 | * } else { |
||
222 | * // ERROR |
||
223 | * } |
||
224 | * ``` |
||
225 | * |
||
226 | * @param string $name Column name. e.g. `user-age` |
||
227 | * @param array $options Column configuration options |
||
228 | * @param bool $errors If set to true will return an array list of errors |
||
229 | * instead of boolean response. Defaults to TRUE |
||
230 | * @return bool|array True on success or array of error messages, depending on |
||
231 | * $error argument |
||
232 | * @throws \Cake\Error\FatalErrorException When provided column name collides |
||
233 | * with existing column names. And when an invalid type is provided |
||
234 | */ |
||
235 | public function addColumn($name, array $options = [], $errors = true) |
||
278 | |||
279 | /** |
||
280 | * Drops an existing column. |
||
281 | * |
||
282 | * @param string $name Name of the column to drop |
||
283 | * @param string|null $bundle Removes the column within a particular bundle |
||
284 | * @return bool True on success, false otherwise |
||
285 | */ |
||
286 | public function dropColumn($name, $bundle = null) |
||
304 | |||
305 | /** |
||
306 | * Gets a list of virtual columns attached to this table. |
||
307 | * |
||
308 | * @param string|null $bundle Get attributes within given bundle, or all of them |
||
309 | * regardless of the bundle if not provided |
||
310 | * @return array Columns information indexed by column name |
||
311 | */ |
||
312 | public function listColumns($bundle = null) |
||
328 | |||
329 | /** |
||
330 | * Update EAV cache for the specified $entity. |
||
331 | * |
||
332 | * @return bool Success |
||
333 | */ |
||
334 | public function updateEavCache(EntityInterface $entity) |
||
398 | |||
399 | /** |
||
400 | * Attaches virtual properties to entities. |
||
401 | * |
||
402 | * This method is also responsible of looking for virtual columns in SELECT and |
||
403 | * WHERE clauses (if applicable) and properly scope the Query object. Query |
||
404 | * scoping is performed by the `_scopeQuery()` method. |
||
405 | * |
||
406 | * @param \Cake\Event\Event $event The beforeFind event that was triggered |
||
407 | * @param \Cake\ORM\Query $query The original query to modify |
||
408 | * @param \ArrayObject $options Additional options given as an array |
||
409 | * @param bool $primary Whether this find is a primary query or not |
||
410 | * @return bool|null |
||
411 | */ |
||
412 | public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary) |
||
434 | |||
435 | |||
436 | /** |
||
437 | * Attach EAV attributes for every entity in the provided result-set. |
||
438 | * |
||
439 | * This method iterates over each retrieved entity and invokes the |
||
440 | * `hydrateEntity()` method. This last should return the altered entity object |
||
441 | * with all its virtual properties, however if this method returns NULL the |
||
442 | * entity will be removed from the resulting collection. |
||
443 | * |
||
444 | * @param \Cake\Collection\CollectionInterface $entities Set of entities to be |
||
445 | * processed |
||
446 | * @param array $args Contains two keys: "options" and "primary" given to the |
||
447 | * originating beforeFind(), and "selectedVirtual" a list of virtual columns |
||
448 | * selected in the originating find query |
||
449 | * @return \Cake\Collection\CollectionInterface New set with altered entities |
||
450 | */ |
||
451 | protected function _hydrateEntities(CollectionInterface $entities, array $args) |
||
477 | |||
478 | /** |
||
479 | * Hydrates a single entity and returns it. |
||
480 | * |
||
481 | * Returning NULL indicates the entity should be removed from the resulting |
||
482 | * collection. |
||
483 | * |
||
484 | * @param \Cake\Datasource\EntityInterface $entity The entity to hydrate |
||
485 | * @param array $values Holds stored virtual values for this particular entity |
||
486 | * @return bool|null|\Cake\Datasource\EntityInterface |
||
487 | */ |
||
488 | public function hydrateEntity(EntityInterface $entity, array $values) |
||
509 | |||
510 | /** |
||
511 | * Retrieves all virtual values of all the entities within the given result-set. |
||
512 | * |
||
513 | * @param \Cake\Collection\CollectionInterface $entities Set of entities |
||
514 | * @param array $args Contains two keys: "options" and "primary" given to the |
||
515 | * originating beforeFind(), and "selectedVirtual" a list of virtual columns |
||
516 | * selected in the originating find query |
||
517 | * @return array Virtual values indexed by entity ID |
||
518 | */ |
||
519 | protected function _prepareSetValues(CollectionInterface $entities, array $args) |
||
564 | |||
565 | /** |
||
566 | * Triggered before data is converted into entities. |
||
567 | * |
||
568 | * Converts incoming POST data to its corresponding types. |
||
569 | * |
||
570 | * @param \Cake\Event\Event $event The event that was triggered |
||
571 | * @param \ArrayObject $data The POST data to be merged with entity |
||
572 | * @param \ArrayObject $options The options passed to the marshaller |
||
573 | * @return void |
||
574 | */ |
||
575 | public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options) |
||
588 | |||
589 | /** |
||
590 | * Save virtual values after an entity's real values were saved. |
||
591 | * |
||
592 | * @param \Cake\Event\Event $event The event that was triggered |
||
593 | * @param \Cake\Datasource\EntityInterface $entity The entity that was saved |
||
594 | * @param \ArrayObject $options Additional options given as an array |
||
595 | * @return bool True always |
||
596 | */ |
||
597 | public function afterSave(Event $event, EntityInterface $entity, ArrayObject $options) |
||
659 | |||
660 | /** |
||
661 | * After an entity was removed from database. Here is when EAV values are |
||
662 | * removed from DB. |
||
663 | * |
||
664 | * @param \Cake\Event\Event $event The event that was triggered |
||
665 | * @param \Cake\Datasource\EntityInterface $entity The entity that was deleted |
||
666 | * @param \ArrayObject $options Additional options given as an array |
||
667 | * @throws \Cake\Error\FatalErrorException When using this behavior in non-atomic mode |
||
668 | * @return void |
||
669 | */ |
||
670 | public function afterDelete(Event $event, EntityInterface $entity, ArrayObject $options) |
||
689 | |||
690 | /** |
||
691 | * Prepares entity's cache-columns (those defined using `cache` option). |
||
692 | * |
||
693 | * @param \Cake\Datasource\EntityInterface $entity The entity to prepare |
||
694 | * @return \Cake\Datasource\EntityInterfa Modified entity |
||
695 | */ |
||
696 | protected function _prepareCachedColumns(EntityInterface $entity) |
||
713 | |||
714 | /** |
||
715 | * Look for virtual columns in some query's clauses. |
||
716 | * |
||
717 | * @param \Cake\ORM\Query $query The query to scope |
||
718 | * @param string|null $bundle Consider attributes only for a specific bundle |
||
719 | * @return \Cake\ORM\Query The modified query object |
||
720 | */ |
||
721 | protected function _scopeQuery(Query $query, $bundle = null) |
||
732 | |||
733 | /** |
||
734 | * Initializes the scope objects |
||
735 | * |
||
736 | * @return void |
||
737 | */ |
||
738 | protected function _initScopes() |
||
753 | } |
||
754 |
It seems like you are assigning to a variable which was imported through a
use
statement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope