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 | * Remove all the empty values from the row and return the cleared row. |
||
| 127 | * |
||
| 128 | * @return array The cleared row |
||
| 129 | */ |
||
| 130 | 7 | protected function clearRow() |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Returns the value(s) of the primary key column(s). As the primary key column can |
||
| 182 | * also consist of two columns, the return value can be an array also. |
||
| 183 | * |
||
| 184 | * @return mixed The primary key value(s) |
||
| 185 | */ |
||
| 186 | 7 | protected function getPrimaryKeyValue() |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Process the observer's business logic. |
||
| 193 | * |
||
| 194 | * @return void |
||
| 195 | */ |
||
| 196 | 7 | protected function process() |
|
| 197 | { |
||
| 198 | |||
| 199 | // initialize the store view code |
||
| 200 | 7 | $this->prepareStoreViewCode(); |
|
| 201 | |||
| 202 | // load the store ID, use the admin store if NO store view code has been set |
||
| 203 | 7 | $storeId = $this->getRowStoreId(StoreViewCodes::ADMIN); |
|
| 204 | |||
| 205 | // load the entity's existing attributes |
||
| 206 | 7 | $this->getAttributesByPrimaryKeyAndStoreId($this->getPrimaryKey(), $storeId); |
|
| 207 | |||
| 208 | // load the store view - if no store view has been set, we assume the admin |
||
| 209 | // store view, which will contain the default (fallback) attribute values |
||
| 210 | 7 | $storeViewCode = $this->getSubject()->getStoreViewCode(StoreViewCodes::ADMIN); |
|
| 211 | |||
| 212 | // query whether or not the row has already been processed |
||
| 213 | 7 | if ($this->storeViewHasBeenProcessed($pk = $this->getPrimaryKeyValue(), $storeViewCode)) { |
|
| 214 | // log a message |
||
| 215 | $this->getSystemLogger()->warning( |
||
| 216 | $this->appendExceptionSuffix( |
||
| 217 | sprintf( |
||
| 218 | 'Attributes for "%s" "%s" + store view code "%s" has already been processed', |
||
| 219 | $this->getPrimaryKeyColumnName(), |
||
| 220 | $pk, |
||
| 221 | $storeViewCode |
||
| 222 | ) |
||
| 223 | ) |
||
| 224 | ); |
||
| 225 | |||
| 226 | // return immediately |
||
| 227 | return; |
||
| 228 | } |
||
| 229 | |||
| 230 | // load the attributes by the found attribute set and the backend types |
||
| 231 | 7 | $attributes = $this->getAttributes(); |
|
| 232 | 7 | $backendTypes = $this->getBackendTypes(); |
|
| 233 | |||
| 234 | // load the header keys |
||
| 235 | 7 | $headers = array_flip($this->getHeaders()); |
|
| 236 | |||
| 237 | // remove all the empty values from the row |
||
| 238 | 7 | $row = $this->clearRow(); |
|
| 239 | |||
| 240 | // iterate over the attributes and append them to the row |
||
| 241 | 7 | foreach ($row as $key => $attributeValue) { |
|
| 242 | // query whether or not attribute with the found code exists |
||
| 243 | 6 | if (!isset($attributes[$attributeCode = $headers[$key]])) { |
|
| 244 | // log a message in debug mode |
||
| 245 | 1 | if ($this->isDebugMode()) { |
|
| 246 | 1 | $this->getSystemLogger()->debug( |
|
| 247 | 1 | $this->appendExceptionSuffix( |
|
| 248 | 1 | sprintf( |
|
| 249 | 1 | 'Can\'t find attribute with attribute code "%s"', |
|
| 250 | $attributeCode |
||
| 251 | ) |
||
| 252 | ) |
||
| 253 | ); |
||
| 254 | } |
||
| 255 | |||
| 256 | // stop processing |
||
| 257 | 1 | continue; |
|
| 258 | } else { |
||
| 259 | // log a message in debug mode |
||
| 260 | 5 | if ($this->isDebugMode()) { |
|
| 261 | // log a message in debug mode |
||
| 262 | 2 | $this->getSystemLogger()->debug( |
|
| 263 | 2 | $this->appendExceptionSuffix( |
|
| 264 | 2 | sprintf( |
|
| 265 | 2 | 'Found attribute with attribute code "%s"', |
|
| 266 | $attributeCode |
||
| 267 | ) |
||
| 268 | ) |
||
| 269 | ); |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | // if yes, load the attribute by its code |
||
| 274 | 5 | $this->attribute = $attributes[$attributeCode]; |
|
| 275 | |||
| 276 | // load the backend type => to find the apropriate entity |
||
| 277 | 5 | $backendType = $this->attribute[MemberNames::BACKEND_TYPE]; |
|
| 278 | 5 | if ($backendType === null) { |
|
| 279 | // log a message in debug mode |
||
| 280 | 1 | $this->getSystemLogger()->warning( |
|
| 281 | 1 | $this->appendExceptionSuffix( |
|
| 282 | 1 | sprintf( |
|
| 283 | 1 | 'Found EMTPY backend type for attribute "%s"', |
|
| 284 | $attributeCode |
||
| 285 | ) |
||
| 286 | ) |
||
| 287 | ); |
||
| 288 | // stop processing |
||
| 289 | 1 | continue; |
|
| 290 | } |
||
| 291 | |||
| 292 | // do nothing on static backend type |
||
| 293 | 4 | if ($backendType === BackendTypeKeys::BACKEND_TYPE_STATIC) { |
|
| 294 | 1 | continue; |
|
| 295 | } |
||
| 296 | |||
| 297 | // query whether or not we've found a supported backend type |
||
| 298 | 3 | if (isset($backendTypes[$backendType])) { |
|
| 299 | // initialize attribute ID/code and backend type |
||
| 300 | 2 | $this->backendType = $backendType; |
|
| 301 | 2 | $this->attributeCode = $attributeCode; |
|
| 302 | 2 | $this->attributeId = $this->attribute[MemberNames::ATTRIBUTE_ID]; |
|
| 303 | |||
| 304 | // set the attribute value as well as the original attribute value |
||
| 305 | 2 | $this->attributeValue = $attributeValue; |
|
| 306 | |||
| 307 | // initialize the persist method for the found backend type |
||
| 308 | 2 | list ($persistMethod, , $deleteMethod) = $backendTypes[$backendType]; |
|
| 309 | |||
| 310 | // prepare the attribute vale and query whether or not it has to be persisted |
||
| 311 | 2 | if ($this->hasChanges($value = $this->initializeAttribute($this->prepareAttributes()))) { |
|
| 312 | // query whether or not the entity's value has to be persisted or deleted. if the value is |
||
| 313 | // an empty string and the status is UPDATE, then the value exists and has to be deleted |
||
| 314 | // We need to user $attributeValue instead of $value[MemberNames::VALUE] in cases where |
||
| 315 | // value was casted by attribute type. E.g. special_price = 0 if value is empty string in CSV |
||
| 316 | 2 | switch ($this->operation) { |
|
| 317 | // create/update the attribute |
||
| 318 | 2 | case OperationNames::CREATE: |
|
| 319 | 1 | case OperationNames::UPDATE: |
|
| 320 | 1 | $this->$persistMethod($value); |
|
| 321 | 1 | break; |
|
| 322 | // delete the attribute |
||
| 323 | 1 | case OperationNames::DELETE: |
|
| 324 | $this->$deleteMethod(array(MemberNames::VALUE_ID => $value[MemberNames::VALUE_ID])); |
||
| 325 | break; |
||
| 326 | // skip the attribute |
||
| 327 | 1 | case OperationNames::SKIP: |
|
| 328 | 1 | $this->getSubject()->getSystemLogger()->debug(sprintf('Skipped processing attribute "%s"', $attributeCode)); |
|
| 329 | 1 | break; |
|
| 330 | // should never happen |
||
| 331 | default: |
||
| 332 | 2 | $this->getSubject()->getSystemLogger()->debug(sprintf('Found invalid entity status "%s" for attribute "%s"', $value[MemberNames::VALUE] ?? 'NULL', $attributeCode)); |
|
| 333 | } |
||
| 334 | } else { |
||
| 335 | $this->getSubject()->getSystemLogger()->debug(sprintf('Skip to persist value for attribute "%s"', $attributeCode)); |
||
| 336 | } |
||
| 337 | |||
| 338 | // continue with the next value |
||
| 339 | 2 | continue; |
|
| 340 | } |
||
| 341 | |||
| 342 | // log the debug message |
||
| 343 | 1 | $this->getSystemLogger()->debug( |
|
| 344 | 1 | $this->getSubject()->appendExceptionSuffix( |
|
| 345 | 1 | sprintf( |
|
| 346 | 1 | 'Found invalid backend type %s for attribute "%s"', |
|
| 347 | $backendType, |
||
| 348 | $attributeCode |
||
| 349 | ) |
||
| 350 | ) |
||
| 351 | ); |
||
| 352 | } |
||
| 353 | 7 | } |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Prepare the attributes of the entity that has to be persisted. |
||
| 357 | * |
||
| 358 | * @return array|null The prepared attributes |
||
| 359 | */ |
||
| 360 | 2 | protected function prepareAttributes() |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Initialize the category product with the passed attributes and returns an instance. |
||
| 383 | * |
||
| 384 | * @param array $attr The category product attributes |
||
| 385 | * |
||
| 386 | * @return array The initialized category product |
||
| 387 | */ |
||
| 388 | 2 | protected function initializeAttribute(array $attr) |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Load's and return's a raw customer entity without primary key but the mandatory members only and nulled values. |
||
| 395 | * |
||
| 396 | * @param array $data An array with data that will be used to initialize the raw entity with |
||
| 397 | * |
||
| 398 | * @return array The initialized entity |
||
| 399 | */ |
||
| 400 | 2 | protected function loadRawEntity(array $data = array()) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Initialize's and return's a new entity with the status 'create'. |
||
| 426 | * |
||
| 427 | * @param array $attr The attributes to merge into the new entity |
||
| 428 | * |
||
| 429 | * @return array The initialized entity |
||
| 430 | */ |
||
| 431 | 2 | protected function initializeEntity(array $attr = array()) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Merge's and return's the entity with the passed attributes and set's the |
||
| 450 | * passed status. |
||
| 451 | * |
||
| 452 | * @param array $entity The entity to merge the attributes into |
||
| 453 | * @param array $attr The attributes to be merged |
||
| 454 | * @param string|null $changeSetName The change set name to use |
||
| 455 | * |
||
| 456 | * @return array The merged entity |
||
| 457 | */ |
||
| 458 | protected function mergeEntity(array $entity, array $attr, $changeSetName = null) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Return's the array with callbacks for the passed type. |
||
| 490 | * |
||
| 491 | * @param string $type The type of the callbacks to return |
||
| 492 | * |
||
| 493 | * @return array The callbacks |
||
| 494 | */ |
||
| 495 | 2 | protected function getCallbacksByType($type) |
|
| 499 | |||
| 500 | /** |
||
| 501 | * Return's mapping for the supported backend types (for the product entity) => persist methods. |
||
| 502 | * |
||
| 503 | * @return array The mapping for the supported backend types |
||
| 504 | */ |
||
| 505 | 7 | protected function getBackendTypes() |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Return's the attributes for the attribute set of the product that has to be created. |
||
| 512 | * |
||
| 513 | * @return array The attributes |
||
| 514 | * @throws \Exception |
||
| 515 | */ |
||
| 516 | 7 | protected function getAttributes() |
|
| 520 | |||
| 521 | /** |
||
| 522 | * Intializes the existing attributes for the entity with the passed primary key. |
||
| 523 | * |
||
| 524 | * @param string $pk The primary key of the entity to load the attributes for |
||
| 525 | * @param integer $storeId The ID of the store view to load the attributes for |
||
| 526 | * |
||
| 527 | * @return array The entity attributes |
||
| 528 | */ |
||
| 529 | abstract protected function getAttributesByPrimaryKeyAndStoreId($pk, $storeId); |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Return's the logger with the passed name, by default the system logger. |
||
| 533 | * |
||
| 534 | * @param string $name The name of the requested system logger |
||
| 535 | * |
||
| 536 | * @return \Psr\Log\LoggerInterface The logger instance |
||
| 537 | * @throws \Exception Is thrown, if the requested logger is NOT available |
||
| 538 | */ |
||
| 539 | abstract protected function getSystemLogger($name = LoggerKeys::SYSTEM); |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Return's the PK to create the product => attribute relation. |
||
| 543 | * |
||
| 544 | * @return integer The PK to create the relation with |
||
| 545 | */ |
||
| 546 | abstract protected function getPrimaryKey(); |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Return's the PK column name to create the product => attribute relation. |
||
| 550 | * |
||
| 551 | * @return string The PK column name |
||
| 552 | */ |
||
| 553 | abstract protected function getPrimaryKeyMemberName(); |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Return's the column name that contains the primary key. |
||
| 557 | * |
||
| 558 | * @return string the column name that contains the primary key |
||
| 559 | */ |
||
| 560 | abstract protected function getPrimaryKeyColumnName(); |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Queries whether or not the passed PK and store view code has already been processed. |
||
| 564 | * |
||
| 565 | * @param string $pk The PK to check been processed |
||
| 566 | * @param string $storeViewCode The store view code to check been processed |
||
| 567 | * |
||
| 568 | * @return boolean TRUE if the PK and store view code has been processed, else FALSE |
||
| 569 | */ |
||
| 570 | abstract protected function storeViewHasBeenProcessed($pk, $storeViewCode); |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Persist's the passed varchar attribute. |
||
| 574 | * |
||
| 575 | * @param array $attribute The attribute to persist |
||
| 576 | * |
||
| 577 | * @return void |
||
| 578 | */ |
||
| 579 | abstract protected function persistVarcharAttribute($attribute); |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Persist's the passed integer attribute. |
||
| 583 | * |
||
| 584 | * @param array $attribute The attribute to persist |
||
| 585 | * |
||
| 586 | * @return void |
||
| 587 | */ |
||
| 588 | abstract protected function persistIntAttribute($attribute); |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Persist's the passed decimal attribute. |
||
| 592 | * |
||
| 593 | * @param array $attribute The attribute to persist |
||
| 594 | * |
||
| 595 | * @return void |
||
| 596 | */ |
||
| 597 | abstract protected function persistDecimalAttribute($attribute); |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Persist's the passed datetime attribute. |
||
| 601 | * |
||
| 602 | * @param array $attribute The attribute to persist |
||
| 603 | * |
||
| 604 | * @return void |
||
| 605 | */ |
||
| 606 | abstract protected function persistDatetimeAttribute($attribute); |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Persist's the passed text attribute. |
||
| 610 | * |
||
| 611 | * @param array $attribute The attribute to persist |
||
| 612 | * |
||
| 613 | * @return void |
||
| 614 | */ |
||
| 615 | abstract protected function persistTextAttribute($attribute); |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Delete's the datetime attribute with the passed value ID. |
||
| 619 | * |
||
| 620 | * @param array $row The attributes of the entity to delete |
||
| 621 | * @param string|null $name The name of the prepared statement that has to be executed |
||
| 622 | * |
||
| 623 | * @return void |
||
| 624 | */ |
||
| 625 | abstract protected function deleteDatetimeAttribute(array $row, $name = null); |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Delete's the decimal attribute with the passed value ID. |
||
| 629 | * |
||
| 630 | * @param array $row The attributes of the entity to delete |
||
| 631 | * @param string|null $name The name of the prepared statement that has to be executed |
||
| 632 | * |
||
| 633 | * @return void |
||
| 634 | */ |
||
| 635 | abstract protected function deleteDecimalAttribute(array $row, $name = null); |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Delete's the integer attribute with the passed value ID. |
||
| 639 | * |
||
| 640 | * @param array $row The attributes of the entity to delete |
||
| 641 | * @param string|null $name The name of the prepared statement that has to be executed |
||
| 642 | * |
||
| 643 | * @return void |
||
| 644 | */ |
||
| 645 | abstract protected function deleteIntAttribute(array $row, $name = null); |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Delete's the text attribute with the passed value ID. |
||
| 649 | * |
||
| 650 | * @param array $row The attributes of the entity to delete |
||
| 651 | * @param string|null $name The name of the prepared statement that has to be executed |
||
| 652 | * |
||
| 653 | * @return void |
||
| 654 | */ |
||
| 655 | abstract protected function deleteTextAttribute(array $row, $name = null); |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Delete's the varchar attribute with the passed value ID. |
||
| 659 | * |
||
| 660 | * @param array $row The attributes of the entity to delete |
||
| 661 | * @param string|null $name The name of the prepared statement that has to be executed |
||
| 662 | * |
||
| 663 | * @return void |
||
| 664 | */ |
||
| 665 | abstract protected function deleteVarcharAttribute(array $row, $name = null); |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Query whether or not the entity has to be processed. |
||
| 669 | * |
||
| 670 | * @param array $entity The entity to query for |
||
| 671 | * |
||
| 672 | * @return boolean TRUE if the entity has to be processed, else FALSE |
||
| 673 | */ |
||
| 674 | abstract protected function hasChanges(array $entity); |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Query whether or not a value for the column with the passed name exists. |
||
| 678 | * |
||
| 679 | * @param string $name The column name to query for a valid value |
||
| 680 | * |
||
| 681 | * @return boolean TRUE if the value is set, else FALSE |
||
| 682 | */ |
||
| 683 | abstract protected function hasValue($name); |
||
| 684 | } |
||
| 685 |
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
Idableprovides a methodequalsIdthat 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.