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 |
||
39 | trait AttributeObserverTrait |
||
40 | { |
||
41 | |||
42 | /** |
||
43 | * The ID of the attribute to create the values for. |
||
44 | * |
||
45 | * @var integer |
||
46 | */ |
||
47 | protected $attributeId; |
||
48 | |||
49 | /** |
||
50 | * The attribute code of the attribute to create the values for. |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $attributeCode; |
||
55 | |||
56 | /** |
||
57 | * The backend type of the attribute to create the values for. |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $backendType; |
||
62 | |||
63 | /** |
||
64 | * The attribute value in process. |
||
65 | * |
||
66 | * @var mixed |
||
67 | */ |
||
68 | protected $attributeValue; |
||
69 | |||
70 | /** |
||
71 | * The array with the column keys that has to be cleaned up when their values are empty. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $cleanUpEmptyColumnKeys; |
||
76 | |||
77 | /** |
||
78 | * The array with the default column values. |
||
79 | * |
||
80 | * @var array |
||
81 | */ |
||
82 | protected $defaultColumnValues; |
||
83 | |||
84 | /** |
||
85 | * The attribute we're actually processing. |
||
86 | * |
||
87 | * @var array |
||
88 | */ |
||
89 | protected $attribute; |
||
90 | |||
91 | /** |
||
92 | * The entity's existing attribues. |
||
93 | * |
||
94 | * @var array |
||
95 | */ |
||
96 | protected $attributes; |
||
97 | |||
98 | /** |
||
99 | * The operation that has to be executed to update the attribute. |
||
100 | * |
||
101 | * @var string |
||
102 | */ |
||
103 | protected $operation; |
||
104 | |||
105 | /** |
||
106 | * The attribute code that has to be processed. |
||
107 | * |
||
108 | * @return string The attribute code |
||
109 | */ |
||
110 | 1 | public function getAttributeCode() |
|
114 | |||
115 | /** |
||
116 | * The attribute value that has to be processed. |
||
117 | * |
||
118 | * @return string The attribute value |
||
119 | */ |
||
120 | 1 | public function getAttributeValue() |
|
124 | |||
125 | /** |
||
126 | * Get empty attribute value constant from global konfiguration |
||
127 | * |
||
128 | * @return string |
||
129 | */ |
||
130 | 7 | private function getEmptyAttributeValueConstant() |
|
131 | { |
||
132 | 7 | return $this->getSubject()->getConfiguration()->getConfiguration()->getEmptyAttributeValueConstant(); |
|
|
|||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Remove all the empty values from the row and return the cleared row. |
||
137 | * |
||
138 | * @return array The cleared row |
||
139 | */ |
||
140 | 7 | protected function clearRow() |
|
141 | { |
||
142 | |||
143 | // initialize the array with the column keys that has to be cleaned-up |
||
144 | 7 | $this->cleanUpEmptyColumnKeys = array(); |
|
145 | |||
146 | // query whether or not column names that has to be cleaned up have been configured |
||
147 | 7 | if ($this->getSubject()->getConfiguration()->hasParam(ConfigurationKeys::CLEAN_UP_EMPTY_COLUMNS)) { |
|
148 | // if yes, load the column names |
||
149 | $cleanUpEmptyColumns = $this->getSubject()->getCleanUpColumns(); |
||
150 | |||
151 | // translate the column names into column keys |
||
152 | foreach ($cleanUpEmptyColumns as $cleanUpEmptyColumn) { |
||
153 | if ($this->hasHeader($cleanUpEmptyColumn)) { |
||
154 | $this->cleanUpEmptyColumnKeys[$cleanUpEmptyColumn] = $this->getHeader($cleanUpEmptyColumn); |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | |||
159 | // initialize the array with the default column values |
||
160 | 7 | $this->defaultColumnValues = array(); |
|
161 | |||
162 | // iterate over the default column values to figure out whether or not the column exists |
||
163 | 7 | $defaultColumnValues = $this->getSubject()->getDefaultColumnValues(); |
|
164 | |||
165 | // prepare the array with the default column values, BUT we only take |
||
166 | // care of default columns WITHOUT any value, because in only in this |
||
167 | // case the default EAV value from the DB should be used when a empty |
||
168 | // column value has been found to create a NEW attribute value |
||
169 | 7 | foreach ($defaultColumnValues as $columnName => $defaultColumnValue) { |
|
170 | if ($defaultColumnValue === '') { |
||
171 | $this->defaultColumnValues[$columnName] = $this->getHeader($columnName); |
||
172 | } |
||
173 | } |
||
174 | |||
175 | 7 | $emptyValueDefinition = $this->getEmptyAttributeValueConstant(); |
|
176 | // load the header keys |
||
177 | 7 | $headers = in_array($emptyValueDefinition, $this->row, true) ? array_flip($this->getHeaders()) : []; |
|
178 | // remove all the empty values from the row, expected the columns has to be cleaned-up |
||
179 | 7 | foreach ($this->row as $key => $value) { |
|
180 | 7 | if ($value === $emptyValueDefinition) { |
|
181 | $this->cleanUpEmptyColumnKeys[$headers[$key]] = $key; |
||
182 | $this->row[$key] = ''; |
||
183 | } |
||
184 | // query whether or not the value is empty AND the column has NOT to be cleaned-up |
||
185 | 7 | if (($value === null || $value === '') && |
|
186 | 7 | in_array($key, $this->cleanUpEmptyColumnKeys) === false && |
|
187 | 7 | in_array($key, $this->defaultColumnValues) === false |
|
188 | ) { |
||
189 | 1 | unset($this->row[$key]); |
|
190 | } |
||
191 | } |
||
192 | |||
193 | // finally return the clean row |
||
194 | 7 | return $this->row; |
|
195 | } |
||
196 | |||
197 | /** |
||
198 | * Returns the value(s) of the primary key column(s). As the primary key column can |
||
199 | * also consist of two columns, the return value can be an array also. |
||
200 | * |
||
201 | * @return mixed The primary key value(s) |
||
202 | */ |
||
203 | 7 | protected function getPrimaryKeyValue() |
|
207 | |||
208 | /** |
||
209 | * Process the observer's business logic. |
||
210 | * |
||
211 | * @return void |
||
212 | */ |
||
213 | 7 | protected function process() |
|
378 | |||
379 | /** |
||
380 | * Prepare the attributes of the entity that has to be persisted. |
||
381 | * |
||
382 | * @return array|null The prepared attributes |
||
383 | */ |
||
384 | 2 | protected function prepareAttributes() |
|
404 | |||
405 | /** |
||
406 | * Initialize the category product with the passed attributes and returns an instance. |
||
407 | * |
||
408 | * @param array $attr The category product attributes |
||
409 | * |
||
410 | * @return array The initialized category product |
||
411 | */ |
||
412 | 2 | protected function initializeAttribute(array $attr) |
|
416 | |||
417 | /** |
||
418 | * Load's and return's a raw customer entity without primary key but the mandatory members only and nulled values. |
||
419 | * |
||
420 | * @param array $data An array with data that will be used to initialize the raw entity with |
||
421 | * |
||
422 | * @return array The initialized entity |
||
423 | */ |
||
424 | 2 | protected function loadRawEntity(array $data = array()) |
|
447 | |||
448 | /** |
||
449 | * Initialize's and return's a new entity with the status 'create'. |
||
450 | * |
||
451 | * @param array $attr The attributes to merge into the new entity |
||
452 | * |
||
453 | * @return array The initialized entity |
||
454 | */ |
||
455 | 2 | protected function initializeEntity(array $attr = array()) |
|
471 | |||
472 | /** |
||
473 | * Merge's and return's the entity with the passed attributes and set's the |
||
474 | * passed status. |
||
475 | * |
||
476 | * @param array $entity The entity to merge the attributes into |
||
477 | * @param array $attr The attributes to be merged |
||
478 | * @param string|null $changeSetName The change set name to use |
||
479 | * |
||
480 | * @return array The merged entity |
||
481 | */ |
||
482 | protected function mergeEntity(array $entity, array $attr, $changeSetName = null) |
||
511 | |||
512 | /** |
||
513 | * Return's the array with callbacks for the passed type. |
||
514 | * |
||
515 | * @param string $type The type of the callbacks to return |
||
516 | * |
||
517 | * @return array The callbacks |
||
518 | */ |
||
519 | 2 | protected function getCallbacksByType($type) |
|
523 | |||
524 | /** |
||
525 | * Return's mapping for the supported backend types (for the product entity) => persist methods. |
||
526 | * |
||
527 | * @return array The mapping for the supported backend types |
||
528 | */ |
||
529 | 7 | protected function getBackendTypes() |
|
533 | |||
534 | /** |
||
535 | * Return's the attributes for the attribute set of the product that has to be created. |
||
536 | * |
||
537 | * @return array The attributes |
||
538 | * @throws \Exception |
||
539 | */ |
||
540 | 7 | protected function getAttributes() |
|
544 | |||
545 | /** |
||
546 | * Intializes the existing attributes for the entity with the passed primary key. |
||
547 | * |
||
548 | * @param string $pk The primary key of the entity to load the attributes for |
||
549 | * @param integer $storeId The ID of the store view to load the attributes for |
||
550 | * |
||
551 | * @return array The entity attributes |
||
552 | */ |
||
553 | abstract protected function getAttributesByPrimaryKeyAndStoreId($pk, $storeId); |
||
554 | |||
555 | /** |
||
556 | * Return's the logger with the passed name, by default the system logger. |
||
557 | * |
||
558 | * @param string $name The name of the requested system logger |
||
559 | * |
||
560 | * @return \Psr\Log\LoggerInterface The logger instance |
||
561 | * @throws \Exception Is thrown, if the requested logger is NOT available |
||
562 | */ |
||
563 | abstract protected function getSystemLogger($name = LoggerKeys::SYSTEM); |
||
564 | |||
565 | /** |
||
566 | * Return's the PK to create the product => attribute relation. |
||
567 | * |
||
568 | * @return integer The PK to create the relation with |
||
569 | */ |
||
570 | abstract protected function getPrimaryKey(); |
||
571 | |||
572 | /** |
||
573 | * Return's the PK column name to create the product => attribute relation. |
||
574 | * |
||
575 | * @return string The PK column name |
||
576 | */ |
||
577 | abstract protected function getPrimaryKeyMemberName(); |
||
578 | |||
579 | /** |
||
580 | * Return's the column name that contains the primary key. |
||
581 | * |
||
582 | * @return string the column name that contains the primary key |
||
583 | */ |
||
584 | abstract protected function getPrimaryKeyColumnName(); |
||
585 | |||
586 | /** |
||
587 | * Queries whether or not the passed PK and store view code has already been processed. |
||
588 | * |
||
589 | * @param string $pk The PK to check been processed |
||
590 | * @param string $storeViewCode The store view code to check been processed |
||
591 | * |
||
592 | * @return boolean TRUE if the PK and store view code has been processed, else FALSE |
||
593 | */ |
||
594 | abstract protected function storeViewHasBeenProcessed($pk, $storeViewCode); |
||
595 | |||
596 | /** |
||
597 | * Persist's the passed varchar attribute. |
||
598 | * |
||
599 | * @param array $attribute The attribute to persist |
||
600 | * |
||
601 | * @return void |
||
602 | */ |
||
603 | abstract protected function persistVarcharAttribute($attribute); |
||
604 | |||
605 | /** |
||
606 | * Persist's the passed integer attribute. |
||
607 | * |
||
608 | * @param array $attribute The attribute to persist |
||
609 | * |
||
610 | * @return void |
||
611 | */ |
||
612 | abstract protected function persistIntAttribute($attribute); |
||
613 | |||
614 | /** |
||
615 | * Persist's the passed decimal attribute. |
||
616 | * |
||
617 | * @param array $attribute The attribute to persist |
||
618 | * |
||
619 | * @return void |
||
620 | */ |
||
621 | abstract protected function persistDecimalAttribute($attribute); |
||
622 | |||
623 | /** |
||
624 | * Persist's the passed datetime attribute. |
||
625 | * |
||
626 | * @param array $attribute The attribute to persist |
||
627 | * |
||
628 | * @return void |
||
629 | */ |
||
630 | abstract protected function persistDatetimeAttribute($attribute); |
||
631 | |||
632 | /** |
||
633 | * Persist's the passed text attribute. |
||
634 | * |
||
635 | * @param array $attribute The attribute to persist |
||
636 | * |
||
637 | * @return void |
||
638 | */ |
||
639 | abstract protected function persistTextAttribute($attribute); |
||
640 | |||
641 | /** |
||
642 | * Delete's the datetime attribute with the passed value ID. |
||
643 | * |
||
644 | * @param array $row The attributes of the entity to delete |
||
645 | * @param string|null $name The name of the prepared statement that has to be executed |
||
646 | * |
||
647 | * @return void |
||
648 | */ |
||
649 | abstract protected function deleteDatetimeAttribute(array $row, $name = null); |
||
650 | |||
651 | /** |
||
652 | * Delete's the decimal attribute with the passed value ID. |
||
653 | * |
||
654 | * @param array $row The attributes of the entity to delete |
||
655 | * @param string|null $name The name of the prepared statement that has to be executed |
||
656 | * |
||
657 | * @return void |
||
658 | */ |
||
659 | abstract protected function deleteDecimalAttribute(array $row, $name = null); |
||
660 | |||
661 | /** |
||
662 | * Delete's the integer attribute with the passed value ID. |
||
663 | * |
||
664 | * @param array $row The attributes of the entity to delete |
||
665 | * @param string|null $name The name of the prepared statement that has to be executed |
||
666 | * |
||
667 | * @return void |
||
668 | */ |
||
669 | abstract protected function deleteIntAttribute(array $row, $name = null); |
||
670 | |||
671 | /** |
||
672 | * Delete's the text attribute with the passed value ID. |
||
673 | * |
||
674 | * @param array $row The attributes of the entity to delete |
||
675 | * @param string|null $name The name of the prepared statement that has to be executed |
||
676 | * |
||
677 | * @return void |
||
678 | */ |
||
679 | abstract protected function deleteTextAttribute(array $row, $name = null); |
||
680 | |||
681 | /** |
||
682 | * Delete's the varchar attribute with the passed value ID. |
||
683 | * |
||
684 | * @param array $row The attributes of the entity to delete |
||
685 | * @param string|null $name The name of the prepared statement that has to be executed |
||
686 | * |
||
687 | * @return void |
||
688 | */ |
||
689 | abstract protected function deleteVarcharAttribute(array $row, $name = null); |
||
690 | |||
691 | /** |
||
692 | * Query whether or not the entity has to be processed. |
||
693 | * |
||
694 | * @param array $entity The entity to query for |
||
695 | * |
||
696 | * @return boolean TRUE if the entity has to be processed, else FALSE |
||
697 | */ |
||
698 | abstract protected function hasChanges(array $entity); |
||
699 | |||
700 | /** |
||
701 | * Query whether or not a value for the column with the passed name exists. |
||
702 | * |
||
703 | * @param string $name The column name to query for a valid value |
||
704 | * |
||
705 | * @return boolean TRUE if the value is set, else FALSE |
||
706 | */ |
||
707 | abstract protected function hasValue($name); |
||
708 | } |
||
709 |
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.