Complex classes like Base 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 Base, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | abstract class Base extends \Aimeos\Controller\Frontend\Base implements Iface |
||
22 | { |
||
23 | private $listTypeAttributes = array(); |
||
24 | |||
25 | |||
26 | /** |
||
27 | * Calculates and returns the current price for the given order product and product prices. |
||
28 | * |
||
29 | * @param \Aimeos\MShop\Order\Item\Base\Product\Iface $product Ordered product item |
||
30 | * @param \Aimeos\MShop\Price\Item\Iface[] $prices List of price items |
||
31 | * @param integer $quantity New product quantity |
||
32 | * @return \Aimeos\MShop\Price\Item\Iface Price item with calculated price |
||
33 | */ |
||
34 | protected function calcPrice( \Aimeos\MShop\Order\Item\Base\Product\Iface $product, array $prices, $quantity ) |
||
35 | { |
||
36 | $context = $this->getContext(); |
||
37 | |||
38 | if( empty( $prices ) ) |
||
39 | { |
||
40 | $manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'product' ); |
||
41 | $prices = $manager->getItem( $product->getProductId(), array( 'price' ) )->getRefItems( 'price', 'default' ); |
||
42 | } |
||
43 | |||
44 | $priceManager = \Aimeos\MShop\Factory::createManager( $context, 'price' ); |
||
45 | $price = $priceManager->getLowestPrice( $prices, $quantity ); |
||
46 | |||
47 | foreach( $this->getAttributeItems( $product->getAttributes() ) as $attrItem ) |
||
48 | { |
||
49 | $prices = $attrItem->getRefItems( 'price', 'default' ); |
||
50 | |||
51 | if( count( $prices ) > 0 ) |
||
52 | { |
||
53 | $attrPrice = $priceManager->getLowestPrice( $prices, $quantity ); |
||
54 | $price->addItem( $attrPrice ); |
||
55 | } |
||
56 | } |
||
57 | |||
58 | // remove product rebate of original price in favor to rebates granted for the order |
||
59 | $price->setRebate( '0.00' ); |
||
60 | |||
61 | return $price; |
||
62 | } |
||
63 | |||
64 | |||
65 | /** |
||
66 | * Checks if the IDs of the given items are really associated to the product. |
||
67 | * |
||
68 | * @param string $prodId Unique ID of the product |
||
69 | * @param string $domain Domain the references must be of |
||
70 | * @param integer $listTypeId ID of the list type the referenced items must be |
||
71 | * @param array $refIds List of IDs that must be associated to the product |
||
72 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If one or more of the IDs are not associated |
||
73 | */ |
||
74 | protected function checkReferences( $prodId, $domain, $listTypeId, array $refIds ) |
||
104 | |||
105 | |||
106 | /** |
||
107 | * Checks for a locale mismatch and migrates the products to the new basket if necessary. |
||
108 | */ |
||
109 | protected function checkLocale() |
||
145 | |||
146 | |||
147 | /** |
||
148 | * Migrates the addresses from the old basket to the current one. |
||
149 | * |
||
150 | * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object |
||
151 | * @param array $errors Associative list of previous errors |
||
152 | * @param string $localeKey Unique identifier of the site, language and currency |
||
153 | * @return array Associative list of errors occured |
||
154 | */ |
||
155 | protected function copyAddresses( \Aimeos\MShop\Order\Item\Base\Iface $basket, array $errors, $localeKey ) |
||
175 | |||
176 | |||
177 | /** |
||
178 | * Migrates the coupons from the old basket to the current one. |
||
179 | * |
||
180 | * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object |
||
181 | * @param array $errors Associative list of previous errors |
||
182 | * @param string $localeKey Unique identifier of the site, language and currency |
||
183 | * @return array Associative list of errors occured |
||
184 | */ |
||
185 | protected function copyCoupons( \Aimeos\MShop\Order\Item\Base\Iface $basket, array $errors, $localeKey ) |
||
205 | |||
206 | |||
207 | /** |
||
208 | * Migrates the products from the old basket to the current one. |
||
209 | * |
||
210 | * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object |
||
211 | * @param array $errors Associative list of previous errors |
||
212 | * @param string $localeKey Unique identifier of the site, language and currency |
||
213 | * @return array Associative list of errors occured |
||
214 | */ |
||
215 | protected function copyProducts( \Aimeos\MShop\Order\Item\Base\Iface $basket, array $errors, $localeKey ) |
||
256 | |||
257 | |||
258 | /** |
||
259 | * Migrates the services from the old basket to the current one. |
||
260 | * |
||
261 | * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object |
||
262 | * @param array $errors Associative list of previous errors |
||
263 | * @return array Associative list of errors occured |
||
264 | */ |
||
265 | protected function copyServices( \Aimeos\MShop\Order\Item\Base\Iface $basket, array $errors ) |
||
285 | |||
286 | |||
287 | /** |
||
288 | * Returns the attribute items for the given attribute IDs. |
||
289 | * |
||
290 | * @param array $attributeIds List of attribute IDs |
||
291 | * @param string[] $domains Names of the domain items that should be fetched too |
||
292 | * @return array List of items implementing \Aimeos\MShop\Attribute\Item\Iface |
||
293 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If the actual attribute number doesn't match the expected one |
||
294 | */ |
||
295 | protected function getAttributes( array $attributeIds, array $domains = array( 'price', 'text' ) ) |
||
324 | |||
325 | |||
326 | /** |
||
327 | * Returns the attribute items using the given order attribute items. |
||
328 | * |
||
329 | * @param \Aimeos\MShop\Order\Item\Base\Product\Attribute\Item[] $orderAttributes List of order product attribute items |
||
330 | * @return \Aimeos\MShop\Attribute\Item\Iface[] Associative list of attribute IDs as key and attribute items as values |
||
331 | */ |
||
332 | protected function getAttributeItems( array $orderAttributes ) |
||
358 | |||
359 | |||
360 | /** |
||
361 | * Returns the list type item for the given domain and code. |
||
362 | * |
||
363 | * @param string $domain Domain name of the list type |
||
364 | * @param string $code Code of the list type |
||
365 | * @return \Aimeos\MShop\Common\Item\Type\Iface List type item |
||
366 | */ |
||
367 | protected function getProductListTypeItem( $domain, $code ) |
||
394 | |||
395 | |||
396 | /** |
||
397 | * Returns the product variants of a selection product that match the given attributes. |
||
398 | * |
||
399 | * @param \Aimeos\MShop\Product\Item\Iface $productItem Product item including sub-products |
||
400 | * @param array $variantAttributeIds IDs for the variant-building attributes |
||
401 | * @param array $domains Names of the domain items that should be fetched too |
||
402 | * @return array List of products matching the given attributes |
||
403 | */ |
||
404 | protected function getProductVariants( \Aimeos\MShop\Product\Item\Iface $productItem, array $variantAttributeIds, |
||
442 | |||
443 | |||
444 | /** |
||
445 | * Returns the value of an array or the default value if it's not available. |
||
446 | * |
||
447 | * @param array $values Associative list of key/value pairs |
||
448 | * @param string $name Name of the key to return the value for |
||
449 | * @param mixed $default Default value if no value is available for the given name |
||
450 | * @return mixed Value from the array or default value |
||
451 | */ |
||
452 | protected function getValue( array $values, $name, $default = null ) |
||
460 | |||
461 | |||
462 | /** |
||
463 | * Checks if the product is part of at least one category in the product catalog. |
||
464 | * |
||
465 | * @param string $prodid Unique ID of the product |
||
466 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If product is not associated to at least one category |
||
467 | * @deprecated 2016.05 |
||
468 | */ |
||
469 | protected function checkCategory( $prodid ) |
||
489 | |||
490 | |||
491 | /** |
||
492 | * Returns the highest stock level for the product. |
||
493 | * |
||
494 | * @param string $prodid Unique ID of the product |
||
495 | * @param string $warehouse Unique code of the warehouse |
||
496 | * @return integer|null Number of available items in stock (null for unlimited stock) |
||
497 | */ |
||
498 | protected function getStockLevel( $prodid, $warehouse ) |
||
531 | } |
||
532 |