Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Intraface_XMLRPC_Shop_Server 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 Intraface_XMLRPC_Shop_Server, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Intraface_XMLRPC_Shop_Server |
||
14 | { |
||
15 | private $kernel; |
||
16 | private $webshop; |
||
17 | private $basket; |
||
18 | private $product; |
||
|
|||
19 | private $credentials; |
||
20 | |||
21 | /** |
||
22 | * Gets a list with products |
||
23 | * |
||
24 | * @param struct $credentials Credentials to use the server |
||
25 | * @param array $search Optional search array |
||
26 | * |
||
27 | * @return array |
||
28 | */ |
||
29 | public function getProducts($credentials, $search = array()) |
||
99 | |||
100 | |||
101 | /** |
||
102 | * Gets one product |
||
103 | * |
||
104 | * @param struct $credentials Credentials to use the server |
||
105 | * @param integer $id Product id |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | public function getProduct($credentials, $id) |
||
131 | |||
132 | /** |
||
133 | * Gets related products |
||
134 | * |
||
135 | * @param struct $credentials Credentials to use the server |
||
136 | * @param integer $id Product id |
||
137 | * |
||
138 | * @return array |
||
139 | */ |
||
140 | View Code Duplication | public function getRelatedProducts($credentials, $id) |
|
156 | |||
157 | /** |
||
158 | * Gets featured products |
||
159 | * |
||
160 | * Method is experimental and only used by discimport.dk. If you need to use it |
||
161 | * as well, please contact [email protected]. |
||
162 | * |
||
163 | * @param struct $credentials Credentials to use the server |
||
164 | * |
||
165 | * @return array |
||
166 | */ |
||
167 | public function getFeaturedProducts($credentials) |
||
200 | |||
201 | /** |
||
202 | * Gets product keywords which can be used to sort ones webshop |
||
203 | * |
||
204 | * Method is experimental and only used by nylivsstil.dk. If you need to use it |
||
205 | * as well, please contact [email protected]. |
||
206 | * |
||
207 | * @param struct $credentials Credentials to use the server |
||
208 | * |
||
209 | * @return array with id and keywords |
||
210 | */ |
||
211 | View Code Duplication | function getProductKeywords($credentials) |
|
221 | |||
222 | /** |
||
223 | * Add product to basket |
||
224 | * |
||
225 | * @param struct $credentials Credentials to use the server |
||
226 | * @param integer $id Product id to add |
||
227 | * @param integer $quantity Optional quantity |
||
228 | * @param string $text Extra text to the itemline |
||
229 | * @param integer $product_detail_id Product detail id |
||
230 | * |
||
231 | * @return boolean |
||
232 | */ |
||
233 | public function addProductToBasket($credentials, $id, $quantity = 1, $text = '', $product_detail_id = 0) |
||
251 | |||
252 | /** |
||
253 | * Change the quantity of one product in basket |
||
254 | * |
||
255 | * @param struct $credentials Credentials to use the server |
||
256 | * @param integer $product_id Product id to change |
||
257 | * @param integer $quantity New quantity |
||
258 | * @param string $text Extra text to the itemline |
||
259 | * @param integer $product_detail_id Product detail id |
||
260 | * |
||
261 | * @return mixed |
||
262 | */ |
||
263 | public function changeProductInBasket($credentials, $product_id, $quantity, $text = '', $product_detail_id = 0) |
||
284 | |||
285 | /** |
||
286 | * Gets an array with the current basket |
||
287 | * |
||
288 | * @param struct $credentials Credentials to use the server |
||
289 | * @param struct $customer customer values |
||
290 | * |
||
291 | * @return array |
||
292 | */ |
||
293 | public function getBasket($credentials, $customer = array()) |
||
316 | |||
317 | /** |
||
318 | * Places an order in Intraface based on the current basket |
||
319 | * |
||
320 | * <code> |
||
321 | * |
||
322 | * </code> |
||
323 | * |
||
324 | * @param struct $credentials Credentials to use the server |
||
325 | * @param struct $values Values to save |
||
326 | * |
||
327 | * @return integer $order_id |
||
328 | */ |
||
329 | public function placeOrder($credentials, $values) |
||
353 | |||
354 | |||
355 | /** |
||
356 | * Saves details for a processed onlineoayment |
||
357 | * |
||
358 | * |
||
359 | * @param struct $credentials Credentials to use the server |
||
360 | * @param struct $values Values to save |
||
361 | * |
||
362 | * @return integer $payment_id |
||
363 | */ |
||
364 | public function saveOnlinePayment($credentials, $values) |
||
390 | |||
391 | |||
392 | /** |
||
393 | * Returns an onlinepayment id to be processed to the id can be used in payment |
||
394 | * |
||
395 | * @param struct $credentials Credentials to use the server |
||
396 | * |
||
397 | * @return integer $payment_id |
||
398 | */ |
||
399 | public function createOnlinePayment($credentials) |
||
421 | |||
422 | /** |
||
423 | * Saves buyer details |
||
424 | * |
||
425 | * @param struct $credentials Credentials to use the server |
||
426 | * @param struct $values Values to save |
||
427 | * |
||
428 | * @return boolean true or false |
||
429 | */ |
||
430 | View Code Duplication | public function saveAddress($credentials, $values) |
|
450 | |||
451 | /** |
||
452 | * Get buyer details |
||
453 | * |
||
454 | * @param struct $credentials Credentials to use the server |
||
455 | * |
||
456 | * @return array |
||
457 | */ |
||
458 | public function getAddress($credentials) |
||
466 | |||
467 | /** |
||
468 | * Saves customer coupon |
||
469 | * |
||
470 | * @param struct $credentials Credentials to use the server |
||
471 | * @param string $customer_coupon Customer coupon to save |
||
472 | * |
||
473 | * @return boolean true or false |
||
474 | */ |
||
475 | View Code Duplication | public function saveCustomerCoupon($credentials, $customer_coupon) |
|
489 | |||
490 | |||
491 | /** |
||
492 | * Get customer coupon |
||
493 | * |
||
494 | * @param struct $credentials Credentials to use the server |
||
495 | * |
||
496 | * @return array |
||
497 | */ |
||
498 | public function getCustomerCoupon($credentials) |
||
506 | |||
507 | /** |
||
508 | * Saves customer EAN location number |
||
509 | * |
||
510 | * @param struct $credentials Credentials to use the server |
||
511 | * @param string $customer_ean Customer EAN to save |
||
512 | * |
||
513 | * @return boolean true or false |
||
514 | */ |
||
515 | View Code Duplication | public function saveCustomerEan($credentials, $customer_ean) |
|
529 | |||
530 | |||
531 | /** |
||
532 | * Get customer EAN location number |
||
533 | * |
||
534 | * @param struct $credentials Credentials to use the server |
||
535 | * |
||
536 | * @return array |
||
537 | */ |
||
538 | public function getCustomerEan($credentials) |
||
546 | |||
547 | /** |
||
548 | * Saves customer comment |
||
549 | * |
||
550 | * @param struct $credentials Credentials to use the server |
||
551 | * @param string $customer_comment Customer coupon to save |
||
552 | * |
||
553 | * @return boolean true or false |
||
554 | */ |
||
555 | View Code Duplication | public function saveCustomerComment($credentials, $customer_comment) |
|
569 | |||
570 | |||
571 | /** |
||
572 | * Get customer comment |
||
573 | * |
||
574 | * @param struct $credentials Credentials to use the server |
||
575 | * |
||
576 | * @return array |
||
577 | */ |
||
578 | public function getCustomerComment($credentials) |
||
586 | |||
587 | /** |
||
588 | * Get receipt text |
||
589 | * |
||
590 | * @param struct $credentials Credentials to use the server |
||
591 | * |
||
592 | * @return array |
||
593 | */ |
||
594 | public function getReceiptText($credentials) |
||
602 | |||
603 | /** |
||
604 | * Checks credentials |
||
605 | * |
||
606 | * @param struct $credentials Credentials to use the server |
||
607 | * |
||
608 | * @return array |
||
609 | */ |
||
610 | private function checkCredentials($credentials) |
||
642 | |||
643 | /** |
||
644 | * Initialize the webshop |
||
645 | * |
||
646 | * @return void |
||
647 | */ |
||
648 | private function _factoryWebshop() |
||
658 | |||
659 | private function utf8Decode($values) |
||
669 | } |
||
670 |
This check marks private properties in classes that are never used. Those properties can be removed.