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 | 'attachFields' => 'attachEntityFields', |
||
| 85 | 'fieldable' => 'fieldable', |
||
| 86 | ], |
||
| 87 | ]; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Instance of EavAttributes table. |
||
| 91 | * |
||
| 92 | * @var \Eav\Model\Table\EavAttributesTable |
||
| 93 | */ |
||
| 94 | public $Attributes = null; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Constructor. |
||
| 98 | * |
||
| 99 | * @param \Cake\ORM\Table $table The table this behavior is attached to |
||
| 100 | * @param array $config Configuration array for this behavior |
||
| 101 | */ |
||
| 102 | public function __construct(Table $table, array $config = []) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Returns a list of events this class is implementing. When the class is |
||
| 116 | * registered in an event manager, each individual method will be associated |
||
| 117 | * with the respective event. |
||
| 118 | * |
||
| 119 | * @return void |
||
| 120 | */ |
||
| 121 | public function implementedEvents() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Modifies the query object in order to merge custom fields records into each |
||
| 136 | * entity under the `_fields` property. |
||
| 137 | * |
||
| 138 | * You can enable or disable this behavior for a single `find()` or `get()` |
||
| 139 | * operation by setting `fieldable` or `eav` to false in the options array for |
||
| 140 | * find method. e.g.: |
||
| 141 | * |
||
| 142 | * ```php |
||
| 143 | * $contents = $this->Contents->find('all', ['fieldable' => false]); |
||
| 144 | * $content = $this->Contents->get($id, ['fieldable' => false]); |
||
| 145 | * ``` |
||
| 146 | * |
||
| 147 | * It also looks for custom fields in WHERE clause. This will search entities in |
||
| 148 | * all bundles this table may have, if you need to restrict the search to an |
||
| 149 | * specific bundle you must use the `bundle` key in find()'s options: |
||
| 150 | * |
||
| 151 | * ```php |
||
| 152 | * $this->Contents |
||
| 153 | * ->find('all', ['bundle' => 'articles']) |
||
| 154 | * ->where(['article-title' => 'My first article!']); |
||
| 155 | * ``` |
||
| 156 | * |
||
| 157 | * The `bundle` option has no effects if no custom fields are given in the |
||
| 158 | * WHERE clause. |
||
| 159 | * |
||
| 160 | * @param \Cake\Event\Event $event The beforeFind event that was triggered |
||
| 161 | * @param \Cake\ORM\Query $query The original query to modify |
||
| 162 | * @param \ArrayObject $options Additional options given as an array |
||
| 163 | * @param bool $primary Whether this find is a primary query or not |
||
| 164 | * @return void |
||
| 165 | */ |
||
| 166 | public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * {@inheritDoc} |
||
| 182 | */ |
||
| 183 | protected function _hydrateEntities(CollectionInterface $entities, array $args) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Attaches entity's field under the `_fields` property, this method is invoked |
||
| 204 | * by `beforeFind()` when iterating results sets. |
||
| 205 | * |
||
| 206 | * @param \Cake\Datasource\EntityInterface $entity The entity being altered |
||
| 207 | * @param array $args Arguments given to the originating `beforeFind()` |
||
| 208 | */ |
||
| 209 | protected function _attachEntityFields(EntityInterface $entity, array $args) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Before an entity is saved. |
||
| 224 | * |
||
| 225 | * Here is where we dispatch each custom field's `$_POST` information to its |
||
| 226 | * corresponding Field Handler, so they can operate over their values. |
||
| 227 | * |
||
| 228 | * Fields Handler's `beforeSave()` method is automatically invoked for each |
||
| 229 | * attached field for the entity being processed, your field handler should look |
||
| 230 | * as follow: |
||
| 231 | * |
||
| 232 | * ```php |
||
| 233 | * use Field\Handler; |
||
| 234 | * |
||
| 235 | * class TextField extends Handler |
||
| 236 | * { |
||
| 237 | * public function beforeSave(Field $field, $post) |
||
| 238 | * { |
||
| 239 | * // alter $field, and do nifty things with $post |
||
| 240 | * // return FALSE; will halt the operation |
||
| 241 | * } |
||
| 242 | * } |
||
| 243 | * ``` |
||
| 244 | * |
||
| 245 | * Field Handlers should **alter** `$field->value` and `$field->extra` according |
||
| 246 | * to its needs using the provided **$post** argument. |
||
| 247 | * |
||
| 248 | * **NOTE:** Returning boolean FALSE will halt the whole Entity's save operation. |
||
| 249 | * |
||
| 250 | * @param \Cake\Event\Event $event The event that was triggered |
||
| 251 | * @param \Cake\Datasource\EntityInterface $entity The entity being saved |
||
| 252 | * @param \ArrayObject $options Additional options given as an array |
||
| 253 | * @throws \Cake\Error\FatalErrorException When using this behavior in non-atomic mode |
||
| 254 | * @return bool True if save operation should continue |
||
| 255 | */ |
||
| 256 | public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * After an entity is saved. |
||
| 316 | * |
||
| 317 | * @param \Cake\Event\Event $event The event that was triggered |
||
| 318 | * @param \Cake\Datasource\EntityInterface $entity The entity that was saved |
||
| 319 | * @param \ArrayObject $options Additional options given as an array |
||
| 320 | * @return bool True always |
||
| 321 | */ |
||
| 322 | public function afterSave(Event $event, EntityInterface $entity, ArrayObject $options) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Deletes an entity from a fieldable table. |
||
| 353 | * |
||
| 354 | * @param \Cake\Event\Event $event The event that was triggered |
||
| 355 | * @param \Cake\Datasource\EntityInterface $entity The entity being deleted |
||
| 356 | * @param \ArrayObject $options Additional options given as an array |
||
| 357 | * @return bool |
||
| 358 | * @throws \Cake\Error\FatalErrorException When using this behavior in non-atomic mode |
||
| 359 | */ |
||
| 360 | public function beforeDelete(Event $event, EntityInterface $entity, ArrayObject $options) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * After an entity was removed from database. |
||
| 389 | * |
||
| 390 | * **NOTE:** This method automatically removes all field values from |
||
| 391 | * `eav_values` database table for each entity. |
||
| 392 | * |
||
| 393 | * @param \Cake\Event\Event $event The event that was triggered |
||
| 394 | * @param \Cake\Datasource\EntityInterface $entity The entity that was deleted |
||
| 395 | * @param \ArrayObject $options Additional options given as an array |
||
| 396 | * @throws \Cake\Error\FatalErrorException When using this behavior in non-atomic mode |
||
| 397 | * @return void |
||
| 398 | */ |
||
| 399 | public function afterDelete(Event $event, EntityInterface $entity, ArrayObject $options) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Gets/sets fieldable behavior status. |
||
| 421 | * |
||
| 422 | * @param array|bool|null $status If set to a boolean value then turns on/off |
||
| 423 | * this behavior |
||
| 424 | * @return bool|void |
||
| 425 | */ |
||
| 426 | public function fieldable($status = null) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * The method which actually fetches custom fields. |
||
| 433 | * |
||
| 434 | * Fetches all Entity's fields under the `_fields` property. |
||
| 435 | * |
||
| 436 | * @param \Cake\Datasource\EntityInterface $entity The entity where to fetch fields |
||
| 437 | * @return \Cake\Datasource\EntityInterface |
||
| 438 | */ |
||
| 439 | public function attachEntityFields(EntityInterface $entity) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Triggers before/after validate events. |
||
| 459 | * |
||
| 460 | * @param \Cake\Datasource\EntityInterface $entity The entity being validated |
||
| 461 | * @return bool True if save operation should continue, false otherwise |
||
| 462 | */ |
||
| 463 | protected function _validation(EntityInterface $entity) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Alters the given $field and fetches incoming POST data, both "value" and |
||
| 501 | * "extra" property will be automatically filled for the given $field entity. |
||
| 502 | * |
||
| 503 | * @param \Field\Model\Entity\Field $field The field entity for which fetch POST information |
||
| 504 | * @return mixed Raw POST information |
||
| 505 | */ |
||
| 506 | protected function _fetchPost(Field $field) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Gets all attributes that should be attached to the given entity, this entity |
||
| 527 | * will be used as context to calculate the proper bundle. |
||
| 528 | * |
||
| 529 | * @param \Cake\Datasource\EntityInterface $entity Entity context |
||
| 530 | * @return array |
||
| 531 | */ |
||
| 532 | protected function _attributesForEntity(EntityInterface $entity) |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Retrieves stored values for all virtual properties by name. This gets all |
||
| 572 | * values at once. |
||
| 573 | * |
||
| 574 | * This method is used to reduce the number of SQl queries, so we get all |
||
| 575 | * values at once in a single Select instead of creating a select for every |
||
| 576 | * field attached to the given entity. |
||
| 577 | * |
||
| 578 | * @param \Cake\Datasource\EntityInterface $entity The entity for which get related values |
||
| 579 | * @param array $attrNames List of attribute names for which get their values |
||
| 580 | * @return \Cake\Datasource\ResultSetInterface |
||
| 581 | */ |
||
| 582 | protected function _fetchValues(EntityInterface $entity, array $attrNames = []) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Creates a new Virtual "Field" to be attached to the given entity. |
||
| 609 | * |
||
| 610 | * This mock Field represents a new property (table column) of the entity. |
||
| 611 | * |
||
| 612 | * @param \Cake\Datasource\EntityInterface $entity The entity where the |
||
| 613 | * generated virtual field will be attached |
||
| 614 | * @param \Cake\Datasource\EntityInterface $attribute The attribute where to get |
||
| 615 | * the information when creating the mock field. |
||
| 616 | * @return \Field\Model\Entity\Field |
||
| 617 | */ |
||
| 618 | protected function _prepareMockField(EntityInterface $entity, EntityInterface $attribute) |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Resolves `bundle` name using $entity as context. |
||
| 680 | * |
||
| 681 | * @param \Cake\Datasource\EntityInterface $entity Entity to use as context when resolving bundle |
||
| 682 | * @return string Bundle name as string value, it may be an empty string if no bundle should be applied |
||
| 683 | */ |
||
| 684 | protected function _resolveBundle(EntityInterface $entity) |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Ensures that virtual properties are included in the marshalling process. |
||
| 697 | * |
||
| 698 | * @param \Cake\ORM\Marhshaller $marshaller The marhshaller of the table the behavior is attached to. |
||
| 699 | * @param array $map The property map being built. |
||
| 700 | * @param array $options The options array used in the marshalling call. |
||
| 701 | * @return array A map of `[property => callable]` of additional properties to marshal. |
||
| 702 | */ |
||
| 703 | public function buildMarshalMap($marshaller, $map, $options) |
||
| 707 | } |
||
| 708 |