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 |
||
22 | { |
||
23 | private $context; |
||
24 | private $serviceItem; |
||
25 | private $communication; |
||
26 | private $beGlobalConfig; |
||
27 | |||
28 | |||
29 | /** |
||
30 | * Initializes the service provider object. |
||
31 | * |
||
32 | * @param \Aimeos\MShop\Context\Item\Iface $context Context object with required objects |
||
33 | * @param \Aimeos\MShop\Service\Item\Iface $serviceItem Service item with configuration for the provider |
||
34 | */ |
||
35 | public function __construct( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Service\Item\Iface $serviceItem ) |
||
40 | |||
41 | |||
42 | /** |
||
43 | * Catch unknown methods |
||
44 | * |
||
45 | * @param string $name Name of the method |
||
46 | * @param array $param List of method parameter |
||
47 | * @throws \Aimeos\MShop\Common\Manager\Exception If method call failed |
||
48 | */ |
||
49 | public function __call( $name, array $param ) |
||
53 | |||
54 | |||
55 | /** |
||
56 | * Returns the price when using the provider. |
||
57 | * Usually, this is the lowest price that is available in the service item but can also be a calculated based on |
||
58 | * the basket content, e.g. 2% of the value as transaction cost. |
||
59 | * |
||
60 | * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object |
||
61 | * @return \Aimeos\MShop\Price\Item\Iface Price item containing the price, shipping, rebate |
||
62 | */ |
||
63 | public function calcPrice( \Aimeos\MShop\Order\Item\Base\Iface $basket ) |
||
74 | |||
75 | |||
76 | /** |
||
77 | * Checks the backend configuration attributes for validity. |
||
78 | * |
||
79 | * @param array $attributes Attributes added by the shop owner in the administraton interface |
||
80 | * @return array An array with the attribute keys as key and an error message as values for all attributes that are |
||
81 | * known by the provider but aren't valid resp. null for attributes whose values are OK |
||
82 | */ |
||
83 | public function checkConfigBE( array $attributes ) |
||
87 | |||
88 | |||
89 | /** |
||
90 | * Checks the frontend configuration attributes for validity. |
||
91 | * |
||
92 | * @param array $attributes Attributes entered by the customer during the checkout process |
||
93 | * @return array An array with the attribute keys as key and an error message as values for all attributes that are |
||
94 | * known by the provider but aren't valid resp. null for attributes whose values are OK |
||
95 | */ |
||
96 | public function checkConfigFE( array $attributes ) |
||
100 | |||
101 | |||
102 | /** |
||
103 | * Returns the configuration attribute definitions of the provider to generate a list of available fields and |
||
104 | * rules for the value of each field in the administration interface. |
||
105 | * |
||
106 | * @return array List of attribute definitions implementing \Aimeos\MW\Common\Critera\Attribute\Iface |
||
107 | */ |
||
108 | public function getConfigBE() |
||
112 | |||
113 | |||
114 | /** |
||
115 | * Returns the configuration attribute definitions of the provider to generate a list of available fields and |
||
116 | * rules for the value of each field in the frontend. |
||
117 | * |
||
118 | * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object |
||
119 | * @return array List of attribute definitions implementing \Aimeos\MW\Common\Critera\Attribute\Iface |
||
120 | */ |
||
121 | public function getConfigFE( \Aimeos\MShop\Order\Item\Base\Iface $basket ) |
||
125 | |||
126 | |||
127 | /** |
||
128 | * Returns the service item which also includes the configuration for the service provider. |
||
129 | * |
||
130 | * @return \Aimeos\MShop\Service\Item\Iface Service item |
||
131 | */ |
||
132 | public function getServiceItem() |
||
136 | |||
137 | |||
138 | /** |
||
139 | * Injects additional global configuration for the backend. |
||
140 | * |
||
141 | * It's used for adding additional backend configuration from the application |
||
142 | * like the URLs to redirect to. |
||
143 | * |
||
144 | * Supported redirect URLs are: |
||
145 | * - payment.url-success |
||
146 | * - payment.url-failure |
||
147 | * - payment.url-cancel |
||
148 | * - payment.url-update |
||
149 | * |
||
150 | * @param array $config Associative list of config keys and their value |
||
151 | */ |
||
152 | public function injectGlobalConfigBE( array $config ) |
||
156 | |||
157 | |||
158 | /** |
||
159 | * Checks if payment provider can be used based on the basket content. |
||
160 | * Checks for country, currency, address, RMS, etc. -> in separate decorators |
||
161 | * |
||
162 | * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object |
||
163 | * @return boolean True if payment provider can be used, false if not |
||
164 | */ |
||
165 | public function isAvailable( \Aimeos\MShop\Order\Item\Base\Iface $basket ) |
||
169 | |||
170 | |||
171 | /** |
||
172 | * Checks what features the payment provider implements. |
||
173 | * |
||
174 | * @param integer $what Constant from abstract class |
||
175 | * @return boolean True if feature is available in the payment provider, false if not |
||
176 | */ |
||
177 | public function isImplemented( $what ) |
||
181 | |||
182 | |||
183 | /** |
||
184 | * Queries for status updates for the given order if supported. |
||
185 | * |
||
186 | * @param \Aimeos\MShop\Order\Item\Iface $order Order invoice object |
||
187 | */ |
||
188 | public function query( \Aimeos\MShop\Order\Item\Iface $order ) |
||
192 | |||
193 | |||
194 | /** |
||
195 | * Looks for new update files and updates the orders for which status updates were received. |
||
196 | * If batch processing of files isn't supported, this method can be empty. |
||
197 | * |
||
198 | * @return boolean True if the update was successful, false if async updates are not supported |
||
199 | * @throws \Aimeos\MShop\Service\Exception If updating one of the orders failed |
||
200 | */ |
||
201 | public function updateAsync() |
||
205 | |||
206 | |||
207 | /** |
||
208 | * Updates the orders for which status updates were received via direct requests (like HTTP). |
||
209 | * |
||
210 | * @param array $params Associative list of request parameters |
||
211 | * @param string|null $body Information sent within the body of the request |
||
212 | * @param string|null &$response Response body for notification requests |
||
213 | * @param array &$header Response headers for notification requests |
||
214 | * @return \Aimeos\MShop\Order\Item\Iface|null Order item if update was successful, null if the given parameters are not valid for this provider |
||
215 | * @throws \Aimeos\MShop\Service\Exception If updating one of the orders failed |
||
216 | */ |
||
217 | public function updateSync( array $params = [], $body = null, &$response = null, array &$header = [] ) |
||
225 | |||
226 | |||
227 | /** |
||
228 | * Sets the communication object for a service provider. |
||
229 | * |
||
230 | * @param \Aimeos\MW\Communication\Iface $communication Object of communication |
||
231 | */ |
||
232 | public function setCommunication( \Aimeos\MW\Communication\Iface $communication ) |
||
236 | |||
237 | |||
238 | /** |
||
239 | * Returns the communication object for the service provider. |
||
240 | * |
||
241 | * @return \Aimeos\MW\Communication\Iface Object for communication |
||
242 | */ |
||
243 | protected function getCommunication() |
||
251 | |||
252 | |||
253 | /** |
||
254 | * Calculates the last date behind the given timestamp depending on the other paramters. |
||
255 | * |
||
256 | * This method is used to calculate the date for comparing the order date to |
||
257 | * if e.g. credit card payments should be captured or direct debit should be |
||
258 | * checked after the given amount of days from external payment providers. |
||
259 | * This method can calculate with business/working days only if requested |
||
260 | * and use the given list of public holidays to take them into account. |
||
261 | * |
||
262 | * @param integer $timestamp Timestamp to use as starting point for the backward calculation |
||
263 | * @param integer $skipdays Number of days to calculate backwards |
||
264 | * @param boolean $businessOnly True if only business days should be used for calculation, false if not |
||
265 | * @param string $publicHolidays Comma separated list of public holidays in YYYY-MM-DD format |
||
266 | * @return string Date in YYY-MM-DD format to be compared to the order date |
||
267 | * @throws \Aimeos\MShop\Service\Exception If the given holiday string is in the wrong format and can't be processed |
||
268 | */ |
||
269 | protected function calcDateLimit( $timestamp, $skipdays = 0, $businessOnly = false, $publicHolidays = '' ) |
||
300 | |||
301 | |||
302 | /** |
||
303 | * Checks required fields and the types of the config array. |
||
304 | * |
||
305 | * @param array $config Config parameters |
||
306 | * @param array $attributes Attributes for the config array |
||
307 | * @return array An array with the attribute keys as key and an error message as values for all attributes that are |
||
308 | * known by the provider but aren't valid resp. null for attributes whose values are OK |
||
309 | */ |
||
310 | protected function checkConfig( array $config, array $attributes ) |
||
387 | |||
388 | |||
389 | /** |
||
390 | * Returns the criteria attribute items for the backend configuration |
||
391 | * |
||
392 | * @return \Aimeos\MW\Criteria\Attribute\Iface[] List of criteria attribute items |
||
393 | */ |
||
394 | protected function getConfigItems( array $configList ) |
||
404 | |||
405 | |||
406 | /** |
||
407 | * Returns the configuration value that matches one of the given keys. |
||
408 | * |
||
409 | * The config of the service item and (optionally) the global config |
||
410 | * is tested in the order of the keys. The first one that matches will |
||
411 | * be returned. |
||
412 | * |
||
413 | * @param array|string $keys Key name or list of key names that should be tested for in the order to test |
||
414 | * @param mixed $default Returned value if the key wasn't was found |
||
415 | * @return mixed Value of the first key that matches or null if none was found |
||
416 | */ |
||
417 | protected function getConfigValue( $keys, $default = null ) |
||
434 | |||
435 | |||
436 | /** |
||
437 | * Returns the context item. |
||
438 | * |
||
439 | * @return \Aimeos\MShop\Context\Item\Iface Context item |
||
440 | */ |
||
441 | protected function getContext() |
||
445 | |||
446 | |||
447 | /** |
||
448 | * Returns the calculated amount of the price item |
||
449 | * |
||
450 | * @param \Aimeos\MShop\Price\Item\Iface $price Price item |
||
451 | * @param boolean $costs Include costs per item |
||
452 | * @param boolean $tax Include tax |
||
453 | * @return string Formatted money amount |
||
454 | */ |
||
455 | protected function getAmount( \Aimeos\MShop\Price\Item\Iface $price, $costs = true, $tax = true ) |
||
480 | |||
481 | |||
482 | /** |
||
483 | * Returns the order item for the given ID. |
||
484 | * |
||
485 | * @param string $id Unique order ID |
||
486 | * @return \Aimeos\MShop\Order\Item\Iface $item Order object |
||
487 | */ |
||
488 | protected function getOrder( $id ) |
||
508 | |||
509 | |||
510 | /** |
||
511 | * Returns the base order which is equivalent to the basket. |
||
512 | * |
||
513 | * @param string $baseId Order base ID stored in the order item |
||
514 | * @param integer $parts Bitmap of the basket parts that should be loaded |
||
515 | * @return \Aimeos\MShop\Order\Item\Base\Iface Basket, optional with addresses, products, services and coupons |
||
516 | */ |
||
517 | protected function getOrderBase( $baseId, $parts = \Aimeos\MShop\Order\Manager\Base\Base::PARTS_SERVICE ) |
||
521 | |||
522 | |||
523 | /** |
||
524 | * Saves the order item. |
||
525 | * |
||
526 | * @param \Aimeos\MShop\Order\Item\Iface $item Order object |
||
527 | * @return \Aimeos\MShop\Order\Item\Iface Order object including the generated ID |
||
|
|||
528 | */ |
||
529 | protected function saveOrder( \Aimeos\MShop\Order\Item\Iface $item ) |
||
533 | |||
534 | |||
535 | /** |
||
536 | * Saves the base order which is equivalent to the basket and its dependent objects. |
||
537 | * |
||
538 | * @param \Aimeos\MShop\Order\Item\Base\Iface $base Order base object with associated items |
||
539 | * @param integer $parts Bitmap of the basket parts that should be stored |
||
540 | */ |
||
541 | protected function saveOrderBase( \Aimeos\MShop\Order\Item\Base\Iface $base, $parts = \Aimeos\MShop\Order\Manager\Base\Base::PARTS_SERVICE ) |
||
545 | |||
546 | |||
547 | /** |
||
548 | * Sets the attributes in the given service item. |
||
549 | * |
||
550 | * @param \Aimeos\MShop\Order\Item\Base\Service\Iface $orderServiceItem Order service item that will be added to the basket |
||
551 | * @param array $attributes Attribute key/value pairs entered by the customer during the checkout process |
||
552 | * @param string $type Type of the configuration values (delivery or payment) |
||
553 | */ |
||
554 | protected function setAttributes( \Aimeos\MShop\Order\Item\Base\Service\Iface $orderServiceItem, array $attributes, $type ) |
||
568 | |||
569 | |||
570 | /** |
||
571 | * Returns the public holidays in ISO format |
||
572 | * |
||
573 | * @param string $list Comma separated list of public holidays in YYYY-MM-DD format |
||
574 | * @return array List of dates in YYYY-MM-DD format |
||
575 | * @throws \Aimeos\MShop\Service\Exception If the given holiday string is in the wrong format and can't be processed |
||
576 | */ |
||
577 | private function getPublicHolidays( $list ) |
||
594 | } |
||
595 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.