Complex classes like AttributeObserverTrait 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 AttributeObserverTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | trait AttributeObserverTrait |
||
41 | { |
||
42 | |||
43 | /** |
||
44 | * The ID of the attribute to create the values for. |
||
45 | * |
||
46 | * @var integer |
||
47 | */ |
||
48 | protected $attributeId; |
||
49 | |||
50 | /** |
||
51 | * The attribute code of the attribute to create the values for. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $attributeCode; |
||
56 | |||
57 | /** |
||
58 | * The backend type of the attribute to create the values for. |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $backendType; |
||
63 | |||
64 | /** |
||
65 | * The attribute value in process. |
||
66 | * |
||
67 | * @var mixed |
||
68 | */ |
||
69 | protected $attributeValue; |
||
70 | |||
71 | /** |
||
72 | * The array with the column keys that has to be cleaned up when their values are empty. |
||
73 | * |
||
74 | * @var array |
||
75 | */ |
||
76 | protected $cleanUpEmptyColumnKeys; |
||
77 | |||
78 | /** |
||
79 | * The array with the default column values. |
||
80 | * |
||
81 | * @var array |
||
82 | */ |
||
83 | protected $defaultColumnValues; |
||
84 | |||
85 | /** |
||
86 | * The attribute we're actually processing. |
||
87 | * |
||
88 | * @var array |
||
89 | */ |
||
90 | protected $attribute; |
||
91 | |||
92 | /** |
||
93 | * The entity's existing attribues. |
||
94 | * |
||
95 | * @var array |
||
96 | */ |
||
97 | protected $attributes; |
||
98 | |||
99 | /** |
||
100 | * The operation that has to be executed to update the attribute. |
||
101 | * |
||
102 | * @var string |
||
103 | */ |
||
104 | protected $operation; |
||
105 | |||
106 | /** |
||
107 | * The attribute code that has to be processed. |
||
108 | * |
||
109 | * @return string The attribute code |
||
110 | */ |
||
111 | public function getAttributeCode() |
||
115 | |||
116 | /** |
||
117 | * The attribute value that has to be processed. |
||
118 | * |
||
119 | * @return string The attribute value |
||
120 | */ |
||
121 | public function getAttributeValue() |
||
125 | |||
126 | /** |
||
127 | * Get empty attribute value constant from global konfiguration |
||
128 | * |
||
129 | * @return string |
||
130 | */ |
||
131 | private function getEmptyAttributeValueConstant() |
||
135 | |||
136 | /** |
||
137 | * Remove all the empty values from the row and return the cleared row. |
||
138 | * |
||
139 | * @return array The cleared row |
||
140 | */ |
||
141 | protected function clearRow() |
||
197 | |||
198 | /** |
||
199 | * Returns the value(s) of the primary key column(s). As the primary key column can |
||
200 | * also consist of two columns, the return value can be an array also. |
||
201 | * |
||
202 | * @return mixed The primary key value(s) |
||
203 | */ |
||
204 | protected function getPrimaryKeyValue() |
||
208 | |||
209 | /** |
||
210 | * Process the observer's business logic. |
||
211 | * |
||
212 | * @return void |
||
213 | */ |
||
214 | protected function process() |
||
372 | |||
373 | /** |
||
374 | * Prepare the attributes of the entity that has to be persisted. |
||
375 | * |
||
376 | * @return array|null The prepared attributes |
||
377 | */ |
||
378 | protected function prepareAttributes() |
||
398 | |||
399 | /** |
||
400 | * Initialize the category product with the passed attributes and returns an instance. |
||
401 | * |
||
402 | * @param array $attr The category product attributes |
||
403 | * |
||
404 | * @return array The initialized category product |
||
405 | */ |
||
406 | protected function initializeAttribute(array $attr) |
||
410 | |||
411 | /** |
||
412 | * Load's and return's a raw customer entity without primary key but the mandatory members only and nulled values. |
||
413 | * |
||
414 | * @param array $data An array with data that will be used to initialize the raw entity with |
||
415 | * |
||
416 | * @return array The initialized entity |
||
417 | */ |
||
418 | protected function loadRawEntity(array $data = array()) |
||
441 | |||
442 | /** |
||
443 | * Initialize's and return's a new entity with the status 'create'. |
||
444 | * |
||
445 | * @param array $attr The attributes to merge into the new entity |
||
446 | * |
||
447 | * @return array The initialized entity |
||
448 | */ |
||
449 | protected function initializeEntity(array $attr = array()) |
||
465 | |||
466 | /** |
||
467 | * Merge's and return's the entity with the passed attributes and set's the |
||
468 | * passed status. |
||
469 | * |
||
470 | * @param array $entity The entity to merge the attributes into |
||
471 | * @param array $attr The attributes to be merged |
||
472 | * @param string|null $changeSetName The change set name to use |
||
473 | * |
||
474 | * @return array The merged entity |
||
475 | */ |
||
476 | protected function mergeEntity(array $entity, array $attr, $changeSetName = null) |
||
505 | |||
506 | /** |
||
507 | * Return's the array with callbacks for the passed type. |
||
508 | * |
||
509 | * @param string $type The type of the callbacks to return |
||
510 | * |
||
511 | * @return array The callbacks |
||
512 | */ |
||
513 | protected function getCallbacksByType($type) |
||
517 | |||
518 | /** |
||
519 | * Return's mapping for the supported backend types (for the product entity) => persist methods. |
||
520 | * |
||
521 | * @return array The mapping for the supported backend types |
||
522 | */ |
||
523 | protected function getBackendTypes() |
||
527 | |||
528 | /** |
||
529 | * Return's the attributes for the attribute set of the product that has to be created. |
||
530 | * |
||
531 | * @return array The attributes |
||
532 | * @throws \Exception |
||
533 | */ |
||
534 | protected function getAttributes() |
||
538 | |||
539 | /** |
||
540 | * Intializes the existing attributes for the entity with the passed primary key. |
||
541 | * |
||
542 | * @param string $pk The primary key of the entity to load the attributes for |
||
543 | * @param integer $storeId The ID of the store view to load the attributes for |
||
544 | * |
||
545 | * @return array The entity attributes |
||
546 | */ |
||
547 | abstract protected function getAttributesByPrimaryKeyAndStoreId($pk, $storeId); |
||
548 | |||
549 | /** |
||
550 | * Return's the logger with the passed name, by default the system logger. |
||
551 | * |
||
552 | * @param string $name The name of the requested system logger |
||
553 | * |
||
554 | * @return \Psr\Log\LoggerInterface The logger instance |
||
555 | * @throws \Exception Is thrown, if the requested logger is NOT available |
||
556 | */ |
||
557 | abstract protected function getSystemLogger($name = LoggerKeys::SYSTEM); |
||
558 | |||
559 | /** |
||
560 | * Return's the PK to create the product => attribute relation. |
||
561 | * |
||
562 | * @return integer The PK to create the relation with |
||
563 | */ |
||
564 | abstract protected function getPrimaryKey(); |
||
565 | |||
566 | /** |
||
567 | * Return's the PK column name to create the product => attribute relation. |
||
568 | * |
||
569 | * @return string The PK column name |
||
570 | */ |
||
571 | abstract protected function getPrimaryKeyMemberName(); |
||
572 | |||
573 | /** |
||
574 | * Return's the column name that contains the primary key. |
||
575 | * |
||
576 | * @return string the column name that contains the primary key |
||
577 | */ |
||
578 | abstract protected function getPrimaryKeyColumnName(); |
||
579 | |||
580 | /** |
||
581 | * Queries whether or not the passed PK and store view code has already been processed. |
||
582 | * |
||
583 | * @param string $pk The PK to check been processed |
||
584 | * @param string $storeViewCode The store view code to check been processed |
||
585 | * |
||
586 | * @return boolean TRUE if the PK and store view code has been processed, else FALSE |
||
587 | */ |
||
588 | abstract protected function storeViewHasBeenProcessed($pk, $storeViewCode); |
||
589 | |||
590 | /** |
||
591 | * Persist's the passed varchar attribute. |
||
592 | * |
||
593 | * @param array $attribute The attribute to persist |
||
594 | * |
||
595 | * @return void |
||
596 | */ |
||
597 | abstract protected function persistVarcharAttribute($attribute); |
||
598 | |||
599 | /** |
||
600 | * Persist's the passed integer attribute. |
||
601 | * |
||
602 | * @param array $attribute The attribute to persist |
||
603 | * |
||
604 | * @return void |
||
605 | */ |
||
606 | abstract protected function persistIntAttribute($attribute); |
||
607 | |||
608 | /** |
||
609 | * Persist's the passed decimal attribute. |
||
610 | * |
||
611 | * @param array $attribute The attribute to persist |
||
612 | * |
||
613 | * @return void |
||
614 | */ |
||
615 | abstract protected function persistDecimalAttribute($attribute); |
||
616 | |||
617 | /** |
||
618 | * Persist's the passed datetime attribute. |
||
619 | * |
||
620 | * @param array $attribute The attribute to persist |
||
621 | * |
||
622 | * @return void |
||
623 | */ |
||
624 | abstract protected function persistDatetimeAttribute($attribute); |
||
625 | |||
626 | /** |
||
627 | * Persist's the passed text attribute. |
||
628 | * |
||
629 | * @param array $attribute The attribute to persist |
||
630 | * |
||
631 | * @return void |
||
632 | */ |
||
633 | abstract protected function persistTextAttribute($attribute); |
||
634 | |||
635 | /** |
||
636 | * Delete's the datetime attribute with the passed value ID. |
||
637 | * |
||
638 | * @param array $row The attributes of the entity to delete |
||
639 | * @param string|null $name The name of the prepared statement that has to be executed |
||
640 | * |
||
641 | * @return void |
||
642 | */ |
||
643 | abstract protected function deleteDatetimeAttribute(array $row, $name = null); |
||
644 | |||
645 | /** |
||
646 | * Delete's the decimal attribute with the passed value ID. |
||
647 | * |
||
648 | * @param array $row The attributes of the entity to delete |
||
649 | * @param string|null $name The name of the prepared statement that has to be executed |
||
650 | * |
||
651 | * @return void |
||
652 | */ |
||
653 | abstract protected function deleteDecimalAttribute(array $row, $name = null); |
||
654 | |||
655 | /** |
||
656 | * Delete's the integer attribute with the passed value ID. |
||
657 | * |
||
658 | * @param array $row The attributes of the entity to delete |
||
659 | * @param string|null $name The name of the prepared statement that has to be executed |
||
660 | * |
||
661 | * @return void |
||
662 | */ |
||
663 | abstract protected function deleteIntAttribute(array $row, $name = null); |
||
664 | |||
665 | /** |
||
666 | * Delete's the text attribute with the passed value ID. |
||
667 | * |
||
668 | * @param array $row The attributes of the entity to delete |
||
669 | * @param string|null $name The name of the prepared statement that has to be executed |
||
670 | * |
||
671 | * @return void |
||
672 | */ |
||
673 | abstract protected function deleteTextAttribute(array $row, $name = null); |
||
674 | |||
675 | /** |
||
676 | * Delete's the varchar attribute with the passed value ID. |
||
677 | * |
||
678 | * @param array $row The attributes of the entity to delete |
||
679 | * @param string|null $name The name of the prepared statement that has to be executed |
||
680 | * |
||
681 | * @return void |
||
682 | */ |
||
683 | abstract protected function deleteVarcharAttribute(array $row, $name = null); |
||
684 | |||
685 | /** |
||
686 | * Query whether or not the entity has to be processed. |
||
687 | * |
||
688 | * @param array $entity The entity to query for |
||
689 | * |
||
690 | * @return boolean TRUE if the entity has to be processed, else FALSE |
||
691 | */ |
||
692 | abstract protected function hasChanges(array $entity); |
||
693 | |||
694 | /** |
||
695 | * Query whether or not a value for the column with the passed name exists. |
||
696 | * |
||
697 | * @param string $name The column name to query for a valid value |
||
698 | * |
||
699 | * @return boolean TRUE if the value is set, else FALSE |
||
700 | */ |
||
701 | abstract protected function hasValue($name); |
||
702 | } |
||
703 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.