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 BunchSubject 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 BunchSubject, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class BunchSubject extends AbstractProductSubject implements ExportableSubjectInterface |
||
| 40 | { |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The trait that implements the export functionality. |
||
| 44 | * |
||
| 45 | * @var \TechDivision\Import\Subjects\ExportableTrait |
||
| 46 | */ |
||
| 47 | use ExportableTrait; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The array with the pre-loaded entity IDs. |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $preLoadedEntityIds = array(); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Mappings for the table column => CSV column header. |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected $headerStockMappings = array( |
||
| 62 | 'qty' => array('qty', 'float'), |
||
| 63 | 'min_qty' => array('out_of_stock_qty', 'float'), |
||
| 64 | 'use_config_min_qty' => array('use_config_min_qty', 'int'), |
||
| 65 | 'is_qty_decimal' => array('is_qty_decimal', 'int'), |
||
| 66 | 'backorders' => array('allow_backorders', 'int'), |
||
| 67 | 'use_config_backorders' => array('use_config_backorders', 'int'), |
||
| 68 | 'min_sale_qty' => array('min_cart_qty', 'float'), |
||
| 69 | 'use_config_min_sale_qty' => array('use_config_min_sale_qty', 'int'), |
||
| 70 | 'max_sale_qty' => array('max_cart_qty', 'float'), |
||
| 71 | 'use_config_max_sale_qty' => array('use_config_max_sale_qty', 'int'), |
||
| 72 | 'is_in_stock' => array('is_in_stock', 'int'), |
||
| 73 | 'notify_stock_qty' => array('notify_on_stock_below', 'float'), |
||
| 74 | 'use_config_notify_stock_qty' => array('use_config_notify_stock_qty', 'int'), |
||
| 75 | 'manage_stock' => array('manage_stock', 'int'), |
||
| 76 | 'use_config_manage_stock' => array('use_config_manage_stock', 'int'), |
||
| 77 | 'use_config_qty_increments' => array('use_config_qty_increments', 'int'), |
||
| 78 | 'qty_increments' => array('qty_increments', 'float'), |
||
| 79 | 'use_config_enable_qty_inc' => array('use_config_enable_qty_inc', 'int'), |
||
| 80 | 'enable_qty_increments' => array('enable_qty_increments', 'int'), |
||
| 81 | 'is_decimal_divided' => array('is_decimal_divided', 'int'), |
||
| 82 | ); |
||
| 83 | |||
| 84 | /** |
||
| 85 | * The array with the available visibility keys. |
||
| 86 | * |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | protected $availableVisibilities = array( |
||
| 90 | 'Not Visible Individually' => VisibilityKeys::VISIBILITY_NOT_VISIBLE, |
||
| 91 | 'Catalog' => VisibilityKeys::VISIBILITY_IN_CATALOG, |
||
| 92 | 'Search' => VisibilityKeys::VISIBILITY_IN_SEARCH, |
||
| 93 | 'Catalog, Search' => VisibilityKeys::VISIBILITY_BOTH |
||
| 94 | ); |
||
| 95 | |||
| 96 | /** |
||
| 97 | * The category IDs the product is related with. |
||
| 98 | * |
||
| 99 | * @var array |
||
| 100 | */ |
||
| 101 | protected $productCategoryIds = array(); |
||
| 102 | |||
| 103 | /** |
||
| 104 | * The default callback mappings for the Magento standard product attributes. |
||
| 105 | * |
||
| 106 | * @var array |
||
| 107 | */ |
||
| 108 | protected $defaultCallbackMappings = array( |
||
| 109 | 'visibility' => array('import_product.callback.visibility'), |
||
| 110 | 'tax_class_id' => array('import_product.callback.tax.class'), |
||
| 111 | 'bundle_price_type' => array('import_product_bundle.callback.bundle.type'), |
||
| 112 | 'bundle_sku_type' => array('import_product_bundle.callback.bundle.type'), |
||
| 113 | 'bundle_weight_type' => array('import_product_bundle.callback.bundle.type'), |
||
| 114 | 'bundle_price_view' => array('import_product_bundle.callback.bundle.price.view'), |
||
| 115 | 'bundle_shipment_type' => array('import_product_bundle.callback.bundle.shipment.type') |
||
| 116 | ); |
||
| 117 | |||
| 118 | /** |
||
| 119 | * The used URL keys. |
||
| 120 | * |
||
| 121 | * @var array |
||
| 122 | */ |
||
| 123 | protected $usedUrlKeys = array(); |
||
| 124 | |||
| 125 | /** |
||
| 126 | * The available entity types. |
||
| 127 | * |
||
| 128 | * @var array |
||
| 129 | */ |
||
| 130 | protected $entityTypes = array(); |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Intializes the previously loaded global data for exactly one bunch. |
||
| 134 | * |
||
| 135 | * @param string $serial The serial of the actual import |
||
| 136 | * |
||
| 137 | * @return void |
||
| 138 | * @see \Importer\Csv\Actions\ProductImportAction::prepare() |
||
| 139 | */ |
||
| 140 | public function setUp($serial) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Clean up the global data after importing the bunch. |
||
| 155 | * |
||
| 156 | * @param string $serial The serial of the actual import |
||
| 157 | * |
||
| 158 | * @return void |
||
| 159 | */ |
||
| 160 | public function tearDown($serial) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Return's the default callback mappings. |
||
| 180 | * |
||
| 181 | * @return array The default callback mappings |
||
| 182 | */ |
||
| 183 | public function getDefaultCallbackMappings() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Return's the mappings for the table column => CSV column header. |
||
| 190 | * |
||
| 191 | * @return array The header stock mappings |
||
| 192 | */ |
||
| 193 | 1 | public function getHeaderStockMappings() |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Return's the visibility key for the passed visibility string. |
||
| 200 | * |
||
| 201 | * @param string $visibility The visibility string to return the key for |
||
| 202 | * |
||
| 203 | * @return integer The requested visibility key |
||
| 204 | * @throws \Exception Is thrown, if the requested visibility is not available |
||
| 205 | */ |
||
| 206 | public function getVisibilityIdByValue($visibility) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Add the passed category ID to the product's category list. |
||
| 224 | * |
||
| 225 | * @param integer $categoryId The category ID to add |
||
| 226 | * |
||
| 227 | * @return void |
||
| 228 | */ |
||
| 229 | public function addProductCategoryId($categoryId) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Return's the list with category IDs the product is related with. |
||
| 236 | * |
||
| 237 | * @return array The product's category IDs |
||
| 238 | */ |
||
| 239 | public function getProductCategoryIds() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Pre-load the entity ID for the product with the passed SKU |
||
| 256 | * and persist it temporary in the registry. |
||
| 257 | * |
||
| 258 | * @param string $sku The SKU of the product to be pre-loaded |
||
| 259 | * |
||
| 260 | * @return void |
||
| 261 | */ |
||
| 262 | public function preLoadEntityId($sku) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Make's the passed URL key unique by adding the next number to the end. |
||
| 274 | * |
||
| 275 | * @param string $urlKey The URL key to make unique |
||
| 276 | * @param integer $pk The PK the URL key is related with |
||
| 277 | * |
||
| 278 | * @return string The unique URL key |
||
| 279 | */ |
||
| 280 | public function makeUrlKeyUnique($urlKey, $pk) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Return's the entity type for the configured entity type code. |
||
| 349 | * |
||
| 350 | * @return array The requested entity type |
||
| 351 | * @throws \Exception Is thrown, if the requested entity type is not available |
||
| 352 | */ |
||
| 353 | View Code Duplication | protected function getEntityType() |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Return's TRUE, if the passed URL key varchar value IS related with the passed PK. |
||
| 371 | * |
||
| 372 | * @param array $productVarcharAttribute The varchar value to check |
||
| 373 | * @param integer $pk The primary key to check |
||
| 374 | * |
||
| 375 | * @return boolean TRUE if the URL key is related, else FALSE |
||
| 376 | */ |
||
| 377 | protected function isUrlKeyOf(array $productVarcharAttribute, $pk) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Return's an array with the available EAV attributes for the passed is user defined flag. |
||
| 384 | * |
||
| 385 | * @param integer $isUserDefined The flag itself |
||
| 386 | * |
||
| 387 | * @return array The array with the EAV attributes matching the passed flag |
||
| 388 | */ |
||
| 389 | public function getEavAttributeByIsUserDefined($isUserDefined = 1) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Return's the URL rewrites for the passed URL entity type and ID. |
||
| 396 | * |
||
| 397 | * @param string $entityType The entity type to load the URL rewrites for |
||
| 398 | * @param integer $entityId The entity ID to laod the rewrites for |
||
| 399 | * |
||
| 400 | * @return array The URL rewrites |
||
| 401 | */ |
||
| 402 | 1 | public function getUrlRewritesByEntityTypeAndEntityId($entityType, $entityId) |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Load's and return's the product with the passed SKU. |
||
| 409 | * |
||
| 410 | * @param string $sku The SKU of the product to load |
||
| 411 | * |
||
| 412 | * @return array The product |
||
| 413 | */ |
||
| 414 | public function loadProduct($sku) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Load's and return's the product website relation with the passed product and website ID. |
||
| 421 | * |
||
| 422 | * @param string $productId The product ID of the relation |
||
| 423 | * @param string $websiteId The website ID of the relation |
||
| 424 | * |
||
| 425 | * @return array The product website |
||
| 426 | */ |
||
| 427 | public function loadProductWebsite($productId, $websiteId) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Return's the category product relation with the passed category/product ID. |
||
| 434 | * |
||
| 435 | * @param integer $categoryId The category ID of the category product relation to return |
||
| 436 | * @param integer $productId The product ID of the category product relation to return |
||
| 437 | * |
||
| 438 | * @return array The category product relation |
||
| 439 | */ |
||
| 440 | public function loadCategoryProduct($categoryId, $productId) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Load's and return's the stock status with the passed product/website/stock ID. |
||
| 447 | * |
||
| 448 | * @param integer $productId The product ID of the stock status to load |
||
| 449 | * @param integer $websiteId The website ID of the stock status to load |
||
| 450 | * @param integer $stockId The stock ID of the stock status to load |
||
| 451 | * |
||
| 452 | * @return array The stock status |
||
| 453 | */ |
||
| 454 | public function loadStockStatus($productId, $websiteId, $stockId) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Load's and return's the stock status with the passed product/website/stock ID. |
||
| 461 | * |
||
| 462 | * @param integer $productId The product ID of the stock item to load |
||
| 463 | * @param integer $websiteId The website ID of the stock item to load |
||
| 464 | * @param integer $stockId The stock ID of the stock item to load |
||
| 465 | * |
||
| 466 | * @return array The stock item |
||
| 467 | */ |
||
| 468 | public function loadStockItem($productId, $websiteId, $stockId) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Load's and return's the datetime attribute with the passed entity/attribute/store ID. |
||
| 475 | * |
||
| 476 | * @param integer $entityId The entity ID of the attribute |
||
| 477 | * @param integer $attributeId The attribute ID of the attribute |
||
| 478 | * @param integer $storeId The store ID of the attribute |
||
| 479 | * |
||
| 480 | * @return array|null The datetime attribute |
||
| 481 | */ |
||
| 482 | public function loadProductDatetimeAttribute($entityId, $attributeId, $storeId) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Load's and return's the decimal attribute with the passed entity/attribute/store ID. |
||
| 489 | * |
||
| 490 | * @param integer $entityId The entity ID of the attribute |
||
| 491 | * @param integer $attributeId The attribute ID of the attribute |
||
| 492 | * @param integer $storeId The store ID of the attribute |
||
| 493 | * |
||
| 494 | * @return array|null The decimal attribute |
||
| 495 | */ |
||
| 496 | public function loadProductDecimalAttribute($entityId, $attributeId, $storeId) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Load's and return's the integer attribute with the passed entity/attribute/store ID. |
||
| 503 | * |
||
| 504 | * @param integer $entityId The entity ID of the attribute |
||
| 505 | * @param integer $attributeId The attribute ID of the attribute |
||
| 506 | * @param integer $storeId The store ID of the attribute |
||
| 507 | * |
||
| 508 | * @return array|null The integer attribute |
||
| 509 | */ |
||
| 510 | public function loadProductIntAttribute($entityId, $attributeId, $storeId) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Load's and return's the text attribute with the passed entity/attribute/store ID. |
||
| 517 | * |
||
| 518 | * @param integer $entityId The entity ID of the attribute |
||
| 519 | * @param integer $attributeId The attribute ID of the attribute |
||
| 520 | * @param integer $storeId The store ID of the attribute |
||
| 521 | * |
||
| 522 | * @return array|null The text attribute |
||
| 523 | */ |
||
| 524 | public function loadProductTextAttribute($entityId, $attributeId, $storeId) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Load's and return's the varchar attribute with the passed entity/attribute/store ID. |
||
| 531 | * |
||
| 532 | * @param integer $entityId The entity ID of the attribute |
||
| 533 | * @param integer $attributeId The attribute ID of the attribute |
||
| 534 | * @param integer $storeId The store ID of the attribute |
||
| 535 | * |
||
| 536 | * @return array|null The varchar attribute |
||
| 537 | */ |
||
| 538 | public function loadProductVarcharAttribute($entityId, $attributeId, $storeId) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Return's the URL rewrite product category relation for the passed |
||
| 545 | * product and category ID. |
||
| 546 | * |
||
| 547 | * @param integer $productId The product ID to load the URL rewrite product category relation for |
||
| 548 | * @param integer $categoryId The category ID to load the URL rewrite product category relation for |
||
| 549 | * |
||
| 550 | * @return array|false The URL rewrite product category relations |
||
| 551 | */ |
||
| 552 | public function loadUrlRewriteProductCategory($productId, $categoryId) |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Persist's the passed product data and return's the ID. |
||
| 559 | * |
||
| 560 | * @param array $product The product data to persist |
||
| 561 | * |
||
| 562 | * @return string The ID of the persisted entity |
||
| 563 | */ |
||
| 564 | public function persistProduct($product) |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Persist's the passed product varchar attribute. |
||
| 571 | * |
||
| 572 | * @param array $attribute The attribute to persist |
||
| 573 | * |
||
| 574 | * @return void |
||
| 575 | */ |
||
| 576 | public function persistProductVarcharAttribute($attribute) |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Persist's the passed product integer attribute. |
||
| 583 | * |
||
| 584 | * @param array $attribute The attribute to persist |
||
| 585 | * |
||
| 586 | * @return void |
||
| 587 | */ |
||
| 588 | public function persistProductIntAttribute($attribute) |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Persist's the passed product decimal attribute. |
||
| 595 | * |
||
| 596 | * @param array $attribute The attribute to persist |
||
| 597 | * |
||
| 598 | * @return void |
||
| 599 | */ |
||
| 600 | public function persistProductDecimalAttribute($attribute) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Persist's the passed product datetime attribute. |
||
| 607 | * |
||
| 608 | * @param array $attribute The attribute to persist |
||
| 609 | * |
||
| 610 | * @return void |
||
| 611 | */ |
||
| 612 | public function persistProductDatetimeAttribute($attribute) |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Persist's the passed product text attribute. |
||
| 619 | * |
||
| 620 | * @param array $attribute The attribute to persist |
||
| 621 | * |
||
| 622 | * @return void |
||
| 623 | */ |
||
| 624 | public function persistProductTextAttribute($attribute) |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Persist's the passed product website data and return's the ID. |
||
| 631 | * |
||
| 632 | * @param array $productWebsite The product website data to persist |
||
| 633 | * |
||
| 634 | * @return void |
||
| 635 | */ |
||
| 636 | public function persistProductWebsite($productWebsite) |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Persist's the passed category product relation. |
||
| 643 | * |
||
| 644 | * @param array $categoryProduct The category product relation to persist |
||
| 645 | * |
||
| 646 | * @return void |
||
| 647 | */ |
||
| 648 | public function persistCategoryProduct($categoryProduct) |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Persist's the passed stock item data and return's the ID. |
||
| 655 | * |
||
| 656 | * @param array $stockItem The stock item data to persist |
||
| 657 | * |
||
| 658 | * @return void |
||
| 659 | */ |
||
| 660 | public function persistStockItem($stockItem) |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Persist's the passed stock status data and return's the ID. |
||
| 667 | * |
||
| 668 | * @param array $stockStatus The stock status data to persist |
||
| 669 | * |
||
| 670 | * @return void |
||
| 671 | */ |
||
| 672 | public function persistStockStatus($stockStatus) |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Persist's the URL rewrite with the passed data. |
||
| 679 | * |
||
| 680 | * @param array $row The URL rewrite to persist |
||
| 681 | * |
||
| 682 | * @return string The ID of the persisted entity |
||
| 683 | */ |
||
| 684 | 1 | public function persistUrlRewrite($row) |
|
| 688 | |||
| 689 | /** |
||
| 690 | * Persist's the URL rewrite product => category relation with the passed data. |
||
| 691 | * |
||
| 692 | * @param array $row The URL rewrite product => category relation to persist |
||
| 693 | * |
||
| 694 | * @return void |
||
| 695 | */ |
||
| 696 | public function persistUrlRewriteProductCategory($row) |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Delete's the entity with the passed attributes. |
||
| 703 | * |
||
| 704 | * @param array $row The attributes of the entity to delete |
||
| 705 | * @param string|null $name The name of the prepared statement that has to be executed |
||
| 706 | * |
||
| 707 | * @return void |
||
| 708 | */ |
||
| 709 | public function deleteProduct($row, $name = null) |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Delete's the URL rewrite(s) with the passed attributes. |
||
| 716 | * |
||
| 717 | * @param array $row The attributes of the entity to delete |
||
| 718 | * @param string|null $name The name of the prepared statement that has to be executed |
||
| 719 | * |
||
| 720 | * @return void |
||
| 721 | */ |
||
| 722 | 1 | public function deleteUrlRewrite($row, $name = null) |
|
| 726 | |||
| 727 | /** |
||
| 728 | * Delete's the stock item(s) with the passed attributes. |
||
| 729 | * |
||
| 730 | * @param array $row The attributes of the entity to delete |
||
| 731 | * @param string|null $name The name of the prepared statement that has to be executed |
||
| 732 | * |
||
| 733 | * @return void |
||
| 734 | */ |
||
| 735 | public function deleteStockItem($row, $name = null) |
||
| 739 | |||
| 740 | /** |
||
| 741 | * Delete's the stock status with the passed attributes. |
||
| 742 | * |
||
| 743 | * @param array $row The attributes of the entity to delete |
||
| 744 | * @param string|null $name The name of the prepared statement that has to be executed |
||
| 745 | * |
||
| 746 | * @return void |
||
| 747 | */ |
||
| 748 | public function deleteStockStatus($row, $name = null) |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Delete's the product website relations with the passed attributes. |
||
| 755 | * |
||
| 756 | * @param array $row The attributes of the entity to delete |
||
| 757 | * @param string|null $name The name of the prepared statement that has to be executed |
||
| 758 | * |
||
| 759 | * @return void |
||
| 760 | */ |
||
| 761 | public function deleteProductWebsite($row, $name = null) |
||
| 765 | |||
| 766 | /** |
||
| 767 | * Delete's the category product relations with the passed attributes. |
||
| 768 | * |
||
| 769 | * @param array $row The attributes of the entity to delete |
||
| 770 | * @param string|null $name The name of the prepared statement that has to be executed |
||
| 771 | * |
||
| 772 | * @return void |
||
| 773 | */ |
||
| 774 | public function deleteCategoryProduct($row, $name = null) |
||
| 778 | } |
||
| 779 |