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 ) |
||
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 | * Creates the order product attribute items from the given attribute IDs and updates the price item if necessary. |
||
289 | * |
||
290 | * @param \Aimeos\MShop\Price\Item\Iface $price Price item of the ordered product |
||
291 | * @param string $prodid Unique product ID where the given attributes must be attached to |
||
292 | * @param integer $quantity Number of products that should be added to the basket |
||
293 | * @param array $attributeIds List of attributes IDs of the given type |
||
294 | * @param string $type Attribute type |
||
295 | * @param array $attributeValues Associative list of attribute IDs as keys and their codes as values |
||
296 | * @return array List of items implementing \Aimeos\MShop\Order\Item\Product\Attribute\Iface |
||
297 | */ |
||
298 | protected function createOrderProductAttributes( \Aimeos\MShop\Price\Item\Iface $price, $prodid, $quantity, |
||
335 | |||
336 | |||
337 | /** |
||
338 | * Returns the attribute items for the given attribute IDs. |
||
339 | * |
||
340 | * @param array $attributeIds List of attribute IDs |
||
341 | * @param string[] $domains Names of the domain items that should be fetched too |
||
342 | * @return array List of items implementing \Aimeos\MShop\Attribute\Item\Iface |
||
343 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If the actual attribute number doesn't match the expected one |
||
344 | */ |
||
345 | protected function getAttributes( array $attributeIds, array $domains = array( 'price', 'text' ) ) |
||
374 | |||
375 | |||
376 | /** |
||
377 | * Returns the attribute items using the given order attribute items. |
||
378 | * |
||
379 | * @param \Aimeos\MShop\Order\Item\Base\Product\Attribute\Item[] $orderAttributes List of order product attribute items |
||
380 | * @return \Aimeos\MShop\Attribute\Item\Iface[] Associative list of attribute IDs as key and attribute items as values |
||
381 | */ |
||
382 | protected function getAttributeItems( array $orderAttributes ) |
||
408 | |||
409 | |||
410 | /** |
||
411 | * Retrieves the domain item specified by the given key and value. |
||
412 | * |
||
413 | * @param string $domain Product manager search key |
||
414 | * @param string $key Domain manager search key |
||
415 | * @param string $value Unique domain identifier |
||
416 | * @param string[] $ref List of referenced items that should be fetched too |
||
417 | * @return \Aimeos\MShop\Common\Item\Iface Domain item object |
||
418 | * @throws \Aimeos\Controller\Frontend\Basket\Exception |
||
419 | */ |
||
420 | protected function getDomainItem( $domain, $key, $value, array $ref ) |
||
441 | |||
442 | |||
443 | /** |
||
444 | * Returns the list type item for the given domain and code. |
||
445 | * |
||
446 | * @param string $domain Domain name of the list type |
||
447 | * @param string $code Code of the list type |
||
448 | * @return \Aimeos\MShop\Common\Item\Type\Iface List type item |
||
449 | */ |
||
450 | protected function getProductListTypeItem( $domain, $code ) |
||
477 | |||
478 | |||
479 | /** |
||
480 | * Returns the product variants of a selection product that match the given attributes. |
||
481 | * |
||
482 | * @param \Aimeos\MShop\Product\Item\Iface $productItem Product item including sub-products |
||
483 | * @param array $variantAttributeIds IDs for the variant-building attributes |
||
484 | * @param array $domains Names of the domain items that should be fetched too |
||
485 | * @return array List of products matching the given attributes |
||
486 | */ |
||
487 | protected function getProductVariants( \Aimeos\MShop\Product\Item\Iface $productItem, array $variantAttributeIds, |
||
525 | |||
526 | |||
527 | /** |
||
528 | * Returns the value of an array or the default value if it's not available. |
||
529 | * |
||
530 | * @param array $values Associative list of key/value pairs |
||
531 | * @param string $name Name of the key to return the value for |
||
532 | * @param mixed $default Default value if no value is available for the given name |
||
533 | * @return mixed Value from the array or default value |
||
534 | */ |
||
535 | protected function getValue( array $values, $name, $default = null ) |
||
543 | } |
||
544 |