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_Server2 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_Server2, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Intraface_XMLRPC_Shop_Server2 extends Intraface_XMLRPC_Server |
||
| 16 | { |
||
| 17 | private $webshop; |
||
| 18 | private $basket; |
||
|
|
|||
| 19 | private $product; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Gets a list with products |
||
| 23 | * |
||
| 24 | * @param struct $credentials Credentials to use the server |
||
| 25 | * @param integer $shop_id Id for the shop |
||
| 26 | * @param array $search Optional search array |
||
| 27 | * |
||
| 28 | * @return array |
||
| 29 | */ |
||
| 30 | public function getProducts($credentials, $shop_id, $search = array()) |
||
| 129 | |||
| 130 | |||
| 131 | /** |
||
| 132 | * Gets one product |
||
| 133 | * |
||
| 134 | * @param struct $credentials Credentials to use the server |
||
| 135 | * @param integer $shop_id Id for the shop |
||
| 136 | * @param integer $id Product id |
||
| 137 | * |
||
| 138 | * @return array |
||
| 139 | */ |
||
| 140 | public function getProduct($credentials, $shop_id, $id) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Gets related products |
||
| 212 | * |
||
| 213 | * @param struct $credentials Credentials to use the server |
||
| 214 | * @param integer $shop_id Id for the shop |
||
| 215 | * @param integer $id Product id |
||
| 216 | * |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | View Code Duplication | public function getRelatedProducts($credentials, $shop_id, $product_id) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Gets featured products |
||
| 238 | * |
||
| 239 | * Method is experimental and only used by discimport.dk. If you need to use it |
||
| 240 | * as well, please contact [email protected]. |
||
| 241 | * |
||
| 242 | * @param struct $credentials Credentials to use the server |
||
| 243 | * @param integer $shop_id Id for the shop |
||
| 244 | * |
||
| 245 | * @return array |
||
| 246 | */ |
||
| 247 | public function getFeaturedProducts($credentials, $shop_id) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Gets product keywords which can be used to sort ones webshop |
||
| 282 | * |
||
| 283 | * |
||
| 284 | * @param struct $credentials Credentials to use the server |
||
| 285 | * @param integer $shop_id Id for the shop |
||
| 286 | * |
||
| 287 | * @return array with id and keywords |
||
| 288 | */ |
||
| 289 | View Code Duplication | function getProductKeywords($credentials, $shop_id) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Returns the categories for the shop |
||
| 301 | * |
||
| 302 | * @param struct $credentials Credentials to use the server |
||
| 303 | * @param integer $shop_id Id for the shop |
||
| 304 | * |
||
| 305 | * @return array with categories |
||
| 306 | * |
||
| 307 | */ |
||
| 308 | View Code Duplication | public function getProductCategories($credentials, $shop_id) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Add product to basket |
||
| 326 | * |
||
| 327 | * @param struct $credentials Credentials to use the server |
||
| 328 | * @param integer $shop_id Id for the shop |
||
| 329 | * @param integer $produt_id Product id to add |
||
| 330 | * @param integer $product_variation_id Product variation id to change |
||
| 331 | * @param integer $quantity Optional quantity |
||
| 332 | * @param string $text Extra text to the itemline |
||
| 333 | * @param integer $product_detail_id Product detail id |
||
| 334 | * |
||
| 335 | * @return boolean |
||
| 336 | */ |
||
| 337 | View Code Duplication | public function addProductToBasket($credentials, $shop_id, $product_id, $product_variation_id, $quantity = 1, $text = '', $product_detail_id = 0) |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Change the quantity of one product in basket |
||
| 363 | * |
||
| 364 | * @param struct $credentials Credentials to use the server |
||
| 365 | * @param integer $shop_id Id for the shop |
||
| 366 | * @param integer $product_id Product id to change |
||
| 367 | * @param integer $product_variation_id Product_variation_id to change |
||
| 368 | * @param integer $quantity New quantity |
||
| 369 | * @param string $text Extra text to the itemline |
||
| 370 | * @param integer $product_detail_id Product detail id |
||
| 371 | * |
||
| 372 | * @return mixed |
||
| 373 | */ |
||
| 374 | View Code Duplication | public function changeProductInBasket($credentials, $shop_id, $product_id, $product_variation_id, $quantity, $text = '', $product_detail_id = 0) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Gets an array with the current basket |
||
| 400 | * |
||
| 401 | * @param struct $credentials Credentials to use the server |
||
| 402 | * @param integer $shop_id Id for the shop |
||
| 403 | * @param struct $customer customer values |
||
| 404 | * |
||
| 405 | * @return array |
||
| 406 | */ |
||
| 407 | public function getBasket($credentials, $shop_id, $customer = array()) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Places an order in Intraface based on the current basket |
||
| 434 | * |
||
| 435 | * @param struct $credentials Credentials to use the server |
||
| 436 | * @param integer $shop_id Id for the shop |
||
| 437 | * @param struct $values Values to save |
||
| 438 | * |
||
| 439 | * @return integer $order_id |
||
| 440 | */ |
||
| 441 | View Code Duplication | public function placeOrder($credentials, $shop_id, $values) |
|
| 465 | |||
| 466 | /** |
||
| 467 | * Saves buyer details |
||
| 468 | * |
||
| 469 | * @param struct $credentials Credentials to use the server |
||
| 470 | * @param integer $shop_id Id for the shop |
||
| 471 | * @param struct $values Values to save |
||
| 472 | * |
||
| 473 | * @return boolean true or false |
||
| 474 | */ |
||
| 475 | View Code Duplication | public function saveAddress($credentials, $shop_id, $values) |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Get buyer details |
||
| 498 | * |
||
| 499 | * @param struct $credentials Credentials to use the server |
||
| 500 | * @param integer $shop_id Id for the shop |
||
| 501 | * |
||
| 502 | * @return array |
||
| 503 | */ |
||
| 504 | public function getAddress($credentials, $shop_id) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Saves customer coupon |
||
| 515 | * |
||
| 516 | * @param struct $credentials Credentials to use the server |
||
| 517 | * @param integer $shop_id Id for the shop |
||
| 518 | * @param string $customer_coupon Customer coupon to save |
||
| 519 | * |
||
| 520 | * @return boolean true or false |
||
| 521 | */ |
||
| 522 | View Code Duplication | public function saveCustomerCoupon($credentials, $shop_id, $customer_coupon) |
|
| 536 | |||
| 537 | |||
| 538 | /** |
||
| 539 | * Get customer coupon |
||
| 540 | * |
||
| 541 | * @param struct $credentials Credentials to use the server |
||
| 542 | * @param integer $shop_id Id for the shop |
||
| 543 | * |
||
| 544 | * @return array |
||
| 545 | */ |
||
| 546 | public function getCustomerCoupon($credentials, $shop_id) |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Saves customer EAN location number |
||
| 556 | * |
||
| 557 | * @param struct $credentials Credentials to use the server |
||
| 558 | * @param integer $shop_id Id for the shop |
||
| 559 | * @param string $customer_ean Customer EAN to save |
||
| 560 | * |
||
| 561 | * @return boolean true or false |
||
| 562 | */ |
||
| 563 | View Code Duplication | public function saveCustomerEan($credentials, $shop_id, $customer_ean) |
|
| 577 | |||
| 578 | |||
| 579 | /** |
||
| 580 | * Get customer EAN location number |
||
| 581 | * |
||
| 582 | * @param struct $credentials Credentials to use the server |
||
| 583 | * @param integer $shop_id Id for the shop |
||
| 584 | * |
||
| 585 | * @return array |
||
| 586 | */ |
||
| 587 | public function getCustomerEan($credentials, $shop_id) |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Saves customer comment |
||
| 598 | * |
||
| 599 | * @param struct $credentials Credentials to use the server |
||
| 600 | * @param integer $shop_id Id for the shop |
||
| 601 | * @param string $customer_comment Customer coupon to save |
||
| 602 | * |
||
| 603 | * @return boolean true or false |
||
| 604 | */ |
||
| 605 | View Code Duplication | public function saveCustomerComment($credentials, $shop_id, $customer_comment) |
|
| 619 | |||
| 620 | |||
| 621 | /** |
||
| 622 | * Get customer comment |
||
| 623 | * |
||
| 624 | * @param struct $credentials Credentials to use the server |
||
| 625 | * @param integer $shop_id Id for the shop |
||
| 626 | * |
||
| 627 | * @return array |
||
| 628 | */ |
||
| 629 | public function getCustomerComment($credentials, $shop_id) |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Get possible payment methods |
||
| 640 | * |
||
| 641 | * @param struct $credentials Credentials to use the server |
||
| 642 | * @param integer $shop_id Id for the shop |
||
| 643 | * |
||
| 644 | * @return array |
||
| 645 | */ |
||
| 646 | public function getPaymentMethods($credentials, $shop_id) |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Saves payment method |
||
| 657 | * |
||
| 658 | * @param struct $credentials Credentials to use the server |
||
| 659 | * @param integer $shop_id Id for the shop |
||
| 660 | * @param string $payment_method Payment method to save |
||
| 661 | * |
||
| 662 | * @return boolean true or false |
||
| 663 | */ |
||
| 664 | View Code Duplication | public function savePaymentMethod($credentials, $shop_id, $payment_method) |
|
| 678 | |||
| 679 | /** |
||
| 680 | * Returns selected payment method |
||
| 681 | * |
||
| 682 | * @param struct $credentials Credentials to use the server |
||
| 683 | * @param integer $shop_id Id for the shop |
||
| 684 | * |
||
| 685 | * @return array |
||
| 686 | */ |
||
| 687 | public function getPaymentMethod($credentials, $shop_id) |
||
| 695 | |||
| 696 | /** |
||
| 697 | * Get receipt text |
||
| 698 | * |
||
| 699 | * @param struct $credentials Credentials to use the server |
||
| 700 | * @param integer $shop_id Id for the shop |
||
| 701 | * |
||
| 702 | * @return array |
||
| 703 | */ |
||
| 704 | public function getReceiptText($credentials, $shop_id) |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Get url for terms of trade |
||
| 715 | * |
||
| 716 | * @param struct $credentials Credentials to use the server |
||
| 717 | * @param integer $shop_id Id for the shop |
||
| 718 | * |
||
| 719 | * @return string |
||
| 720 | */ |
||
| 721 | public function getTermsOfTradeUrl($credentials, $shop_id) |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Get identifier |
||
| 732 | * |
||
| 733 | * @param struct $credentials Credentials to use the server |
||
| 734 | * @param integer $shop_id Id for the shop |
||
| 735 | * |
||
| 736 | * @return string |
||
| 737 | */ |
||
| 738 | public function getIdentifier($credentials, $shop_id) |
||
| 746 | |||
| 747 | /** |
||
| 748 | * Checks credentials |
||
| 749 | * |
||
| 750 | * @param struct $credentials Credentials to use the server |
||
| 751 | * |
||
| 752 | * @return array |
||
| 753 | */ |
||
| 754 | /* |
||
| 755 | protected function checkCredentials($credentials) |
||
| 756 | { |
||
| 757 | $this->credentials = $credentials; |
||
| 758 | |||
| 759 | if (count($credentials) != 2) { // -4 |
||
| 760 | require_once 'XML/RPC2/Exception.php'; |
||
| 761 | throw new XML_RPC2_FaultException('wrong argument count in $credentials - got ' . count($credentials) . ' arguments - need 2', -4); |
||
| 762 | } |
||
| 763 | if (empty($credentials['private_key'])) { // -5 |
||
| 764 | require_once 'XML/RPC2/Exception.php'; |
||
| 765 | throw new XML_RPC2_FaultException('supply a private_key', -5); |
||
| 766 | } |
||
| 767 | if (empty($credentials['session_id'])) { // -5 |
||
| 768 | require_once 'XML/RPC2/Exception.php'; |
||
| 769 | throw new XML_RPC2_FaultException('supply a session_id', -5); |
||
| 770 | } |
||
| 771 | |||
| 772 | $auth_adapter = new Intraface_Auth_PrivateKeyLogin(MDB2::singleton(DB_DSN), $credentials['session_id'], $credentials['private_key']); |
||
| 773 | $weblogin = $auth_adapter->auth(); |
||
| 774 | |||
| 775 | if (!$weblogin) { |
||
| 776 | require_once 'XML/RPC2/Exception.php'; |
||
| 777 | throw new XML_RPC2_FaultException('access to intranet denied', -2); |
||
| 778 | } |
||
| 779 | |||
| 780 | $this->kernel = new Intraface_Kernel($credentials['session_id']); |
||
| 781 | $this->kernel->weblogin = $weblogin; |
||
| 782 | $this->kernel->intranet = new Intraface_Intranet($weblogin->getActiveIntranetId()); |
||
| 783 | $this->kernel->setting = new Intraface_Setting($this->kernel->intranet->get('id')); |
||
| 784 | |||
| 785 | return true; |
||
| 786 | } |
||
| 787 | */ |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Initialize the webshop |
||
| 791 | * |
||
| 792 | * @return void |
||
| 793 | */ |
||
| 794 | private function _factoryWebshop($shop_id) |
||
| 809 | } |
||
| 810 |
This check marks private properties in classes that are never used. Those properties can be removed.