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 |
||
| 46 | class FieldableBehavior extends EavBehavior |
||
| 47 | { |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Used for reduce BD queries and allow inter-method communication. |
||
| 51 | * Example, it allows to pass some information from beforeDelete() to |
||
| 52 | * afterDelete(). |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $_cache = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Default configuration. |
||
| 60 | * |
||
| 61 | * These are merged with user-provided configuration when the behavior is used. |
||
| 62 | * Available options are: |
||
| 63 | * |
||
| 64 | * - `bundle`: Bundle within this the table. Can be a string or a callable |
||
| 65 | * method that must return a string to use as bundle. Default null. If set to |
||
| 66 | * a callable function, it will receive the entity being saved as first |
||
| 67 | * argument, so you can calculate a bundle name for each particular entity. |
||
| 68 | * |
||
| 69 | * - `enabled`: True enables this behavior or false for disable. Default to |
||
| 70 | * true. |
||
| 71 | * |
||
| 72 | * - `cache`: Column-based cache. See EAV plugin's documentation. |
||
| 73 | * |
||
| 74 | * Bundles are usually set to dynamic values. For example, for the "contents" |
||
| 75 | * table we have "content" entities, but we may have "article contents", "page |
||
| 76 | * contents", etc. depending on the "type of content" they are; is said that |
||
| 77 | * "article" and "page" **are bundles** of "contents" table. |
||
| 78 | * |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | protected $_fieldableDefaultConfig = [ |
||
| 82 | 'bundle' => null, |
||
| 83 | 'implementedMethods' => [ |
||
| 84 | 'configureFieldable' => 'configureFieldable', |
||
| 85 | 'attachFields' => 'attachEntityFields', |
||
| 86 | 'unbindFieldable' => 'unbindFieldable', |
||
| 87 | 'bindFieldable' => 'bindFieldable', |
||
| 88 | ], |
||
| 89 | ]; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Instance of EavAttributes table. |
||
| 93 | * |
||
| 94 | * @var \Eav\Model\Table\EavAttributesTable |
||
| 95 | */ |
||
| 96 | public $Attributes = null; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Constructor. |
||
| 100 | * |
||
| 101 | * @param \Cake\ORM\Table $table The table this behavior is attached to |
||
| 102 | * @param array $config Configuration array for this behavior |
||
| 103 | */ |
||
| 104 | public function __construct(Table $table, array $config = []) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Returns a list of events this class is implementing. When the class is |
||
| 118 | * registered in an event manager, each individual method will be associated |
||
| 119 | * with the respective event. |
||
| 120 | * |
||
| 121 | * @return void |
||
| 122 | */ |
||
| 123 | public function implementedEvents() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Modifies the query object in order to merge custom fields records |
||
| 138 | * into each entity under the `_fields` property. |
||
| 139 | * |
||
| 140 | * ### Events Triggered: |
||
| 141 | * |
||
| 142 | * - `Field.<FieldHandler>.Entity.beforeFind`: This event is triggered for each |
||
| 143 | * entity in the resulting collection and for each field attached to these |
||
| 144 | * entities. It receives three arguments, a field entity representing the |
||
| 145 | * field being processed, an options array and boolean value indicating |
||
| 146 | * whether the query that initialized the event is part of a primary find |
||
| 147 | * operation or not. Returning false will cause the entity to be removed from |
||
| 148 | * the resulting collection, also will stop event propagation, so other |
||
| 149 | * fields won't be able to listen this event. If the event is stopped using |
||
| 150 | * the event API, will halt the entire find operation. |
||
| 151 | * |
||
| 152 | * You can enable or disable this behavior for a single `find()` or `get()` |
||
| 153 | * operation by setting `fieldable` or `eav` to false in the options array for |
||
| 154 | * find method. e.g.: |
||
| 155 | * |
||
| 156 | * ```php |
||
| 157 | * $contents = $this->Contents->find('all', ['fieldable' => false]); |
||
| 158 | * $content = $this->Contents->get($id, ['fieldable' => false]); |
||
| 159 | * ``` |
||
| 160 | * |
||
| 161 | * It also looks for custom fields in WHERE clause. This will search entities in |
||
| 162 | * all bundles this table may have, if you need to restrict the search to an |
||
| 163 | * specific bundle you must use the `bundle` key in find()'s options: |
||
| 164 | * |
||
| 165 | * ```php |
||
| 166 | * $this->Contents |
||
| 167 | * ->find('all', ['bundle' => 'articles']) |
||
| 168 | * ->where(['article-title' => 'My first article!']); |
||
| 169 | * ``` |
||
| 170 | * |
||
| 171 | * The `bundle` option has no effects if no custom fields are given in the |
||
| 172 | * WHERE clause. |
||
| 173 | * |
||
| 174 | * @param \Cake\Event\Event $event The beforeFind event that was triggered |
||
| 175 | * @param \Cake\ORM\Query $query The original query to modify |
||
| 176 | * @param \ArrayObject $options Additional options given as an array |
||
| 177 | * @param bool $primary Whether this find is a primary query or not |
||
| 178 | * @return void |
||
| 179 | */ |
||
| 180 | public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary) |
||
| 194 | |||
| 195 | |||
| 196 | /** |
||
| 197 | * {@inheritDoc} |
||
| 198 | */ |
||
| 199 | public function hydrateEntities(CollectionInterface $entities, array $options) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * {@inheritDoc} |
||
| 222 | * |
||
| 223 | * Attaches entity's field under the `_fields` property, this method is invoked |
||
| 224 | * by `beforeFind()` when iterating results sets. |
||
| 225 | */ |
||
| 226 | public function attachEntityAttributes(EntityInterface $entity, array $options) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Before an entity is saved. |
||
| 243 | * |
||
| 244 | * Here is where we dispatch each custom field's `$_POST` information to its |
||
| 245 | * corresponding Field Handler, so they can operate over their values. |
||
| 246 | * |
||
| 247 | * Fields Handler's `beforeSave()` method is automatically invoked for each |
||
| 248 | * attached field for the entity being processed, your field handler should look |
||
| 249 | * as follow: |
||
| 250 | * |
||
| 251 | * ```php |
||
| 252 | * use Field\Handler; |
||
| 253 | * |
||
| 254 | * class TextField extends Handler |
||
| 255 | * { |
||
| 256 | * public function beforeSave(Field $field, $post) |
||
| 257 | * { |
||
| 258 | * // alter $field, and do nifty things with $post |
||
| 259 | * // return FALSE; will halt the operation |
||
| 260 | * } |
||
| 261 | * } |
||
| 262 | * ``` |
||
| 263 | * |
||
| 264 | * Field Handlers should **alter** `$field->value` and `$field->extra` according |
||
| 265 | * to its needs using the provided **$post** argument. |
||
| 266 | * |
||
| 267 | * **NOTE:** Returning boolean FALSE will halt the whole Entity's save operation. |
||
| 268 | * |
||
| 269 | * @param \Cake\Event\Event $event The event that was triggered |
||
| 270 | * @param \Cake\Datasource\EntityInterface $entity The entity being saved |
||
| 271 | * @param \ArrayObject $options Additional options given as an array |
||
| 272 | * @throws \Cake\Error\FatalErrorException When using this behavior in non-atomic mode |
||
| 273 | * @return bool True if save operation should continue |
||
| 274 | */ |
||
| 275 | public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * After an entity is saved. |
||
| 332 | * |
||
| 333 | * ### Events Triggered: |
||
| 334 | * |
||
| 335 | * - `Field.<FieldHandler>.Entity.afterSave`: Will be triggered after a |
||
| 336 | * successful insert or save, listeners will receive two arguments, the field |
||
| 337 | * entity and the options array. The type of operation performed (insert or |
||
| 338 | * update) can be infer by checking the field entity's method `isNew`, true |
||
| 339 | * meaning an insert and false an update. |
||
| 340 | * |
||
| 341 | * @param \Cake\Event\Event $event The event that was triggered |
||
| 342 | * @param \Cake\Datasource\EntityInterface $entity The entity that was saved |
||
| 343 | * @param \ArrayObject $options Additional options given as an array |
||
| 344 | * @return bool True always |
||
| 345 | */ |
||
| 346 | public function afterSave(Event $event, EntityInterface $entity, ArrayObject $options) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Deletes an entity from a fieldable table. |
||
| 377 | * |
||
| 378 | * ### Events Triggered: |
||
| 379 | * |
||
| 380 | * - `Field.<FieldHandler>.Entity.beforeDelete`: Fired before the delete occurs. |
||
| 381 | * If stopped the delete will be aborted. Receives as arguments the field |
||
| 382 | * entity and options array. |
||
| 383 | * |
||
| 384 | * @param \Cake\Event\Event $event The event that was triggered |
||
| 385 | * @param \Cake\Datasource\EntityInterface $entity The entity being deleted |
||
| 386 | * @param \ArrayObject $options Additional options given as an array |
||
| 387 | * @return bool |
||
| 388 | * @throws \Cake\Error\FatalErrorException When using this behavior in non-atomic mode |
||
| 389 | */ |
||
| 390 | public function beforeDelete(Event $event, EntityInterface $entity, ArrayObject $options) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * After an entity was removed from database. |
||
| 418 | * |
||
| 419 | * ### Events Triggered: |
||
| 420 | * |
||
| 421 | * - `Field.<FieldHandler>.Entity.afterDelete`: Fired after the delete has been |
||
| 422 | * successful. Receives as arguments the field entity and options array. |
||
| 423 | * |
||
| 424 | * **NOTE:** This method automatically removes all field values from |
||
| 425 | * `eav_values` database table for each entity. |
||
| 426 | * |
||
| 427 | * @param \Cake\Event\Event $event The event that was triggered |
||
| 428 | * @param \Cake\Datasource\EntityInterface $entity The entity that was deleted |
||
| 429 | * @param \ArrayObject $options Additional options given as an array |
||
| 430 | * @throws \Cake\Error\FatalErrorException When using this behavior in non-atomic mode |
||
| 431 | * @return void |
||
| 432 | */ |
||
| 433 | public function afterDelete(Event $event, EntityInterface $entity, ArrayObject $options) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Changes behavior's configuration parameters on the fly. |
||
| 455 | * |
||
| 456 | * @param array $config Configuration parameters as `key` => `value` |
||
| 457 | * @return void |
||
| 458 | */ |
||
| 459 | public function configureFieldable($config) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Enables this behavior. |
||
| 466 | * |
||
| 467 | * @return void |
||
| 468 | */ |
||
| 469 | public function bindFieldable() |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Disables this behavior. |
||
| 476 | * |
||
| 477 | * @return void |
||
| 478 | */ |
||
| 479 | public function unbindFieldable() |
||
| 483 | |||
| 484 | /** |
||
| 485 | * The method which actually fetches custom fields. |
||
| 486 | * |
||
| 487 | * Fetches all Entity's fields under the `_fields` property. |
||
| 488 | * |
||
| 489 | * @param \Cake\Datasource\EntityInterface $entity The entity where to fetch fields |
||
| 490 | * @return \Cake\Datasource\EntityInterface |
||
| 491 | */ |
||
| 492 | public function attachEntityFields(EntityInterface $entity) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Triggers before/after validate events. |
||
| 511 | * |
||
| 512 | * @param \Cake\Datasource\EntityInterface $entity The entity being validated |
||
| 513 | * @return bool True if save operation should continue, false otherwise |
||
| 514 | */ |
||
| 515 | protected function _validation(EntityInterface $entity) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Alters the given $field and fetches incoming POST data, both "value" and |
||
| 552 | * "extra" property will be automatically filled for the given $field entity. |
||
| 553 | * |
||
| 554 | * @param \Field\Model\Entity\Field $field The field entity for which |
||
| 555 | * fetch POST information |
||
| 556 | * @return mixed Raw POST information |
||
| 557 | */ |
||
| 558 | protected function _fetchPost(Field $field) |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Gets all attributes that should be attached to the given entity, this entity |
||
| 579 | * will be used as context to calculate the proper bundle. |
||
| 580 | * |
||
| 581 | * @param \Cake\Datasource\EntityInterface $entity Entity context |
||
| 582 | * @return array |
||
| 583 | */ |
||
| 584 | protected function _attributesForEntity(EntityInterface $entity) |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Retrives stored values for all virtual properties by name. This gets all |
||
| 623 | * values at once. |
||
| 624 | * |
||
| 625 | * This method is used to reduce the number of SQl queries, so we get all |
||
| 626 | * values at once in a single Select instead of creating a select for every |
||
| 627 | * field attached to the given entity. |
||
| 628 | * |
||
| 629 | * @param \Cake\Datasource\EntityInterface $entity The entuity for which |
||
| 630 | * get related values |
||
| 631 | * @param array $attrNames List of attribute names for which get their |
||
| 632 | * values |
||
| 633 | * @return \Cake\Datasource\ResultSetInterface |
||
| 634 | */ |
||
| 635 | protected function _fetchValues(EntityInterface $entity, array $attrNames = []) |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Creates a new Virtual "Field" to be attached to the given entity. |
||
| 661 | * |
||
| 662 | * This mock Field represents a new property (table column) of the entity. |
||
| 663 | * |
||
| 664 | * @param \Cake\Datasource\EntityInterface $entity The entity where the |
||
| 665 | * generated virtual field will be attached |
||
| 666 | * @param \Cake\Datasource\EntityInterface $attribute The attribute where to get |
||
| 667 | * the information when creating the mock field. |
||
| 668 | * @return \Field\Model\Entity\Field |
||
| 669 | */ |
||
| 670 | protected function _prepareMockField(EntityInterface $entity, EntityInterface $attribute) |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Resolves `bundle` name using $entity as context. |
||
| 731 | * |
||
| 732 | * @param \Cake\Datasource\EntityInterface $entity Entity to use as context when |
||
| 733 | * resolving bundle |
||
| 734 | * @return string Bundle name as string value, it may be an empty string if no |
||
| 735 | * bundle should be applied |
||
| 736 | */ |
||
| 737 | protected function _resolveBundle(EntityInterface $entity) |
||
| 746 | } |
||
| 747 |
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.