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 extends Base implements Iface, \Aimeos\Controller\Frontend\Common\Iface |
||
| 22 | { |
||
| 23 | private $basket; |
||
| 24 | private $domainManager; |
||
| 25 | |||
| 26 | |||
| 27 | /** |
||
| 28 | * Initializes the frontend controller. |
||
| 29 | * |
||
| 30 | * @param \Aimeos\MShop\Context\Item\Iface $context Object storing the required instances for manaing databases |
||
| 31 | * connections, logger, session, etc. |
||
| 32 | */ |
||
| 33 | public function __construct( \Aimeos\MShop\Context\Item\Iface $context ) |
||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * Empties the basket and removing all products, addresses, services, etc. |
||
| 46 | */ |
||
| 47 | public function clear() |
||
| 52 | |||
| 53 | |||
| 54 | /** |
||
| 55 | * Returns the basket object. |
||
| 56 | * |
||
| 57 | * @return \Aimeos\MShop\Order\Item\Base\Iface Basket holding products, addresses and delivery/payment options |
||
| 58 | */ |
||
| 59 | public function get() |
||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * Adds a categorized product to the basket of the user stored in the session. |
||
| 67 | * |
||
| 68 | * @param string $prodid ID of the base product to add |
||
| 69 | * @param integer $quantity Amount of products that should by added |
||
| 70 | * @param array $options Possible options are: 'stock'=>true|false and 'variant'=>true|false |
||
| 71 | * The 'stock'=>false option allows adding products without being in stock. |
||
| 72 | * The 'variant'=>false option allows adding the selection product to the basket |
||
| 73 | * instead of the specific sub-product if the variant-building attribute IDs |
||
| 74 | * doesn't match a specific sub-product or if the attribute IDs are missing. |
||
| 75 | * @param array $variantAttributeIds List of variant-building attribute IDs that identify a specific product |
||
| 76 | * in a selection products |
||
| 77 | * @param array $configAttributeIds List of attribute IDs that doesn't identify a specific product in a |
||
| 78 | * selection of products but are stored together with the product (e.g. for configurable products) |
||
| 79 | * @param array $hiddenAttributeIds List of attribute IDs that should be stored along with the product in the order |
||
| 80 | * @param array $customAttributeValues Associative list of attribute IDs and arbitrary values that should be stored |
||
| 81 | * along with the product in the order |
||
| 82 | * @param string $warehouse Unique code of the warehouse to deliver the products from |
||
| 83 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If the product isn't available |
||
| 84 | */ |
||
| 85 | public function addProduct( $prodid, $quantity = 1, array $options = array(), array $variantAttributeIds = array(), |
||
| 86 | array $configAttributeIds = array(), array $hiddenAttributeIds = array(), array $customAttributeValues = array(), |
||
| 87 | $warehouse = 'default' ) |
||
| 88 | { |
||
| 89 | $context = $this->getContext(); |
||
| 90 | |||
| 91 | $productItem = $this->getDomainItem( 'product', 'product.id', $prodid, array( 'media', 'supplier', 'price', 'product', 'text' ) ); |
||
| 92 | |||
| 93 | $orderBaseProductItem = \Aimeos\MShop\Factory::createManager( $context, 'order/base/product' )->createItem(); |
||
| 94 | $orderBaseProductItem->copyFrom( $productItem ); |
||
| 95 | $orderBaseProductItem->setQuantity( $quantity ); |
||
| 96 | $orderBaseProductItem->setWarehouseCode( $warehouse ); |
||
| 97 | |||
| 98 | $attr = array(); |
||
| 99 | $prices = $productItem->getRefItems( 'price', 'default', 'default' ); |
||
| 100 | |||
| 101 | switch( $productItem->getType() ) |
||
| 102 | { |
||
| 103 | case 'select': |
||
| 104 | $attr = $this->getVariantDetails( $orderBaseProductItem, $productItem, $prices, $variantAttributeIds, $options ); |
||
| 105 | break; |
||
| 106 | case 'bundle': |
||
| 107 | $this->addBundleProducts( $orderBaseProductItem, $productItem, $variantAttributeIds, $warehouse ); |
||
| 108 | break; |
||
| 109 | } |
||
| 110 | |||
| 111 | $priceManager = \Aimeos\MShop\Factory::createManager( $context, 'price' ); |
||
| 112 | $price = $priceManager->getLowestPrice( $prices, $quantity ); |
||
| 113 | |||
| 114 | $attr = array_merge( $attr, $this->createOrderProductAttributes( $price, $prodid, $quantity, $configAttributeIds, 'config' ) ); |
||
| 115 | $attr = array_merge( $attr, $this->createOrderProductAttributes( $price, $prodid, $quantity, $hiddenAttributeIds, 'hidden' ) ); |
||
| 116 | $attr = array_merge( $attr, $this->createOrderProductAttributes( $price, $prodid, $quantity, array_keys( $customAttributeValues ), 'custom', $customAttributeValues ) ); |
||
| 117 | |||
| 118 | // remove product rebate of original price in favor to rebates granted for the order |
||
| 119 | $price->setRebate( '0.00' ); |
||
| 120 | |||
| 121 | $orderBaseProductItem->setPrice( $price ); |
||
| 122 | $orderBaseProductItem->setAttributes( $attr ); |
||
| 123 | |||
| 124 | $this->addProductInStock( $orderBaseProductItem, $productItem->getId(), $quantity, $options, $warehouse ); |
||
| 125 | |||
| 126 | $this->domainManager->setSession( $this->basket ); |
||
| 127 | } |
||
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * Deletes a product item from the basket. |
||
| 132 | * |
||
| 133 | * @param integer $position Position number (key) of the order product item |
||
| 134 | */ |
||
| 135 | public function deleteProduct( $position ) |
||
| 136 | { |
||
| 137 | $product = $this->basket->getProduct( $position ); |
||
| 138 | |||
| 139 | if( $product->getFlags() === \Aimeos\MShop\Order\Item\Base\Product\Base::FLAG_IMMUTABLE ) |
||
| 140 | { |
||
| 141 | $msg = sprintf( 'Basket item at position "%1$d" cannot be deleted manually', $position ); |
||
| 142 | throw new \Aimeos\Controller\Frontend\Basket\Exception( $msg ); |
||
| 143 | } |
||
| 144 | |||
| 145 | $this->basket->deleteProduct( $position ); |
||
| 146 | $this->domainManager->setSession( $this->basket ); |
||
| 147 | } |
||
| 148 | |||
| 149 | |||
| 150 | /** |
||
| 151 | * Edits the quantity of a product item in the basket. |
||
| 152 | * |
||
| 153 | * @param integer $position Position number (key) of the order product item |
||
| 154 | * @param integer $quantity New quantiy of the product item |
||
| 155 | * @param array $options Possible options are: 'stock'=>true|false |
||
| 156 | * The 'stock'=>false option allows adding products without being in stock. |
||
| 157 | * @param string[] $configAttributeCodes Codes of the product config attributes that should be REMOVED |
||
| 158 | */ |
||
| 159 | public function editProduct( $position, $quantity, array $options = array(), |
||
| 160 | array $configAttributeCodes = array() ) |
||
| 161 | { |
||
| 162 | $product = $this->basket->getProduct( $position ); |
||
| 163 | $product->setQuantity( $quantity ); // Enforce check immediately |
||
| 164 | |||
| 165 | if( $product->getFlags() & \Aimeos\MShop\Order\Item\Base\Product\Base::FLAG_IMMUTABLE ) |
||
| 166 | { |
||
| 167 | $msg = sprintf( 'Basket item at position "%1$d" cannot be changed', $position ); |
||
| 168 | throw new \Aimeos\Controller\Frontend\Basket\Exception( $msg ); |
||
| 169 | } |
||
| 170 | |||
| 171 | $attributes = $product->getAttributes(); |
||
| 172 | foreach( $attributes as $key => $attribute ) |
||
| 173 | { |
||
| 174 | if( in_array( $attribute->getCode(), $configAttributeCodes ) ) { |
||
| 175 | unset( $attributes[$key] ); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | $product->setAttributes( $attributes ); |
||
| 179 | |||
| 180 | $productItem = $this->getDomainItem( 'product', 'product.code', $product->getProductCode(), array( 'price', 'text' ) ); |
||
| 181 | $prices = $productItem->getRefItems( 'price', 'default' ); |
||
| 182 | |||
| 183 | $product->setPrice( $this->calcPrice( $product, $prices, $quantity ) ); |
||
| 184 | |||
| 185 | $this->editProductInStock( $product, $productItem, $quantity, $position, $options ); |
||
| 186 | |||
| 187 | $this->domainManager->setSession( $this->basket ); |
||
| 188 | } |
||
| 189 | |||
| 190 | |||
| 191 | /** |
||
| 192 | * Adds the given coupon code and updates the basket. |
||
| 193 | * |
||
| 194 | * @param string $code Coupon code entered by the user |
||
| 195 | * @throws \Aimeos\Controller\Frontend\Basket\Exception if the coupon code is invalid or not allowed |
||
| 196 | */ |
||
| 197 | public function addCoupon( $code ) |
||
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * Removes the given coupon code and its effects from the basket. |
||
| 246 | * |
||
| 247 | * @param string $code Coupon code entered by the user |
||
| 248 | * @throws \Aimeos\Controller\Frontend\Basket\Exception if the coupon code is invalid |
||
| 249 | */ |
||
| 250 | public function deleteCoupon( $code ) |
||
| 267 | |||
| 268 | |||
| 269 | /** |
||
| 270 | * Sets the address of the customer in the basket. |
||
| 271 | * |
||
| 272 | * @param string $type Address type constant from \Aimeos\MShop\Order\Item\Base\Address\Base |
||
| 273 | * @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 |
||
| 274 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If the billing or delivery address is not of any required type of |
||
| 275 | * if one of the keys is invalid when using an array with key/value pairs |
||
| 276 | */ |
||
| 277 | public function setAddress( $type, $value ) |
||
| 303 | |||
| 304 | |||
| 305 | /** |
||
| 306 | * Sets the delivery/payment service item based on the service ID. |
||
| 307 | * |
||
| 308 | * @param string $type Service type code like 'payment' or 'delivery' |
||
| 309 | * @param string $id Unique ID of the service item |
||
| 310 | * @param array $attributes Associative list of key/value pairs containing the attributes selected or |
||
| 311 | * entered by the customer when choosing one of the delivery or payment options |
||
| 312 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If there is no price to the service item attached |
||
| 313 | */ |
||
| 314 | public function setService( $type, $id, array $attributes = array() ) |
||
| 352 | |||
| 353 | |||
| 354 | /** |
||
| 355 | * Adds the bundled products to the order product item. |
||
| 356 | * |
||
| 357 | * @param \Aimeos\MShop\Order\Item\Base\Product\Iface $orderBaseProductItem Order product item |
||
| 358 | * @param \Aimeos\MShop\Product\Item\Iface $productItem Bundle product item |
||
| 359 | * @param array $variantAttributeIds List of product variant attribute IDs |
||
| 360 | * @param string $warehouse |
||
| 361 | */ |
||
| 362 | protected function addBundleProducts( \Aimeos\MShop\Order\Item\Base\Product\Iface $orderBaseProductItem, |
||
| 401 | |||
| 402 | |||
| 403 | /** |
||
| 404 | * Edits the changed product to the basket if it's in stock. |
||
| 405 | * |
||
| 406 | * @param \Aimeos\MShop\Order\Item\Base\Product\Iface $orderBaseProductItem Old order product from basket |
||
| 407 | * @param string $productId Unique ID of the product item that belongs to the order product |
||
| 408 | * @param integer $quantity Number of products to add to the basket |
||
| 409 | * @param array $options Associative list of options |
||
| 410 | * @param string $warehouse Warehouse code for retrieving the stock level |
||
| 411 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If there's not enough stock available |
||
| 412 | */ |
||
| 413 | protected function addProductInStock( \Aimeos\MShop\Order\Item\Base\Product\Iface $orderBaseProductItem, |
||
| 441 | |||
| 442 | |||
| 443 | /** |
||
| 444 | * Creates the order product attribute items from the given attribute IDs and updates the price item if necessary. |
||
| 445 | * |
||
| 446 | * @param \Aimeos\MShop\Price\Item\Iface $price Price item of the ordered product |
||
| 447 | * @param string $prodid Unique product ID where the given attributes must be attached to |
||
| 448 | * @param integer $quantity Number of products that should be added to the basket |
||
| 449 | * @param array $attributeIds List of attributes IDs of the given type |
||
| 450 | * @param string $type Attribute type |
||
| 451 | * @param array $attributeValues Associative list of attribute IDs as keys and their codes as values |
||
| 452 | * @return array List of items implementing \Aimeos\MShop\Order\Item\Product\Attribute\Iface |
||
| 453 | */ |
||
| 454 | protected function createOrderProductAttributes( \Aimeos\MShop\Price\Item\Iface $price, $prodid, $quantity, |
||
| 491 | |||
| 492 | |||
| 493 | /** |
||
| 494 | * Edits the changed product to the basket if it's in stock. |
||
| 495 | * |
||
| 496 | * @param \Aimeos\MShop\Order\Item\Base\Product\Iface $product Old order product from basket |
||
| 497 | * @param \Aimeos\MShop\Product\Item\Iface $productItem Product item that belongs to the order product |
||
| 498 | * @param integer $quantity New product quantity |
||
| 499 | * @param integer $position Position of the old order product in the basket |
||
| 500 | * @param array Associative list of options |
||
| 501 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If there's not enough stock available |
||
| 502 | */ |
||
| 503 | protected function editProductInStock( \Aimeos\MShop\Order\Item\Base\Product\Iface $product, |
||
| 525 | |||
| 526 | |||
| 527 | /** |
||
| 528 | * Retrieves the domain item specified by the given key and value. |
||
| 529 | * |
||
| 530 | * @param string $domain Product manager search key |
||
| 531 | * @param string $key Domain manager search key |
||
| 532 | * @param string $value Unique domain identifier |
||
| 533 | * @param string[] $ref List of referenced items that should be fetched too |
||
| 534 | * @return \Aimeos\MShop\Common\Item\Iface Domain item object |
||
| 535 | * @throws \Aimeos\Controller\Frontend\Basket\Exception |
||
| 536 | */ |
||
| 537 | protected function getDomainItem( $domain, $key, $value, array $ref ) |
||
| 558 | |||
| 559 | |||
| 560 | /** |
||
| 561 | * Returns the variant attributes and updates the price list if necessary. |
||
| 562 | * |
||
| 563 | * @param \Aimeos\MShop\Order\Item\Base\Product\Iface $orderBaseProductItem Order product item |
||
| 564 | * @param \Aimeos\MShop\Product\Item\Iface &$productItem Product item which is replaced if necessary |
||
| 565 | * @param array &$prices List of product prices that will be updated if necessary |
||
| 566 | * @param array $variantAttributeIds List of product variant attribute IDs |
||
| 567 | * @param array $options Associative list of options |
||
| 568 | * @return \Aimeos\MShop\Order\Item\Base\Product\Attribute\Iface[] List of order product attributes |
||
| 569 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If no product variant is found |
||
| 570 | */ |
||
| 571 | protected function getVariantDetails( \Aimeos\MShop\Order\Item\Base\Product\Iface $orderBaseProductItem, |
||
| 613 | |||
| 614 | |||
| 615 | /** |
||
| 616 | * Fills the order address object with the values from the array. |
||
| 617 | * |
||
| 618 | * @param \Aimeos\MShop\Order\Item\Base\Address\Iface $address Address item to store the values into |
||
| 619 | * @param array $map Associative array of key/value pairs. The keys must be the same as when calling toArray() from |
||
| 620 | * an address item. |
||
| 621 | * @throws \Aimeos\Controller\Frontend\Basket\Exception |
||
| 622 | */ |
||
| 623 | protected function setAddressFromArray( \Aimeos\MShop\Order\Item\Base\Address\Iface $address, array $map ) |
||
| 637 | } |
||
| 638 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: