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 |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Drops a package from the Balikobot – the package must be not ordered |
||
| 63 | * |
||
| 64 | * @param string $shipper |
||
| 65 | * @param int $packageId |
||
| 66 | * |
||
| 67 | * @return void |
||
| 68 | * |
||
| 69 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 70 | */ |
||
| 71 | 2 | public function dropPackage(string $shipper, int $packageId): void |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Drops a package from the Balikobot – the package must be not ordered |
||
| 78 | * |
||
| 79 | * @param string $shipper |
||
| 80 | * @param array<int> $packageIds |
||
| 81 | * |
||
| 82 | * @return void |
||
| 83 | * |
||
| 84 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 85 | */ |
||
| 86 | 7 | public function dropPackages(string $shipper, array $packageIds): void |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Tracks a package |
||
| 99 | * |
||
| 100 | * @param string $shipper |
||
| 101 | * @param string $carrierId |
||
| 102 | * |
||
| 103 | * @return array<array<string,float|string>> |
||
| 104 | * |
||
| 105 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 106 | */ |
||
| 107 | 7 | public function trackPackage(string $shipper, string $carrierId): array |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Tracks a packages |
||
| 116 | * |
||
| 117 | * @param string $shipper |
||
| 118 | * @param array<string> $carrierIds |
||
| 119 | * |
||
| 120 | * @return array<array<array<string,float|string>>> |
||
| 121 | * |
||
| 122 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 123 | */ |
||
| 124 | 21 | public function trackPackages(string $shipper, array $carrierIds): array |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Tracks a package, get the last info |
||
| 165 | * |
||
| 166 | * @param string $shipper |
||
| 167 | * @param string $carrierId |
||
| 168 | * |
||
| 169 | * @return array<string,float|string> |
||
| 170 | * |
||
| 171 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 172 | */ |
||
| 173 | 7 | public function trackPackageLastStatus(string $shipper, string $carrierId): array |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Tracks a package, get the last info |
||
| 182 | * |
||
| 183 | * @param string $shipper |
||
| 184 | * @param array<string> $carrierIds |
||
| 185 | * |
||
| 186 | * @return array<array<string,float|string|null>> |
||
| 187 | * |
||
| 188 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 189 | */ |
||
| 190 | 19 | public function trackPackagesLastStatus(string $shipper, array $carrierIds): array |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Returns packages from the front (not ordered) for given shipper |
||
| 219 | * |
||
| 220 | * @param string $shipper |
||
| 221 | * |
||
| 222 | * @return array<array<string,int|string>> |
||
| 223 | * |
||
| 224 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 225 | */ |
||
| 226 | 6 | public function getOverview(string $shipper): array |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Gets labels |
||
| 235 | * |
||
| 236 | * @param string $shipper |
||
| 237 | * @param array<int> $packageIds |
||
| 238 | * |
||
| 239 | * @return string |
||
| 240 | * |
||
| 241 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 242 | */ |
||
| 243 | 7 | public function getLabels(string $shipper, array $packageIds): string |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Gets complete information about a package |
||
| 258 | * |
||
| 259 | * @param string $shipper |
||
| 260 | * @param int $packageId |
||
| 261 | * |
||
| 262 | * @return array<string,int|string> |
||
| 263 | * |
||
| 264 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 265 | */ |
||
| 266 | 6 | public function getPackageInfo(string $shipper, int $packageId): array |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Order shipment for packages |
||
| 275 | * |
||
| 276 | * @param string $shipper |
||
| 277 | * @param array<int> $packageIds |
||
| 278 | * |
||
| 279 | * @return array<string,int|string> |
||
| 280 | * |
||
| 281 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 282 | */ |
||
| 283 | 7 | public function orderShipment(string $shipper, array $packageIds): array |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Get order details |
||
| 298 | * |
||
| 299 | * @param string $shipper |
||
| 300 | * @param int $orderId |
||
| 301 | * |
||
| 302 | * @return array<string,int|string|array> |
||
| 303 | * |
||
| 304 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 305 | */ |
||
| 306 | 7 | public function getOrder(string $shipper, int $orderId): array |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Order pickup for packages |
||
| 317 | * |
||
| 318 | * @param string $shipper |
||
| 319 | * @param \DateTime $dateFrom |
||
| 320 | * @param \DateTime $dateTo |
||
| 321 | * @param float $weight |
||
| 322 | * @param int $packageCount |
||
| 323 | * @param string|null $message |
||
| 324 | * |
||
| 325 | * @return void |
||
| 326 | * |
||
| 327 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 328 | */ |
||
| 329 | 4 | public function orderPickup( |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Returns available services for the given shipper |
||
| 351 | * |
||
| 352 | * @param string $shipper |
||
| 353 | * @param string|null $country |
||
| 354 | * @param string|null $version |
||
| 355 | * |
||
| 356 | * @return array<string,string> |
||
| 357 | * |
||
| 358 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 359 | */ |
||
| 360 | 12 | public function getServices(string $shipper, string $country = null, string $version = null): array |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Returns available B2A services for the given shipper |
||
| 371 | * |
||
| 372 | * @param string $shipper |
||
| 373 | * |
||
| 374 | * @return array<string,string> |
||
| 375 | * |
||
| 376 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 377 | */ |
||
| 378 | 8 | public function getB2AServices(string $shipper): array |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Returns all manipulation units for the given shipper |
||
| 389 | * |
||
| 390 | * @param string $shipper |
||
| 391 | * @param bool $fullData |
||
| 392 | * |
||
| 393 | * @return array<string,string|array> |
||
| 394 | * |
||
| 395 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 396 | */ |
||
| 397 | 9 | public function getManipulationUnits(string $shipper, bool $fullData = false): array |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Returns available manipulation units for the given shipper |
||
| 412 | * |
||
| 413 | * @param string $shipper |
||
| 414 | * @param bool $fullData |
||
| 415 | * |
||
| 416 | * @return array<string,string|array> |
||
| 417 | * |
||
| 418 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 419 | */ |
||
| 420 | 9 | public function getActivatedManipulationUnits(string $shipper, bool $fullData = false): array |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Returns available branches for the given shipper and its service |
||
| 435 | * Full branches instead branches request |
||
| 436 | * |
||
| 437 | * @param string $shipper |
||
| 438 | * @param string|null $service |
||
| 439 | * @param bool $fullBranchRequest |
||
| 440 | * @param string|null $country |
||
| 441 | * @param string|null $version |
||
| 442 | * |
||
| 443 | * @return array<array<string,mixed>> |
||
| 444 | * |
||
| 445 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 446 | */ |
||
| 447 | 15 | public function getBranches( |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Returns available branches for the given shipper in given location |
||
| 469 | * |
||
| 470 | * @param string $shipper |
||
| 471 | * @param string $country |
||
| 472 | * @param string $city |
||
| 473 | * @param string|null $postcode |
||
| 474 | * @param string|null $street |
||
| 475 | * @param int|null $maxResults |
||
| 476 | * @param float|null $radius |
||
| 477 | * @param string|null $type |
||
| 478 | * |
||
| 479 | * @return array<array<string,mixed>> |
||
| 480 | * |
||
| 481 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 482 | */ |
||
| 483 | 9 | public function getBranchesForLocation( |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Returns list of countries where service with cash-on-delivery payment type is available in |
||
| 512 | * |
||
| 513 | * @param string $shipper |
||
| 514 | * |
||
| 515 | * @return array<array<int|string,array<string,array>>> |
||
| 516 | * |
||
| 517 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 518 | */ |
||
| 519 | 8 | public function getCodCountries(string $shipper): array |
|
| 531 | |||
| 532 | /** |
||
| 533 | * Returns list of countries where service is available in |
||
| 534 | * |
||
| 535 | * @param string $shipper |
||
| 536 | * |
||
| 537 | * @return array<array<int|string,string>> |
||
| 538 | * |
||
| 539 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 540 | */ |
||
| 541 | 8 | public function getCountries(string $shipper): array |
|
| 553 | |||
| 554 | /** |
||
| 555 | * Returns available branches for the given shipper and its service |
||
| 556 | * |
||
| 557 | * @param string $shipper |
||
| 558 | * @param string $service |
||
| 559 | * @param string|null $country |
||
| 560 | * |
||
| 561 | * @return array<array<string,mixed>> |
||
| 562 | * |
||
| 563 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 564 | */ |
||
| 565 | 12 | public function getPostCodes(string $shipper, string $service, string $country = null): array |
|
| 585 | |||
| 586 | /** |
||
| 587 | * Check package(s) data |
||
| 588 | * |
||
| 589 | * @param string $shipper |
||
| 590 | * @param array<array<string>> $packages |
||
| 591 | * |
||
| 592 | * @return void |
||
| 593 | * |
||
| 594 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 595 | */ |
||
| 596 | 5 | public function checkPackages(string $shipper, array $packages): void |
|
| 600 | |||
| 601 | /** |
||
| 602 | * Returns available manipulation units for the given shipper |
||
| 603 | * |
||
| 604 | * @param string $shipper |
||
| 605 | * @param bool $fullData |
||
| 606 | * |
||
| 607 | * @return array<string> |
||
| 608 | * |
||
| 609 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 610 | */ |
||
| 611 | 9 | public function getAdrUnits(string $shipper, bool $fullData = false): array |
|
| 623 | |||
| 624 | /** |
||
| 625 | * Returns available activated services for the given shipper |
||
| 626 | * |
||
| 627 | * @param string $shipper |
||
| 628 | * |
||
| 629 | * @return array<string,mixed> |
||
| 630 | * |
||
| 631 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 632 | */ |
||
| 633 | 6 | public function getActivatedServices(string $shipper): array |
|
| 641 | |||
| 642 | /** |
||
| 643 | * Order shipments from place B (typically supplier / previous consignee) to place A (shipping point) |
||
| 644 | * |
||
| 645 | * @param string $shipper |
||
| 646 | * @param array<array<string,mixed>> $packages |
||
| 647 | * |
||
| 648 | * @return array<array<string,mixed>> |
||
| 649 | * |
||
| 650 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 651 | */ |
||
| 652 | 11 | public function orderB2AShipment(string $shipper, array $packages): array |
|
| 664 | |||
| 665 | /** |
||
| 666 | * Get PDF link with signed consignment delivery document by the recipient |
||
| 667 | * |
||
| 668 | * @param string $shipper |
||
| 669 | * @param string $carrierId |
||
| 670 | * |
||
| 671 | * @return string |
||
| 672 | * |
||
| 673 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 674 | */ |
||
| 675 | 7 | public function getProofOfDelivery(string $shipper, string $carrierId): string |
|
| 681 | |||
| 682 | /** |
||
| 683 | * Get array of PDF links with signed consignment delivery document by the recipient |
||
| 684 | * |
||
| 685 | * @param string $shipper |
||
| 686 | * @param array<string> $carrierIds |
||
| 687 | * |
||
| 688 | * @return array<string> |
||
| 689 | * |
||
| 690 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 691 | */ |
||
| 692 | 19 | public function getProofOfDeliveries(string $shipper, array $carrierIds): array |
|
| 712 | |||
| 713 | /** |
||
| 714 | * Obtain the price of carriage at consignment level |
||
| 715 | * |
||
| 716 | * @param string $shipper |
||
| 717 | * @param array<array<string,mixed>> $packages |
||
| 718 | * |
||
| 719 | * @return array<array<string,mixed>> |
||
| 720 | * |
||
| 721 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 722 | */ |
||
| 723 | 10 | public function getTransportCosts(string $shipper, array $packages): array |
|
| 735 | |||
| 736 | /** |
||
| 737 | * Ģet information on individual countries of the world |
||
| 738 | * |
||
| 739 | * @return array<array<string,mixed>> |
||
| 740 | * |
||
| 741 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 742 | */ |
||
| 743 | 8 | public function getCountriesData(): array |
|
| 751 | |||
| 752 | /** |
||
| 753 | * Validate response item status |
||
| 754 | * |
||
| 755 | * @param array<mixed,mixed> $responseItem |
||
| 756 | * @param array<mixed,mixed> $response |
||
| 757 | * |
||
| 758 | * @return void |
||
| 759 | * |
||
| 760 | * @throws \Inspirum\Balikobot\Exceptions\BadRequestException |
||
| 761 | */ |
||
| 762 | 23 | private function validateStatus(array $responseItem, array $response): void |
|
| 768 | |||
| 769 | /** |
||
| 770 | * Validate that every response item has given attribute |
||
| 771 | * |
||
| 772 | * @param array<int,array<string,mixed>> $items |
||
| 773 | * @param string $attribute |
||
| 774 | * @param array<mixed,mixed> $response |
||
| 775 | * |
||
| 776 | * @return void |
||
| 777 | * |
||
| 778 | * @throws \Inspirum\Balikobot\Exceptions\BadRequestException |
||
| 779 | */ |
||
| 780 | 41 | private function validateResponseItemHasAttribute(array $items, string $attribute, array $response): void |
|
| 788 | |||
| 789 | /** |
||
| 790 | * Validate indexes |
||
| 791 | * |
||
| 792 | * @param array<mixed,mixed> $response |
||
| 793 | * @param array<mixed,mixed> $request |
||
| 794 | * |
||
| 795 | * @return void |
||
| 796 | * |
||
| 797 | * @throws \Inspirum\Balikobot\Exceptions\BadRequestException |
||
| 798 | */ |
||
| 799 | 78 | private function validateIndexes(array $response, array $request): void |
|
| 805 | |||
| 806 | /** |
||
| 807 | * Normalize response items |
||
| 808 | * |
||
| 809 | * @param array<array<string,string>> $items |
||
| 810 | * @param string $keyName |
||
| 811 | * @param string|null $valueName |
||
| 812 | * |
||
| 813 | * @return array<string,mixed> |
||
| 814 | */ |
||
| 815 | 33 | private function normalizeResponseItems(array $items, string $keyName, ?string $valueName): array |
|
| 825 | |||
| 826 | /** |
||
| 827 | * Encapsulate ids |
||
| 828 | * |
||
| 829 | * @param array<int|string> $ids |
||
| 830 | * |
||
| 831 | * @return array<array<int|string>> |
||
| 832 | */ |
||
| 833 | 66 | private function encapsulateIds(array $ids): array |
|
| 841 | } |
||
| 842 |