Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Client |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * API requester |
||
| 16 | * |
||
| 17 | * @var \Inspirum\Balikobot\Contracts\RequesterInterface |
||
| 18 | */ |
||
| 19 | private $requester; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Balikobot API client |
||
| 23 | * |
||
| 24 | * @param \Inspirum\Balikobot\Contracts\RequesterInterface $requester |
||
| 25 | */ |
||
| 26 | 274 | public function __construct(RequesterInterface $requester) |
|
| 30 | |||
| 31 | /** |
||
| 32 | * Add package(s) to the Balikobot |
||
| 33 | * |
||
| 34 | * @param string $shipper |
||
| 35 | * @param array<array<string,mixed>> $packages |
||
| 36 | * @param string|null $version |
||
| 37 | * @param mixed|null $labelsUrl |
||
| 38 | * |
||
| 39 | * @return array<array<string,mixed>> |
||
| 40 | * |
||
| 41 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 42 | */ |
||
| 43 | 25 | public function addPackages(string $shipper, array $packages, string $version = null, &$labelsUrl = null): array |
|
| 44 | { |
||
| 45 | 25 | $response = $this->requester->call($version ?: API::V1, $shipper, Request::ADD, $packages); |
|
| 46 | |||
| 47 | 16 | if (isset($response['labels_url'])) { |
|
| 48 | 5 | $labelsUrl = $response['labels_url']; |
|
| 49 | } |
||
| 50 | |||
| 51 | unset( |
||
| 52 | 16 | $response['labels_url'], |
|
| 53 | 16 | $response['status'] |
|
| 54 | ); |
||
| 55 | |||
| 56 | 16 | $this->validateIndexes($response, $packages); |
|
| 57 | |||
| 58 | 15 | $this->validateResponseItemHasAttribute($response, 'package_id', $response); |
|
| 59 | |||
| 60 | 13 | return $response; |
|
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Drops a package from the Balikobot – the package must be not ordered |
||
| 65 | * |
||
| 66 | * @param string $shipper |
||
| 67 | * @param int $packageId |
||
| 68 | * |
||
| 69 | * @return void |
||
| 70 | * |
||
| 71 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 72 | */ |
||
| 73 | 2 | public function dropPackage(string $shipper, int $packageId): void |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Drops a package from the Balikobot – the package must be not ordered |
||
| 80 | * |
||
| 81 | * @param string $shipper |
||
| 82 | * @param array<int> $packageIds |
||
| 83 | * |
||
| 84 | * @return void |
||
| 85 | * |
||
| 86 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 87 | */ |
||
| 88 | 7 | public function dropPackages(string $shipper, array $packageIds): void |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Tracks a package |
||
| 101 | * |
||
| 102 | * @param string $shipper |
||
| 103 | * @param string $carrierId |
||
| 104 | * |
||
| 105 | * @return array<array<string,float|string>> |
||
| 106 | * |
||
| 107 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 108 | */ |
||
| 109 | 7 | public function trackPackage(string $shipper, string $carrierId): array |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Tracks a packages |
||
| 118 | * |
||
| 119 | * @param string $shipper |
||
| 120 | * @param array<string> $carrierIds |
||
| 121 | * |
||
| 122 | * @return array<array<array<string,float|string>>> |
||
| 123 | * |
||
| 124 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 125 | */ |
||
| 126 | 21 | public function trackPackages(string $shipper, array $carrierIds): array |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Tracks a package, get the last info |
||
| 167 | * |
||
| 168 | * @param string $shipper |
||
| 169 | * @param string $carrierId |
||
| 170 | * |
||
| 171 | * @return array<string,float|string> |
||
| 172 | * |
||
| 173 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 174 | */ |
||
| 175 | 7 | public function trackPackageLastStatus(string $shipper, string $carrierId): array |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Tracks a package, get the last info |
||
| 184 | * |
||
| 185 | * @param string $shipper |
||
| 186 | * @param array<string> $carrierIds |
||
| 187 | * |
||
| 188 | * @return array<array<string,float|string|null>> |
||
| 189 | * |
||
| 190 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 191 | */ |
||
| 192 | 19 | public function trackPackagesLastStatus(string $shipper, array $carrierIds): array |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Returns packages from the front (not ordered) for given shipper |
||
| 221 | * |
||
| 222 | * @param string $shipper |
||
| 223 | * |
||
| 224 | * @return array<array<string,int|string>> |
||
| 225 | * |
||
| 226 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 227 | */ |
||
| 228 | 6 | public function getOverview(string $shipper): array |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Gets labels |
||
| 237 | * |
||
| 238 | * @param string $shipper |
||
| 239 | * @param array<int> $packageIds |
||
| 240 | * |
||
| 241 | * @return string |
||
| 242 | * |
||
| 243 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 244 | */ |
||
| 245 | 7 | public function getLabels(string $shipper, array $packageIds): string |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Gets complete information about a package by its package ID |
||
| 260 | * |
||
| 261 | * @param string $shipper |
||
| 262 | * @param int $packageId |
||
| 263 | * |
||
| 264 | * @return array<string,int|string> |
||
| 265 | * |
||
| 266 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 267 | */ |
||
| 268 | 6 | public function getPackageInfo(string $shipper, int $packageId): array |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Gets complete information about a package by its carrier ID |
||
| 277 | * |
||
| 278 | * @param string $shipper |
||
| 279 | * @param string $carrierId |
||
| 280 | * |
||
| 281 | * @return array<string,int|string> |
||
| 282 | * |
||
| 283 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 284 | */ |
||
| 285 | 7 | public function getPackageInfoByCarrierId(string $shipper, string $carrierId): array |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Order shipment for packages |
||
| 300 | * |
||
| 301 | * @param string $shipper |
||
| 302 | * @param array<int> $packageIds |
||
| 303 | * |
||
| 304 | * @return array<string,int|string> |
||
| 305 | * |
||
| 306 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 307 | */ |
||
| 308 | 7 | public function orderShipment(string $shipper, array $packageIds): array |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Get order details |
||
| 323 | * |
||
| 324 | * @param string $shipper |
||
| 325 | * @param int $orderId |
||
| 326 | * |
||
| 327 | * @return array<string,int|string|array> |
||
| 328 | * |
||
| 329 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 330 | */ |
||
| 331 | 4 | public function getOrder(string $shipper, int $orderId): array |
|
| 339 | |||
| 340 | 4 | /** |
|
| 341 | 4 | * Order pickup for packages |
|
| 342 | 4 | * |
|
| 343 | 4 | * @param string $shipper |
|
| 344 | 4 | * @param \DateTime $dateFrom |
|
| 345 | 4 | * @param \DateTime $dateTo |
|
| 346 | * @param float $weight |
||
| 347 | * @param int $packageCount |
||
| 348 | 4 | * @param string|null $message |
|
| 349 | 1 | * |
|
| 350 | * @return void |
||
| 351 | * |
||
| 352 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 353 | */ |
||
| 354 | public function orderPickup( |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Returns available services for the given shipper |
||
| 376 | * |
||
| 377 | * @param string $shipper |
||
| 378 | * @param string|null $country |
||
| 379 | * @param string|null $version |
||
| 380 | 8 | * |
|
| 381 | * @return array<string,string> |
||
| 382 | 8 | * |
|
| 383 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 384 | 5 | */ |
|
| 385 | public function getServices(string $shipper, string $country = null, string $version = null): array |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Returns available B2A services for the given shipper |
||
| 396 | * |
||
| 397 | * @param string $shipper |
||
| 398 | * |
||
| 399 | 9 | * @return array<string,string> |
|
| 400 | * |
||
| 401 | 9 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
|
| 402 | */ |
||
| 403 | 6 | public function getB2AServices(string $shipper): array |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Returns all manipulation units for the given shipper |
||
| 414 | * |
||
| 415 | * @param string $shipper |
||
| 416 | * @param bool $fullData |
||
| 417 | * |
||
| 418 | * @return array<string,string|array> |
||
| 419 | * |
||
| 420 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 421 | */ |
||
| 422 | 9 | public function getManipulationUnits(string $shipper, bool $fullData = false): array |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Returns available manipulation units for the given shipper |
||
| 437 | * |
||
| 438 | * @param string $shipper |
||
| 439 | * @param bool $fullData |
||
| 440 | * |
||
| 441 | * @return array<string,string|array> |
||
| 442 | * |
||
| 443 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 444 | */ |
||
| 445 | public function getActivatedManipulationUnits(string $shipper, bool $fullData = false): array |
||
| 457 | |||
| 458 | 15 | /** |
|
| 459 | 15 | * Returns available branches for the given shipper and its service |
|
| 460 | * Full branches instead branches request |
||
| 461 | 15 | * |
|
| 462 | * @param string $shipper |
||
| 463 | * @param string|null $service |
||
| 464 | 12 | * @param bool $fullBranchRequest |
|
| 465 | * @param string|null $country |
||
| 466 | 12 | * @param string|null $version |
|
| 467 | * |
||
| 468 | * @return array<array<string,mixed>> |
||
| 469 | * |
||
| 470 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 471 | */ |
||
| 472 | public function getBranches( |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Returns available branches for the given shipper in given location |
||
| 494 | * |
||
| 495 | * @param string $shipper |
||
| 496 | 9 | * @param string $country |
|
| 497 | 9 | * @param string $city |
|
| 498 | 9 | * @param string|null $postcode |
|
| 499 | 9 | * @param string|null $street |
|
| 500 | 9 | * @param int|null $maxResults |
|
| 501 | 9 | * @param float|null $radius |
|
| 502 | 9 | * @param string|null $type |
|
| 503 | * |
||
| 504 | * @return array<array<string,mixed>> |
||
| 505 | 9 | * |
|
| 506 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 507 | 6 | */ |
|
| 508 | public function getBranchesForLocation( |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Returns list of countries where service with cash-on-delivery payment type is available in |
||
| 537 | * |
||
| 538 | * @param string $shipper |
||
| 539 | * |
||
| 540 | * @return array<array<int|string,array<string,array>>> |
||
| 541 | * |
||
| 542 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 543 | 8 | */ |
|
| 544 | public function getCodCountries(string $shipper): array |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Returns list of countries where service is available in |
||
| 559 | * |
||
| 560 | * @param string $shipper |
||
| 561 | * |
||
| 562 | * @return array<array<int|string,string>> |
||
| 563 | * |
||
| 564 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 565 | */ |
||
| 566 | public function getCountries(string $shipper): array |
||
| 578 | 5 | ||
| 579 | 5 | /** |
|
| 580 | 5 | * Returns available branches for the given shipper and its service |
|
| 581 | 5 | * |
|
| 582 | * @param string $shipper |
||
| 583 | * @param string $service |
||
| 584 | * @param string|null $country |
||
| 585 | 9 | * |
|
| 586 | * @return array<array<string,mixed>> |
||
| 587 | * |
||
| 588 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 589 | */ |
||
| 590 | public function getPostCodes(string $shipper, string $service, string $country = null): array |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Check package(s) data |
||
| 613 | 9 | * |
|
| 614 | * @param string $shipper |
||
| 615 | 9 | * @param array<array<string>> $packages |
|
| 616 | * |
||
| 617 | 6 | * @return void |
|
| 618 | 6 | * |
|
| 619 | 6 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
|
| 620 | 6 | */ |
|
| 621 | public function checkPackages(string $shipper, array $packages): void |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Returns available manipulation units for the given shipper |
||
| 628 | * |
||
| 629 | * @param string $shipper |
||
| 630 | * @param bool $fullData |
||
| 631 | * |
||
| 632 | * @return array<string> |
||
| 633 | * |
||
| 634 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 635 | 6 | */ |
|
| 636 | public function getAdrUnits(string $shipper, bool $fullData = false): array |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Returns available activated services for the given shipper |
||
| 651 | * |
||
| 652 | * @param string $shipper |
||
| 653 | * |
||
| 654 | 11 | * @return array<string,mixed> |
|
| 655 | * |
||
| 656 | 11 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
|
| 657 | */ |
||
| 658 | 8 | public function getActivatedServices(string $shipper): array |
|
| 666 | |||
| 667 | /** |
||
| 668 | * Order shipments from place B (typically supplier / previous consignee) to place A (shipping point) |
||
| 669 | * |
||
| 670 | * @param string $shipper |
||
| 671 | * @param array<array<string,mixed>> $packages |
||
| 672 | * |
||
| 673 | * @return array<array<string,mixed>> |
||
| 674 | * |
||
| 675 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 676 | */ |
||
| 677 | 7 | public function orderB2AShipment(string $shipper, array $packages): array |
|
| 689 | |||
| 690 | /** |
||
| 691 | * Get PDF link with signed consignment delivery document by the recipient |
||
| 692 | * |
||
| 693 | * @param string $shipper |
||
| 694 | 19 | * @param string $carrierId |
|
| 695 | * |
||
| 696 | 19 | * @return string |
|
| 697 | * |
||
| 698 | 19 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
|
| 699 | */ |
||
| 700 | 15 | public function getProofOfDelivery(string $shipper, string $carrierId): string |
|
| 706 | 11 | ||
| 707 | 11 | /** |
|
| 708 | * Get array of PDF links with signed consignment delivery document by the recipient |
||
| 709 | 11 | * |
|
| 710 | * @param string $shipper |
||
| 711 | * @param array<string> $carrierIds |
||
| 712 | 10 | * |
|
| 713 | * @return array<string> |
||
| 714 | * |
||
| 715 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 716 | */ |
||
| 717 | public function getProofOfDeliveries(string $shipper, array $carrierIds): array |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Obtain the price of carriage at consignment level |
||
| 740 | * |
||
| 741 | * @param string $shipper |
||
| 742 | * @param array<array<string,mixed>> $packages |
||
| 743 | * |
||
| 744 | * @return array<array<string,mixed>> |
||
| 745 | 8 | * |
|
| 746 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 747 | 8 | */ |
|
| 748 | public function getTransportCosts(string $shipper, array $packages): array |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Ģet information on individual countries of the world |
||
| 763 | * |
||
| 764 | 23 | * @return array<array<string,mixed>> |
|
| 765 | * |
||
| 766 | 23 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
|
| 767 | 3 | */ |
|
| 768 | public function getCountriesData(): array |
||
| 776 | |||
| 777 | /** |
||
| 778 | * Validate response item status |
||
| 779 | * |
||
| 780 | * @param array<mixed,mixed> $responseItem |
||
| 781 | * @param array<mixed,mixed> $response |
||
| 782 | 41 | * |
|
| 783 | * @return void |
||
| 784 | 41 | * |
|
| 785 | 41 | * @throws \Inspirum\Balikobot\Exceptions\BadRequestException |
|
| 786 | 8 | */ |
|
| 787 | private function validateStatus(array $responseItem, array $response): void |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Validate that every response item has given attribute |
||
| 796 | * |
||
| 797 | * @param array<int,array<string,mixed>> $items |
||
| 798 | * @param string $attribute |
||
| 799 | * @param array<mixed,mixed> $response |
||
| 800 | * |
||
| 801 | 78 | * @return void |
|
| 802 | * |
||
| 803 | 78 | * @throws \Inspirum\Balikobot\Exceptions\BadRequestException |
|
| 804 | 14 | */ |
|
| 805 | private function validateResponseItemHasAttribute(array $items, string $attribute, array $response): void |
||
| 813 | |||
| 814 | /** |
||
| 815 | * Validate indexes |
||
| 816 | * |
||
| 817 | 33 | * @param array<mixed,mixed> $response |
|
| 818 | * @param array<mixed,mixed> $request |
||
| 819 | 33 | * |
|
| 820 | * @return void |
||
| 821 | 33 | * |
|
| 822 | 15 | * @throws \Inspirum\Balikobot\Exceptions\BadRequestException |
|
| 823 | */ |
||
| 824 | private function validateIndexes(array $response, array $request): void |
||
| 830 | |||
| 831 | /** |
||
| 832 | * Normalize response items |
||
| 833 | * |
||
| 834 | * @param array<array<string,string>> $items |
||
| 835 | 66 | * @param string $keyName |
|
| 836 | * @param string|null $valueName |
||
| 837 | 66 | * |
|
| 838 | * @return array<string,mixed> |
||
| 839 | 65 | */ |
|
| 840 | private function normalizeResponseItems(array $items, string $keyName, ?string $valueName): array |
||
| 850 | |||
| 851 | /** |
||
| 852 | * Encapsulate ids |
||
| 853 | * |
||
| 854 | * @param array<int|string> $ids |
||
| 855 | * |
||
| 856 | * @return array<array<int|string>> |
||
| 857 | */ |
||
| 858 | private function encapsulateIds(array $ids): array |
||
| 866 | } |
||
| 867 |