Complex classes like Standard 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 Standard, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Standard |
||
| 22 | extends Base |
||
|
2 ignored issues
–
show
|
|||
| 23 | implements Iface, \Aimeos\Controller\Frontend\Common\Iface |
||
|
1 ignored issue
–
show
|
|||
| 24 | { |
||
| 25 | private $basket; |
||
| 26 | private $domainManager; |
||
| 27 | |||
| 28 | |||
| 29 | /** |
||
| 30 | * Initializes the frontend controller. |
||
| 31 | * |
||
| 32 | * @param \Aimeos\MShop\Context\Item\Iface $context Object storing the required instances for manaing databases |
||
| 33 | * connections, logger, session, etc. |
||
| 34 | */ |
||
| 35 | public function __construct( \Aimeos\MShop\Context\Item\Iface $context ) |
||
| 44 | |||
| 45 | |||
| 46 | /** |
||
| 47 | * Explicitely persists the basket content |
||
| 48 | */ |
||
| 49 | public function save() |
||
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * Empties the basket and removing all products, addresses, services, etc. |
||
| 59 | */ |
||
| 60 | public function clear() |
||
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | * Returns the basket object. |
||
| 69 | * |
||
| 70 | * @return \Aimeos\MShop\Order\Item\Base\Iface Basket holding products, addresses and delivery/payment options |
||
| 71 | */ |
||
| 72 | public function get() |
||
| 76 | |||
| 77 | |||
| 78 | /** |
||
| 79 | * Adds a categorized product to the basket of the user stored in the session. |
||
| 80 | * |
||
| 81 | * @param string $prodid ID of the base product to add |
||
| 82 | * @param integer $quantity Amount of products that should by added |
||
| 83 | * @param array $options Possible options are: 'stock'=>true|false and 'variant'=>true|false |
||
| 84 | * The 'stock'=>false option allows adding products without being in stock. |
||
| 85 | * The 'variant'=>false option allows adding the selection product to the basket |
||
| 86 | * instead of the specific sub-product if the variant-building attribute IDs |
||
| 87 | * doesn't match a specific sub-product or if the attribute IDs are missing. |
||
| 88 | * @param array $variantAttributeIds List of variant-building attribute IDs that identify a specific product |
||
| 89 | * in a selection products |
||
| 90 | * @param array $configAttributeIds List of attribute IDs that doesn't identify a specific product in a |
||
| 91 | * selection of products but are stored together with the product (e.g. for configurable products) |
||
| 92 | * @param array $hiddenAttributeIds List of attribute IDs that should be stored along with the product in the order |
||
| 93 | * @param array $customAttributeValues Associative list of attribute IDs and arbitrary values that should be stored |
||
| 94 | * along with the product in the order |
||
| 95 | * @param string $warehouse Unique code of the warehouse to deliver the products from |
||
| 96 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If the product isn't available |
||
| 97 | */ |
||
| 98 | public function addProduct( $prodid, $quantity = 1, array $options = array(), array $variantAttributeIds = array(), |
||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * Deletes a product item from the basket. |
||
| 145 | * |
||
| 146 | * @param integer $position Position number (key) of the order product item |
||
| 147 | */ |
||
| 148 | public function deleteProduct( $position ) |
||
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * Edits the quantity of a product item in the basket. |
||
| 165 | * |
||
| 166 | * @param integer $position Position number (key) of the order product item |
||
| 167 | * @param integer $quantity New quantiy of the product item |
||
| 168 | * @param array $options Possible options are: 'stock'=>true|false |
||
| 169 | * The 'stock'=>false option allows adding products without being in stock. |
||
| 170 | * @param string[] $configAttributeCodes Codes of the product config attributes that should be REMOVED |
||
| 171 | */ |
||
| 172 | public function editProduct( $position, $quantity, array $options = array(), |
||
| 202 | |||
| 203 | |||
| 204 | /** |
||
| 205 | * Adds the given coupon code and updates the basket. |
||
| 206 | * |
||
| 207 | * @param string $code Coupon code entered by the user |
||
| 208 | * @throws \Aimeos\Controller\Frontend\Basket\Exception if the coupon code is invalid or not allowed |
||
| 209 | */ |
||
| 210 | public function addCoupon( $code ) |
||
| 255 | |||
| 256 | |||
| 257 | /** |
||
| 258 | * Removes the given coupon code and its effects from the basket. |
||
| 259 | * |
||
| 260 | * @param string $code Coupon code entered by the user |
||
| 261 | * @throws \Aimeos\Controller\Frontend\Basket\Exception if the coupon code is invalid |
||
| 262 | */ |
||
| 263 | public function deleteCoupon( $code ) |
||
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * Sets the address of the customer in the basket. |
||
| 284 | * |
||
| 285 | * @param string $type Address type constant from \Aimeos\MShop\Order\Item\Base\Address\Base |
||
| 286 | * @param \Aimeos\MShop\Common\Item\Address\Iface|array|null $value Address object or array with key/value pairs of address or null to remove address from basket |
||
| 287 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If the billing or delivery address is not of any required type of |
||
| 288 | * if one of the keys is invalid when using an array with key/value pairs |
||
| 289 | */ |
||
| 290 | public function setAddress( $type, $value ) |
||
| 316 | |||
| 317 | |||
| 318 | /** |
||
| 319 | * Sets the delivery/payment service item based on the service ID. |
||
| 320 | * |
||
| 321 | * @param string $type Service type code like 'payment' or 'delivery' |
||
| 322 | * @param string $id Unique ID of the service item |
||
| 323 | * @param array $attributes Associative list of key/value pairs containing the attributes selected or |
||
| 324 | * entered by the customer when choosing one of the delivery or payment options |
||
| 325 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If there is no price to the service item attached |
||
| 326 | */ |
||
| 327 | public function setService( $type, $id, array $attributes = array() ) |
||
| 365 | |||
| 366 | |||
| 367 | /** |
||
| 368 | * Adds the bundled products to the order product item. |
||
| 369 | * |
||
| 370 | * @param \Aimeos\MShop\Order\Item\Base\Product\Iface $orderBaseProductItem Order product item |
||
| 371 | * @param \Aimeos\MShop\Product\Item\Iface $productItem Bundle product item |
||
| 372 | * @param array $variantAttributeIds List of product variant attribute IDs |
||
| 373 | * @param string $warehouse |
||
| 374 | */ |
||
| 375 | protected function addBundleProducts( \Aimeos\MShop\Order\Item\Base\Product\Iface $orderBaseProductItem, |
||
| 414 | |||
| 415 | |||
| 416 | /** |
||
| 417 | * Edits the changed product to the basket if it's in stock. |
||
| 418 | * |
||
| 419 | * @param \Aimeos\MShop\Order\Item\Base\Product\Iface $orderBaseProductItem Old order product from basket |
||
| 420 | * @param string $productId Unique ID of the product item that belongs to the order product |
||
| 421 | * @param integer $quantity Number of products to add to the basket |
||
| 422 | * @param array $options Associative list of options |
||
| 423 | * @param string $warehouse Warehouse code for retrieving the stock level |
||
| 424 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If there's not enough stock available |
||
| 425 | */ |
||
| 426 | protected function addProductInStock( \Aimeos\MShop\Order\Item\Base\Product\Iface $orderBaseProductItem, |
||
| 454 | |||
| 455 | |||
| 456 | /** |
||
| 457 | * Creates the order product attribute items from the given attribute IDs and updates the price item if necessary. |
||
| 458 | * |
||
| 459 | * @param \Aimeos\MShop\Price\Item\Iface $price Price item of the ordered product |
||
| 460 | * @param string $prodid Unique product ID where the given attributes must be attached to |
||
| 461 | * @param integer $quantity Number of products that should be added to the basket |
||
| 462 | * @param array $attributeIds List of attributes IDs of the given type |
||
| 463 | * @param string $type Attribute type |
||
| 464 | * @param array $attributeValues Associative list of attribute IDs as keys and their codes as values |
||
| 465 | * @return array List of items implementing \Aimeos\MShop\Order\Item\Product\Attribute\Iface |
||
| 466 | */ |
||
| 467 | protected function createOrderProductAttributes( \Aimeos\MShop\Price\Item\Iface $price, $prodid, $quantity, |
||
| 504 | |||
| 505 | |||
| 506 | /** |
||
| 507 | * Edits the changed product to the basket if it's in stock. |
||
| 508 | * |
||
| 509 | * @param \Aimeos\MShop\Order\Item\Base\Product\Iface $product Old order product from basket |
||
| 510 | * @param \Aimeos\MShop\Product\Item\Iface $productItem Product item that belongs to the order product |
||
| 511 | * @param integer $quantity New product quantity |
||
| 512 | * @param integer $position Position of the old order product in the basket |
||
| 513 | * @param array Associative list of options |
||
| 514 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If there's not enough stock available |
||
| 515 | */ |
||
| 516 | protected function editProductInStock( \Aimeos\MShop\Order\Item\Base\Product\Iface $product, |
||
| 538 | |||
| 539 | |||
| 540 | /** |
||
| 541 | * Retrieves the domain item specified by the given key and value. |
||
| 542 | * |
||
| 543 | * @param string $domain Product manager search key |
||
| 544 | * @param string $key Domain manager search key |
||
| 545 | * @param string $value Unique domain identifier |
||
| 546 | * @param string[] $ref List of referenced items that should be fetched too |
||
| 547 | * @return \Aimeos\MShop\Common\Item\Iface Domain item object |
||
| 548 | * @throws \Aimeos\Controller\Frontend\Basket\Exception |
||
| 549 | */ |
||
| 550 | protected function getDomainItem( $domain, $key, $value, array $ref ) |
||
| 571 | |||
| 572 | |||
| 573 | /** |
||
| 574 | * Returns the variant attributes and updates the price list if necessary. |
||
| 575 | * |
||
| 576 | * @param \Aimeos\MShop\Order\Item\Base\Product\Iface $orderBaseProductItem Order product item |
||
| 577 | * @param \Aimeos\MShop\Product\Item\Iface &$productItem Product item which is replaced if necessary |
||
| 578 | * @param array &$prices List of product prices that will be updated if necessary |
||
| 579 | * @param array $variantAttributeIds List of product variant attribute IDs |
||
| 580 | * @param array $options Associative list of options |
||
| 581 | * @return \Aimeos\MShop\Order\Item\Base\Product\Attribute\Iface[] List of order product attributes |
||
| 582 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If no product variant is found |
||
| 583 | */ |
||
| 584 | protected function getVariantDetails( \Aimeos\MShop\Order\Item\Base\Product\Iface $orderBaseProductItem, |
||
| 626 | |||
| 627 | |||
| 628 | /** |
||
| 629 | * Fills the order address object with the values from the array. |
||
| 630 | * |
||
| 631 | * @param \Aimeos\MShop\Order\Item\Base\Address\Iface $address Address item to store the values into |
||
| 632 | * @param array $map Associative array of key/value pairs. The keys must be the same as when calling toArray() from |
||
| 633 | * an address item. |
||
| 634 | * @throws \Aimeos\Controller\Frontend\Basket\Exception |
||
| 635 | */ |
||
| 636 | protected function setAddressFromArray( \Aimeos\MShop\Order\Item\Base\Address\Iface $address, array $map ) |
||
| 650 | } |
||
| 651 |