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 FieldableBehavior 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 FieldableBehavior, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
45 | class FieldableBehavior extends EavBehavior |
||
46 | { |
||
47 | |||
48 | /** |
||
49 | * Used for reduce BD queries and allow inter-method communication. |
||
50 | * Example, it allows to pass some information from beforeDelete() to |
||
51 | * afterDelete(). |
||
52 | * |
||
53 | * @var array |
||
54 | */ |
||
55 | protected $_cache = []; |
||
56 | |||
57 | /** |
||
58 | * Default configuration. |
||
59 | * |
||
60 | * These are merged with user-provided configuration when the behavior is used. |
||
61 | * Available options are: |
||
62 | * |
||
63 | * - `bundle`: Bundle within this the table. Can be a string or a callable |
||
64 | * method that must return a string to use as bundle. Default null. If set to |
||
65 | * a callable function, it will receive the entity being saved as first |
||
66 | * argument, so you can calculate a bundle name for each particular entity. |
||
67 | * |
||
68 | * - `enabled`: True enables this behavior or false for disable. Default to |
||
69 | * true. |
||
70 | * |
||
71 | * - `cache`: Column-based cache. See EAV plugin's documentation. |
||
72 | * |
||
73 | * Bundles are usually set to dynamic values. For example, for the "contents" |
||
74 | * table we have "content" entities, but we may have "article contents", "page |
||
75 | * contents", etc. depending on the "type of content" they are; is said that |
||
76 | * "article" and "page" **are bundles** of "contents" table. |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $_fieldableDefaultConfig = [ |
||
81 | 'bundle' => null, |
||
82 | 'implementedMethods' => [ |
||
83 | 'configureFieldable' => 'configureFieldable', |
||
84 | 'attachFields' => 'attachEntityFields', |
||
85 | 'unbindFieldable' => 'unbindFieldable', |
||
86 | 'bindFieldable' => 'bindFieldable', |
||
87 | ], |
||
88 | ]; |
||
89 | |||
90 | /** |
||
91 | * Instance of EavAttributes table. |
||
92 | * |
||
93 | * @var \Eav\Model\Table\EavAttributesTable |
||
94 | */ |
||
95 | public $Attributes = null; |
||
96 | |||
97 | /** |
||
98 | * Constructor. |
||
99 | * |
||
100 | * @param \Cake\ORM\Table $table The table this behavior is attached to |
||
101 | * @param array $config Configuration array for this behavior |
||
102 | */ |
||
103 | public function __construct(Table $table, array $config = []) |
||
114 | |||
115 | /** |
||
116 | * Returns a list of events this class is implementing. When the class is |
||
117 | * registered in an event manager, each individual method will be associated |
||
118 | * with the respective event. |
||
119 | * |
||
120 | * @return void |
||
121 | */ |
||
122 | public function implementedEvents() |
||
134 | |||
135 | /** |
||
136 | * Modifies the query object in order to merge custom fields records |
||
137 | * into each entity under the `_fields` property. |
||
138 | * |
||
139 | * ### Events Triggered: |
||
140 | * |
||
141 | * - `Field.<FieldHandler>.Entity.beforeFind`: This event is triggered for each |
||
142 | * entity in the resulting collection and for each field attached to these |
||
143 | * entities. It receives three arguments, a field entity representing the |
||
144 | * field being processed, an options array and boolean value indicating |
||
145 | * whether the query that initialized the event is part of a primary find |
||
146 | * operation or not. Returning false will cause the entity to be removed from |
||
147 | * the resulting collection, also will stop event propagation, so other |
||
148 | * fields won't be able to listen this event. If the event is stopped using |
||
149 | * the event API, will halt the entire find operation. |
||
150 | * |
||
151 | * You can enable or disable this behavior for a single `find()` or `get()` |
||
152 | * operation by setting `fieldable` or `eav` to false in the options array for |
||
153 | * find method. e.g.: |
||
154 | * |
||
155 | * ```php |
||
156 | * $contents = $this->Contents->find('all', ['fieldable' => false]); |
||
157 | * $content = $this->Contents->get($id, ['fieldable' => false]); |
||
158 | * ``` |
||
159 | * |
||
160 | * It also looks for custom fields in WHERE clause. This will search entities in |
||
161 | * all bundles this table may have, if you need to restrict the search to an |
||
162 | * specific bundle you must use the `bundle` key in find()'s options: |
||
163 | * |
||
164 | * ```php |
||
165 | * $this->Contents |
||
166 | * ->find('all', ['bundle' => 'articles']) |
||
167 | * ->where(['article-title' => 'My first article!']); |
||
168 | * ``` |
||
169 | * |
||
170 | * The `bundle` option has no effects if no custom fields are given in the |
||
171 | * WHERE clause. |
||
172 | * |
||
173 | * @param \Cake\Event\Event $event The beforeFind event that was triggered |
||
174 | * @param \Cake\ORM\Query $query The original query to modify |
||
175 | * @param \ArrayObject $options Additional options given as an array |
||
176 | * @param bool $primary Whether this find is a primary query or not |
||
177 | * @return void |
||
178 | */ |
||
179 | public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary) |
||
189 | |||
190 | /** |
||
191 | * {@inheritDoc} |
||
192 | * |
||
193 | * Attaches entity's field under the `_fields` property, this method is invoked |
||
194 | * by `beforeFind()` when iterating results sets. |
||
195 | * |
||
196 | * When `bundle` option is used the Entity will be removed from the collection |
||
197 | * if it does not belongs to that bundle. |
||
198 | */ |
||
199 | public function attachEntityAttributes(EntityInterface $entity, array $options = []) |
||
218 | |||
219 | /** |
||
220 | * Before an entity is saved. |
||
221 | * |
||
222 | * Here is where we dispatch each custom field's `$_POST` information to its |
||
223 | * corresponding Field Handler, so they can operate over their values. |
||
224 | * |
||
225 | * Fields Handler's `beforeSave()` method is automatically invoked for each |
||
226 | * attached field for the entity being processed, your field handler should look |
||
227 | * as follow: |
||
228 | * |
||
229 | * ```php |
||
230 | * use Field\Handler; |
||
231 | * |
||
232 | * class TextField extends Handler |
||
233 | * { |
||
234 | * public function beforeSave(Field $field, $post) |
||
235 | * { |
||
236 | * // alter $field, and do nifty things with $post |
||
237 | * // return FALSE; will halt the operation |
||
238 | * } |
||
239 | * } |
||
240 | * ``` |
||
241 | * |
||
242 | * Field Handlers should **alter** `$field->value` and `$field->extra` according |
||
243 | * to its needs using the provided **$post** argument. |
||
244 | * |
||
245 | * **NOTE:** Returning boolean FALSE will halt the whole Entity's save operation. |
||
246 | * |
||
247 | * @param \Cake\Event\Event $event The event that was triggered |
||
248 | * @param \Cake\Datasource\EntityInterface $entity The entity being saved |
||
249 | * @param \ArrayObject $options Additional options given as an array |
||
250 | * @throws \Cake\Error\FatalErrorException When using this behavior in non-atomic mode |
||
251 | * @return bool True if save operation should continue |
||
252 | */ |
||
253 | public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options) |
||
310 | |||
311 | /** |
||
312 | * After an entity is saved. |
||
313 | * |
||
314 | * ### Events Triggered: |
||
315 | * |
||
316 | * - `Field.<FieldHandler>.Entity.afterSave`: Will be triggered after a |
||
317 | * successful insert or save, listeners will receive two arguments, the field |
||
318 | * entity and the options array. The type of operation performed (insert or |
||
319 | * update) can be infer by checking the field entity's method `isNew`, true |
||
320 | * meaning an insert and false an update. |
||
321 | * |
||
322 | * @param \Cake\Event\Event $event The event that was triggered |
||
323 | * @param \Cake\Datasource\EntityInterface $entity The entity that was saved |
||
324 | * @param \ArrayObject $options Additional options given as an array |
||
325 | * @return bool True always |
||
326 | */ |
||
327 | public function afterSave(Event $event, EntityInterface $entity, ArrayObject $options) |
||
355 | |||
356 | /** |
||
357 | * Deletes an entity from a fieldable table. |
||
358 | * |
||
359 | * ### Events Triggered: |
||
360 | * |
||
361 | * - `Field.<FieldHandler>.Entity.beforeDelete`: Fired before the delete occurs. |
||
362 | * If stopped the delete will be aborted. Receives as arguments the field |
||
363 | * entity and options array. |
||
364 | * |
||
365 | * @param \Cake\Event\Event $event The event that was triggered |
||
366 | * @param \Cake\Datasource\EntityInterface $entity The entity being deleted |
||
367 | * @param \ArrayObject $options Additional options given as an array |
||
368 | * @return bool |
||
369 | * @throws \Cake\Error\FatalErrorException When using this behavior in non-atomic mode |
||
370 | */ |
||
371 | public function beforeDelete(Event $event, EntityInterface $entity, ArrayObject $options) |
||
397 | |||
398 | /** |
||
399 | * After an entity was removed from database. |
||
400 | * |
||
401 | * ### Events Triggered: |
||
402 | * |
||
403 | * - `Field.<FieldHandler>.Entity.afterDelete`: Fired after the delete has been |
||
404 | * successful. Receives as arguments the field entity and options array. |
||
405 | * |
||
406 | * **NOTE:** This method automatically removes all field values from |
||
407 | * `eav_values` database table for each entity. |
||
408 | * |
||
409 | * @param \Cake\Event\Event $event The event that was triggered |
||
410 | * @param \Cake\Datasource\EntityInterface $entity The entity that was deleted |
||
411 | * @param \ArrayObject $options Additional options given as an array |
||
412 | * @throws \Cake\Error\FatalErrorException When using this behavior in non-atomic mode |
||
413 | * @return void |
||
414 | */ |
||
415 | public function afterDelete(Event $event, EntityInterface $entity, ArrayObject $options) |
||
434 | |||
435 | /** |
||
436 | * Changes behavior's configuration parameters on the fly. |
||
437 | * |
||
438 | * @param array $config Configuration parameters as `key` => `value` |
||
439 | * @return void |
||
440 | */ |
||
441 | public function configureFieldable($config) |
||
445 | |||
446 | /** |
||
447 | * Enables this behavior. |
||
448 | * |
||
449 | * @return void |
||
450 | */ |
||
451 | public function bindFieldable() |
||
455 | |||
456 | /** |
||
457 | * Disables this behavior. |
||
458 | * |
||
459 | * @return void |
||
460 | */ |
||
461 | public function unbindFieldable() |
||
465 | |||
466 | /** |
||
467 | * The method which actually fetches custom fields. |
||
468 | * |
||
469 | * Fetches all Entity's fields under the `_fields` property. |
||
470 | * |
||
471 | * @param \Cake\Datasource\EntityInterface $entity The entity where to fetch fields |
||
472 | * @return \Cake\Datasource\EntityInterface |
||
473 | */ |
||
474 | public function attachEntityFields(EntityInterface $entity) |
||
491 | |||
492 | /** |
||
493 | * Triggers before/after validate events. |
||
494 | * |
||
495 | * @param \Cake\Datasource\EntityInterface $entity The entity being validated |
||
496 | * @return bool True if save operation should continue, false otherwise |
||
497 | */ |
||
498 | protected function _validation(EntityInterface $entity) |
||
533 | |||
534 | /** |
||
535 | * Alters the given $field and fetches incoming POST data, both "value" and |
||
536 | * "extra" property will be automatically filled for the given $field entity. |
||
537 | * |
||
538 | * @param \Field\Model\Entity\Field $field The field entity for which |
||
539 | * fetch POST information |
||
540 | * @return mixed Raw POST information |
||
541 | */ |
||
542 | protected function _fetchPost(Field $field) |
||
560 | |||
561 | /** |
||
562 | * Gets all attributes that should be attached to the given entity, this entity |
||
563 | * will be used as context to calculate the proper bundle. |
||
564 | * |
||
565 | * @param \Cake\Datasource\EntityInterface $entity Entity context |
||
566 | * @return array |
||
567 | */ |
||
568 | protected function _attributesForEntity(EntityInterface $entity) |
||
605 | |||
606 | /** |
||
607 | * Retrives stored values for all virtual properties by name. This gets all |
||
608 | * values at once. |
||
609 | * |
||
610 | * This method is used to reduce the number of SQl queries, so we get all |
||
611 | * values at once in a single Select instead of creating a select for every |
||
612 | * field attached to the given entity. |
||
613 | * |
||
614 | * @param \Cake\Datasource\EntityInterface $entity The entuity for which |
||
615 | * get related values |
||
616 | * @param array $attrNames List of attribute names for which get their |
||
617 | * values |
||
618 | * @return \Cake\Datasource\ResultSetInterface |
||
619 | */ |
||
620 | protected function _fetchValues(EntityInterface $entity, array $attrNames = []) |
||
644 | |||
645 | /** |
||
646 | * Creates a new Virtual "Field" to be attached to the given entity. |
||
647 | * |
||
648 | * This mock Field represents a new property (table column) of the entity. |
||
649 | * |
||
650 | * @param \Cake\Datasource\EntityInterface $entity The entity where the |
||
651 | * generated virtual field will be attached |
||
652 | * @param \Cake\Datasource\EntityInterface $attribute The attribute where to get |
||
653 | * the information when creating the mock field. |
||
654 | * @return \Field\Model\Entity\Field |
||
655 | */ |
||
656 | protected function _prepareMockField(EntityInterface $entity, EntityInterface $attribute) |
||
715 | |||
716 | /** |
||
717 | * Resolves `bundle` name using $entity as context. |
||
718 | * |
||
719 | * @param \Cake\Datasource\EntityInterface $entity Entity to use as context when |
||
720 | * resolving bundle |
||
721 | * @return string Bundle name as string value, it may be an empty string if no |
||
722 | * bundle should be applied |
||
723 | */ |
||
724 | protected function _resolveBundle(EntityInterface $entity) |
||
734 | } |
||
735 |
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.