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 implements Iface |
||
22 | { |
||
23 | private $context; |
||
24 | private $object; |
||
25 | private $item; |
||
26 | private $code = ''; |
||
27 | |||
28 | /** |
||
29 | * Initializes the coupon model. |
||
30 | * |
||
31 | * @param \Aimeos\MShop\Context\Item\Iface $context Context object |
||
32 | * @param \Aimeos\MShop\Coupon\Item\Iface $item Coupon item to set |
||
33 | * @param string $code Coupon code entered by the customer |
||
34 | */ |
||
35 | public function __construct( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Coupon\Item\Iface $item, $code ) |
||
41 | |||
42 | |||
43 | /** |
||
44 | * Updates the result of a coupon to the order base instance. |
||
45 | * |
||
46 | * @param \Aimeos\MShop\Order\Item\Base\Iface $base Basic order of the customer |
||
47 | */ |
||
48 | public function updateCoupon( \Aimeos\MShop\Order\Item\Base\Iface $base ) |
||
59 | |||
60 | |||
61 | /** |
||
62 | * Removes the result of a coupon from the order base instance. |
||
63 | * |
||
64 | * @param \Aimeos\MShop\Order\Item\Base\Iface $base Basic order of the customer |
||
65 | */ |
||
66 | public function deleteCoupon( \Aimeos\MShop\Order\Item\Base\Iface $base ) |
||
70 | |||
71 | /** |
||
72 | * Checks the backend configuration attributes for validity. |
||
73 | * |
||
74 | * @param array $attributes Attributes added by the shop owner in the administraton interface |
||
75 | * @return array An array with the attribute keys as key and an error message as values for all attributes that are |
||
76 | * known by the provider but aren't valid resp. null for attributes whose values are OK |
||
77 | */ |
||
78 | public function checkConfigBE( array $attributes ) |
||
82 | |||
83 | |||
84 | /** |
||
85 | * Returns the configuration attribute definitions of the provider to generate a list of available fields and |
||
86 | * rules for the value of each field in the administration interface. |
||
87 | * |
||
88 | * @return array List of attribute definitions implementing \Aimeos\MW\Common\Critera\Attribute\Iface |
||
89 | */ |
||
90 | public function getConfigBE() |
||
94 | |||
95 | |||
96 | /** |
||
97 | * Tests if a coupon should be granted |
||
98 | * |
||
99 | * @param \Aimeos\MShop\Order\Item\Base\Iface $base |
||
100 | */ |
||
101 | public function isAvailable( \Aimeos\MShop\Order\Item\Base\Iface $base ) |
||
105 | |||
106 | |||
107 | /** |
||
108 | * Injects the reference of the outmost object |
||
109 | * |
||
110 | * @param \Aimeos\MShop\Coupon\Provider\Iface $object Reference to the outmost provider or decorator |
||
111 | * @return \Aimeos\MShop\Coupon\Provider\Iface Coupon object for chaining method calls |
||
112 | */ |
||
113 | public function setObject( \Aimeos\MShop\Coupon\Provider\Iface $object ) |
||
118 | |||
119 | |||
120 | /** |
||
121 | * Checks required fields and the types of the config array. |
||
122 | * |
||
123 | * @param array $config Config parameters |
||
124 | * @param array $attributes Attributes for the config array |
||
125 | * @return array An array with the attribute keys as key and an error message as values for all attributes that are |
||
126 | * known by the provider but aren't valid resp. null for attributes whose values are OK |
||
127 | */ |
||
128 | protected function checkConfig( array $config, array $attributes ) |
||
202 | |||
203 | |||
204 | /** |
||
205 | * Returns the coupon code the provider is responsible for. |
||
206 | * |
||
207 | * @return string Coupon code |
||
208 | */ |
||
209 | protected function getCode() |
||
213 | |||
214 | |||
215 | /** |
||
216 | * Returns the criteria attribute items for the backend configuration |
||
217 | * |
||
218 | * @return \Aimeos\MW\Criteria\Attribute\Iface[] List of criteria attribute items |
||
219 | */ |
||
220 | protected function getConfigItems( array $configList ) |
||
230 | |||
231 | |||
232 | /** |
||
233 | * Returns the configuration value from the service item specified by its key. |
||
234 | * |
||
235 | * @param string $key Configuration key |
||
236 | * @param mixed $default Default value if configuration key isn't available |
||
237 | * @return mixed Value from service item configuration |
||
238 | */ |
||
239 | protected function getConfigValue( $key, $default = null ) |
||
249 | |||
250 | |||
251 | /** |
||
252 | * Returns the stored context object. |
||
253 | * |
||
254 | * @return \Aimeos\MShop\Context\Item\Iface Context object |
||
255 | */ |
||
256 | protected function getContext() |
||
260 | |||
261 | |||
262 | /** |
||
263 | * Returns the stored coupon item. |
||
264 | * |
||
265 | * @return \Aimeos\MShop\Coupon\Item\Iface Coupon item |
||
266 | */ |
||
267 | protected function getItemBase() |
||
271 | |||
272 | |||
273 | /** |
||
274 | * Returns the outmost decorator of the decorator stack |
||
275 | * |
||
276 | * @return \Aimeos\MShop\Coupon\Provider\Iface Outmost decorator object |
||
277 | */ |
||
278 | protected function getObject() |
||
286 | |||
287 | |||
288 | /** |
||
289 | * Creates an order product from the product item. |
||
290 | * |
||
291 | * @param string $productCode Unique product code |
||
292 | * @param integer $quantity Number of products in basket |
||
293 | * @param string $stockType Unique code of the stock type the product is from |
||
294 | * @return \Aimeos\MShop\Order\Item\Base\Product\Iface Ordered product |
||
295 | */ |
||
296 | protected function createProduct( $productCode, $quantity = 1, $stockType = 'default' ) |
||
327 | |||
328 | |||
329 | /** |
||
330 | * Creates the order products for monetary rebates. |
||
331 | * |
||
332 | * @param \Aimeos\MShop\Order\Item\Base\Iface Basket object |
||
333 | * @param string $productCode Unique product code |
||
334 | * @param float $rebate Rebate amount that should be granted |
||
335 | * @param integer $quantity Number of products in basket |
||
336 | * @param string $stockType Unique code of the stock type the product is from |
||
337 | * @return \Aimeos\MShop\Order\Item\Base\Product\Iface[] Order products with monetary rebates |
||
338 | */ |
||
339 | protected function createMonetaryRebateProducts( \Aimeos\MShop\Order\Item\Base\Iface $base, |
||
384 | |||
385 | |||
386 | /** |
||
387 | * Returns a list of tax rates and their price items for the given basket. |
||
388 | * |
||
389 | * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket containing the products, services, etc. |
||
390 | * @return array Associative list of tax rates as key and corresponding price items as value |
||
391 | */ |
||
392 | protected function getPriceByTaxRate( \Aimeos\MShop\Order\Item\Base\Iface $basket ) |
||
437 | } |
||
438 |