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 ApiClient 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 ApiClient, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class ApiClient |
||
| 11 | { |
||
| 12 | const VERSION = 'v3'; |
||
| 13 | |||
| 14 | protected $client; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Site code |
||
| 18 | */ |
||
| 19 | protected $siteCode; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Client creating |
||
| 23 | * |
||
| 24 | * @param string $url |
||
| 25 | * @param string $apiKey |
||
| 26 | * @param string $siteCode |
||
|
|
|||
| 27 | * @return mixed |
||
| 28 | */ |
||
| 29 | public function __construct($url, $apiKey, $site = null) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Create a order |
||
| 43 | * |
||
| 44 | * @param array $order |
||
| 45 | * @param string $site (default: null) |
||
| 46 | * @return ApiResponse |
||
| 47 | */ |
||
| 48 | View Code Duplication | public function ordersCreate(array $order, $site = null) |
|
| 58 | |||
| 59 | /** |
||
| 60 | * Edit a order |
||
| 61 | * |
||
| 62 | * @param array $order |
||
| 63 | * @param string $site (default: null) |
||
| 64 | * @return ApiResponse |
||
| 65 | */ |
||
| 66 | View Code Duplication | public function ordersEdit(array $order, $by = 'externalId', $site = null) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Upload array of the orders |
||
| 90 | * |
||
| 91 | * @param array $orders |
||
| 92 | * @param string $site (default: null) |
||
| 93 | * @return ApiResponse |
||
| 94 | */ |
||
| 95 | View Code Duplication | public function ordersUpload(array $orders, $site = null) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Get order by id or externalId |
||
| 108 | * |
||
| 109 | * @param string $id |
||
| 110 | * @param string $by (default: 'externalId') |
||
| 111 | * @param string $site (default: null) |
||
| 112 | * @return ApiResponse |
||
| 113 | */ |
||
| 114 | View Code Duplication | public function ordersGet($id, $by = 'externalId', $site = null) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Returns a orders history |
||
| 125 | * |
||
| 126 | * @param \DateTime $startDate (default: null) |
||
| 127 | * @param \DateTime $endDate (default: null) |
||
| 128 | * @param int $limit (default: 100) |
||
| 129 | * @param int $offset (default: 0) |
||
| 130 | * @param bool $skipMyChanges (default: true) |
||
| 131 | * @return ApiResponse |
||
| 132 | */ |
||
| 133 | public function ordersHistory( |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Returns filtered orders list |
||
| 163 | * |
||
| 164 | * @param array $filter (default: array()) |
||
| 165 | * @param int $page (default: null) |
||
| 166 | * @param int $limit (default: null) |
||
| 167 | * @return ApiResponse |
||
| 168 | */ |
||
| 169 | View Code Duplication | public function ordersList(array $filter = array(), $page = null, $limit = null) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Returns statuses of the orders |
||
| 188 | * |
||
| 189 | * @param array $ids (default: array()) |
||
| 190 | * @param array $externalIds (default: array()) |
||
| 191 | * @return ApiResponse |
||
| 192 | */ |
||
| 193 | public function ordersStatuses(array $ids = array(), array $externalIds = array()) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Save order IDs' (id and externalId) association in the CRM |
||
| 209 | * |
||
| 210 | * @param array $ids |
||
| 211 | * @return ApiResponse |
||
| 212 | */ |
||
| 213 | View Code Duplication | public function ordersFixExternalIds(array $ids) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Get orders assembly history |
||
| 226 | * |
||
| 227 | * @param array $filter (default: array()) |
||
| 228 | * @param int $page (default: null) |
||
| 229 | * @param int $limit (default: null) |
||
| 230 | * @return ApiResponse |
||
| 231 | */ |
||
| 232 | View Code Duplication | public function ordersPacksHistory(array $filter = array(), $page = null, $limit = null) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Create a customer |
||
| 251 | * |
||
| 252 | * @param array $customer |
||
| 253 | * @param string $site (default: null) |
||
| 254 | * @return ApiResponse |
||
| 255 | */ |
||
| 256 | View Code Duplication | public function customersCreate(array $customer, $site = null) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Edit a customer |
||
| 269 | * |
||
| 270 | * @param array $customer |
||
| 271 | * @param string $by (default: 'externalId') |
||
| 272 | * @param string $site (default: null) |
||
| 273 | * @return ApiResponse |
||
| 274 | */ |
||
| 275 | View Code Duplication | public function customersEdit(array $customer, $by = 'externalId', $site = null) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Upload array of the customers |
||
| 302 | * |
||
| 303 | * @param array $customers |
||
| 304 | * @param string $site (default: null) |
||
| 305 | * @return ApiResponse |
||
| 306 | */ |
||
| 307 | View Code Duplication | public function customersUpload(array $customers, $site = null) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Get customer by id or externalId |
||
| 320 | * |
||
| 321 | * @param string $id |
||
| 322 | * @param string $by (default: 'externalId') |
||
| 323 | * @param string $site (default: null) |
||
| 324 | * @return ApiResponse |
||
| 325 | */ |
||
| 326 | View Code Duplication | public function customersGet($id, $by = 'externalId', $site = null) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Returns filtered customers list |
||
| 337 | * |
||
| 338 | * @param array $filter (default: array()) |
||
| 339 | * @param int $page (default: null) |
||
| 340 | * @param int $limit (default: null) |
||
| 341 | * @return ApiResponse |
||
| 342 | */ |
||
| 343 | View Code Duplication | public function customersList(array $filter = array(), $page = null, $limit = null) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Save customer IDs' (id and externalId) association in the CRM |
||
| 362 | * |
||
| 363 | * @param array $ids |
||
| 364 | * @return ApiResponse |
||
| 365 | */ |
||
| 366 | View Code Duplication | public function customersFixExternalIds(array $ids) |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Get purchace prices & stock balance |
||
| 379 | * |
||
| 380 | * @param array $filter (default: array()) |
||
| 381 | * @param int $page (default: null) |
||
| 382 | * @param int $limit (default: null) |
||
| 383 | * @param string $site (default: null) |
||
| 384 | * @return ApiResponse |
||
| 385 | */ |
||
| 386 | View Code Duplication | public function storeInventories(array $filter = array(), $page = null, $limit = null, $site = null) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Upload store inventories |
||
| 405 | * |
||
| 406 | * @param array $offers |
||
| 407 | * @param string $site (default: null) |
||
| 408 | * @return ApiResponse |
||
| 409 | */ |
||
| 410 | View Code Duplication | public function storeInventoriesUpload(array $offers, $site = null) |
|
| 422 | |||
| 423 | /** |
||
| 424 | * Returns deliveryServices list |
||
| 425 | * |
||
| 426 | * @return ApiResponse |
||
| 427 | */ |
||
| 428 | public function deliveryServicesList() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Returns deliveryTypes list |
||
| 435 | * |
||
| 436 | * @return ApiResponse |
||
| 437 | */ |
||
| 438 | public function deliveryTypesList() |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Returns orderMethods list |
||
| 445 | * |
||
| 446 | * @return ApiResponse |
||
| 447 | */ |
||
| 448 | public function orderMethodsList() |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Returns orderTypes list |
||
| 455 | * |
||
| 456 | * @return ApiResponse |
||
| 457 | */ |
||
| 458 | public function orderTypesList() |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Returns paymentStatuses list |
||
| 465 | * |
||
| 466 | * @return ApiResponse |
||
| 467 | */ |
||
| 468 | public function paymentStatusesList() |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Returns paymentTypes list |
||
| 475 | * |
||
| 476 | * @return ApiResponse |
||
| 477 | */ |
||
| 478 | public function paymentTypesList() |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Returns productStatuses list |
||
| 485 | * |
||
| 486 | * @return ApiResponse |
||
| 487 | */ |
||
| 488 | public function productStatusesList() |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Returns statusGroups list |
||
| 495 | * |
||
| 496 | * @return ApiResponse |
||
| 497 | */ |
||
| 498 | public function statusGroupsList() |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Returns statuses list |
||
| 505 | * |
||
| 506 | * @return ApiResponse |
||
| 507 | */ |
||
| 508 | public function statusesList() |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Returns sites list |
||
| 515 | * |
||
| 516 | * @return ApiResponse |
||
| 517 | */ |
||
| 518 | public function sitesList() |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Edit deliveryService |
||
| 525 | * |
||
| 526 | * @param array $data delivery service data |
||
| 527 | * @return ApiResponse |
||
| 528 | */ |
||
| 529 | View Code Duplication | public function deliveryServicesEdit(array $data) |
|
| 543 | |||
| 544 | /** |
||
| 545 | * Edit deliveryType |
||
| 546 | * |
||
| 547 | * @param array $data delivery type data |
||
| 548 | * @return ApiResponse |
||
| 549 | */ |
||
| 550 | View Code Duplication | public function deliveryTypesEdit(array $data) |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Edit orderMethod |
||
| 567 | * |
||
| 568 | * @param array $data order method data |
||
| 569 | * @return ApiResponse |
||
| 570 | */ |
||
| 571 | View Code Duplication | public function orderMethodsEdit(array $data) |
|
| 585 | |||
| 586 | /** |
||
| 587 | * Edit orderType |
||
| 588 | * |
||
| 589 | * @param array $data order type data |
||
| 590 | * @return ApiResponse |
||
| 591 | */ |
||
| 592 | View Code Duplication | public function orderTypesEdit(array $data) |
|
| 606 | |||
| 607 | /** |
||
| 608 | * Edit paymentStatus |
||
| 609 | * |
||
| 610 | * @param array $data payment status data |
||
| 611 | * @return ApiResponse |
||
| 612 | */ |
||
| 613 | View Code Duplication | public function paymentStatusesEdit(array $data) |
|
| 627 | |||
| 628 | /** |
||
| 629 | * Edit paymentType |
||
| 630 | * |
||
| 631 | * @param array $data payment type data |
||
| 632 | * @return ApiResponse |
||
| 633 | */ |
||
| 634 | View Code Duplication | public function paymentTypesEdit(array $data) |
|
| 648 | |||
| 649 | /** |
||
| 650 | * Edit productStatus |
||
| 651 | * |
||
| 652 | * @param array $data product status data |
||
| 653 | * @return ApiResponse |
||
| 654 | */ |
||
| 655 | View Code Duplication | public function productStatusesEdit(array $data) |
|
| 669 | |||
| 670 | /** |
||
| 671 | * Edit order status |
||
| 672 | * |
||
| 673 | * @param array $data status data |
||
| 674 | * @return ApiResponse |
||
| 675 | */ |
||
| 676 | View Code Duplication | public function statusesEdit(array $data) |
|
| 690 | |||
| 691 | /** |
||
| 692 | * Edit site |
||
| 693 | * |
||
| 694 | * @param array $data site data |
||
| 695 | * @return ApiResponse |
||
| 696 | */ |
||
| 697 | View Code Duplication | public function sitesEdit(array $data) |
|
| 711 | |||
| 712 | /** |
||
| 713 | * Update CRM basic statistic |
||
| 714 | * |
||
| 715 | * @return ApiResponse |
||
| 716 | */ |
||
| 717 | public function statisticUpdate() |
||
| 721 | |||
| 722 | /** |
||
| 723 | * Return current site |
||
| 724 | * |
||
| 725 | * @return string |
||
| 726 | */ |
||
| 727 | public function getSite() |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Set site |
||
| 734 | * |
||
| 735 | * @param string $site |
||
| 736 | * @return void |
||
| 737 | */ |
||
| 738 | public function setSite($site) |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Check ID parameter |
||
| 745 | * |
||
| 746 | * @param string $by |
||
| 747 | * @return bool |
||
| 748 | */ |
||
| 749 | protected function checkIdParameter($by) |
||
| 762 | |||
| 763 | /** |
||
| 764 | * Fill params by site value |
||
| 765 | * |
||
| 766 | * @param string $site |
||
| 767 | * @param array $params |
||
| 768 | * @return array |
||
| 769 | */ |
||
| 770 | protected function fillSite($site, array $params) |
||
| 780 | } |
||
| 781 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.