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 |
||
| 10 | class Client |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * API requester |
||
| 14 | * |
||
| 15 | * @var \Inspirum\Balikobot\Contracts\RequesterInterface |
||
| 16 | */ |
||
| 17 | private $requester; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Request and Response formatter |
||
| 21 | * |
||
| 22 | * @var \Inspirum\Balikobot\Services\Formatter |
||
| 23 | */ |
||
| 24 | private $formatter; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Response validator |
||
| 28 | * |
||
| 29 | * @var \Inspirum\Balikobot\Services\Validator |
||
| 30 | */ |
||
| 31 | private $validator; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Balikobot API client |
||
| 35 | * |
||
| 36 | * @param \Inspirum\Balikobot\Contracts\RequesterInterface $requester |
||
| 37 | */ |
||
| 38 | 322 | public function __construct(RequesterInterface $requester) |
|
| 44 | |||
| 45 | /** |
||
| 46 | * Add package(s) to the Balikobot |
||
| 47 | * |
||
| 48 | * @param string $shipper |
||
| 49 | * @param array<array<string,mixed>> $packages |
||
| 50 | * @param mixed|null $labelsUrl |
||
| 51 | * |
||
| 52 | * @return array<array<string,mixed>> |
||
| 53 | * |
||
| 54 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 55 | */ |
||
| 56 | 29 | public function addPackages(string $shipper, array $packages, &$labelsUrl = null): array |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Drops a package from the Balikobot – the package must be not ordered |
||
| 75 | * |
||
| 76 | * @param string $shipper |
||
| 77 | * @param string $packageId |
||
| 78 | * |
||
| 79 | * @return void |
||
| 80 | * |
||
| 81 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 82 | */ |
||
| 83 | 2 | public function dropPackage(string $shipper, string $packageId): void |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Drops a package from the Balikobot – the package must be not ordered |
||
| 90 | * |
||
| 91 | * @param string $shipper |
||
| 92 | * @param array<string> $packageIds |
||
| 93 | * |
||
| 94 | * @return void |
||
| 95 | * |
||
| 96 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 97 | */ |
||
| 98 | 9 | public function dropPackages(string $shipper, array $packageIds): void |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Tracks a package |
||
| 107 | * |
||
| 108 | * @param string $shipper |
||
| 109 | * @param string $carrierId |
||
| 110 | * |
||
| 111 | * @return array<array<string,float|string>> |
||
| 112 | * |
||
| 113 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 114 | */ |
||
| 115 | 7 | public function trackPackage(string $shipper, string $carrierId): array |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Tracks a packages |
||
| 122 | * |
||
| 123 | * @param string $shipper |
||
| 124 | * @param array<string> $carrierIds |
||
| 125 | * |
||
| 126 | * @return array<array<array<string,float|string>>> |
||
| 127 | * |
||
| 128 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 129 | */ |
||
| 130 | 22 | public function trackPackages(string $shipper, array $carrierIds): array |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Tracks a package, get the last info |
||
| 143 | * |
||
| 144 | * @param string $shipper |
||
| 145 | * @param string $carrierId |
||
| 146 | * |
||
| 147 | * @return array<string,float|string|null> |
||
| 148 | * |
||
| 149 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 150 | */ |
||
| 151 | 7 | public function trackPackageLastStatus(string $shipper, string $carrierId): array |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Tracks a package, get the last info |
||
| 158 | * |
||
| 159 | * @param string $shipper |
||
| 160 | * @param array<string> $carrierIds |
||
| 161 | * |
||
| 162 | * @return array<array<string,float|string|null>> |
||
| 163 | * |
||
| 164 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 165 | */ |
||
| 166 | 20 | public function trackPackagesLastStatus(string $shipper, array $carrierIds): array |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Returns packages from the front (not ordered) for given shipper |
||
| 185 | * |
||
| 186 | * @param string $shipper |
||
| 187 | * |
||
| 188 | * @return array<array<string,int|string>> |
||
| 189 | * |
||
| 190 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 191 | */ |
||
| 192 | 7 | public function getOverview(string $shipper): array |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Gets labels |
||
| 199 | * |
||
| 200 | * @param string $shipper |
||
| 201 | * @param array<string> $packageIds |
||
| 202 | * |
||
| 203 | * @return string |
||
| 204 | * |
||
| 205 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 206 | */ |
||
| 207 | 9 | public function getLabels(string $shipper, array $packageIds): string |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Gets complete information about a package by its package ID |
||
| 216 | * |
||
| 217 | * @param string $shipper |
||
| 218 | * @param string $packageId |
||
| 219 | * |
||
| 220 | * @return array<string,int|string> |
||
| 221 | * |
||
| 222 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 223 | */ |
||
| 224 | 4 | public function getPackageInfo(string $shipper, string $packageId): array |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Gets complete information about a package by its carrier ID |
||
| 231 | * |
||
| 232 | * @param string $shipper |
||
| 233 | * @param string $carrierId |
||
| 234 | * |
||
| 235 | * @return array<string,int|string> |
||
| 236 | * |
||
| 237 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 238 | */ |
||
| 239 | 8 | public function getPackageInfoByCarrierId(string $shipper, string $carrierId): array |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Order shipment for packages |
||
| 252 | * |
||
| 253 | * @param string $shipper |
||
| 254 | * @param array<string> $packageIds |
||
| 255 | * |
||
| 256 | * @return array<string,int|string> |
||
| 257 | * |
||
| 258 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 259 | */ |
||
| 260 | 10 | public function orderShipment(string $shipper, array $packageIds): array |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Get order details |
||
| 269 | * |
||
| 270 | * @param string $shipper |
||
| 271 | * @param string $orderId |
||
| 272 | * |
||
| 273 | * @return array<string,int|string|array> |
||
| 274 | * |
||
| 275 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 276 | */ |
||
| 277 | 9 | public function getOrder(string $shipper, string $orderId): array |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Order pickup for packages |
||
| 286 | * |
||
| 287 | * @param string $shipper |
||
| 288 | * @param \DateTime $dateFrom |
||
| 289 | * @param \DateTime $dateTo |
||
| 290 | * @param float $weight |
||
| 291 | * @param int $packageCount |
||
| 292 | * @param string|null $message |
||
| 293 | * |
||
| 294 | * @return void |
||
| 295 | * |
||
| 296 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 297 | */ |
||
| 298 | 4 | public function orderPickup( |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Returns available services for the given shipper |
||
| 318 | * |
||
| 319 | * @param string $shipper |
||
| 320 | * |
||
| 321 | * @return array<string,string> |
||
| 322 | * |
||
| 323 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 324 | */ |
||
| 325 | 13 | public function getServices(string $shipper): array |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Returns available B2A services for the given shipper |
||
| 334 | * |
||
| 335 | * @param string $shipper |
||
| 336 | * |
||
| 337 | * @return array<string,string> |
||
| 338 | * |
||
| 339 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 340 | */ |
||
| 341 | 9 | public function getB2AServices(string $shipper): array |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Returns all manipulation units for the given shipper |
||
| 350 | * |
||
| 351 | * @param string $shipper |
||
| 352 | * @param bool $fullData |
||
| 353 | * |
||
| 354 | * @return array<string,string|array> |
||
| 355 | * |
||
| 356 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 357 | */ |
||
| 358 | 12 | public function getManipulationUnits(string $shipper, bool $fullData = false): array |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Returns available manipulation units for the given shipper |
||
| 371 | * |
||
| 372 | * @param string $shipper |
||
| 373 | * @param bool $fullData |
||
| 374 | * |
||
| 375 | * @return array<string,string|array> |
||
| 376 | * |
||
| 377 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 378 | */ |
||
| 379 | 12 | public function getActivatedManipulationUnits(string $shipper, bool $fullData = false): array |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Returns available branches for the given shipper and its service |
||
| 392 | * Full branches instead branches request |
||
| 393 | * |
||
| 394 | * @param string $shipper |
||
| 395 | * @param string|null $service |
||
| 396 | * @param bool $fullBranchesRequest |
||
| 397 | * @param string|null $country |
||
| 398 | * |
||
| 399 | * @return array<array<string,mixed>> |
||
| 400 | * |
||
| 401 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 402 | */ |
||
| 403 | 18 | public function getBranches( |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Returns available branches for the given shipper in given location |
||
| 426 | * |
||
| 427 | * @param string $shipper |
||
| 428 | * @param string $country |
||
| 429 | * @param string $city |
||
| 430 | * @param string|null $postcode |
||
| 431 | * @param string|null $street |
||
| 432 | * @param int|null $maxResults |
||
| 433 | * @param float|null $radius |
||
| 434 | * @param string|null $type |
||
| 435 | * |
||
| 436 | * @return array<array<string,mixed>> |
||
| 437 | * |
||
| 438 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 439 | */ |
||
| 440 | 11 | public function getBranchesForLocation( |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Returns list of countries where service with cash-on-delivery payment type is available in |
||
| 472 | * |
||
| 473 | * @param string $shipper |
||
| 474 | * |
||
| 475 | * @return array<array<int|string,array<string,array>>> |
||
| 476 | * |
||
| 477 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 478 | */ |
||
| 479 | 9 | public function getCodCountries(string $shipper): array |
|
| 489 | |||
| 490 | /** |
||
| 491 | * Returns list of countries where service is available in |
||
| 492 | * |
||
| 493 | * @param string $shipper |
||
| 494 | * |
||
| 495 | * @return array<array<int|string,string>> |
||
| 496 | * |
||
| 497 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 498 | */ |
||
| 499 | 9 | public function getCountries(string $shipper): array |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Returns available branches for the given shipper and its service |
||
| 512 | * |
||
| 513 | * @param string $shipper |
||
| 514 | * @param string $service |
||
| 515 | * @param string|null $country |
||
| 516 | * |
||
| 517 | * @return array<array<string,mixed>> |
||
| 518 | * |
||
| 519 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 520 | */ |
||
| 521 | 13 | public function getPostCodes(string $shipper, string $service, string $country = null): array |
|
| 527 | |||
| 528 | /** |
||
| 529 | * Check package(s) data |
||
| 530 | * |
||
| 531 | * @param string $shipper |
||
| 532 | * @param array<array<string>> $packages |
||
| 533 | * |
||
| 534 | * @return void |
||
| 535 | * |
||
| 536 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 537 | */ |
||
| 538 | 7 | public function checkPackages(string $shipper, array $packages): void |
|
| 542 | |||
| 543 | /** |
||
| 544 | * Returns available manipulation units for the given shipper |
||
| 545 | * |
||
| 546 | * @param string $shipper |
||
| 547 | * @param bool $fullData |
||
| 548 | * |
||
| 549 | * @return array<string> |
||
| 550 | * |
||
| 551 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 552 | */ |
||
| 553 | 12 | public function getAdrUnits(string $shipper, bool $fullData = false): array |
|
| 563 | |||
| 564 | /** |
||
| 565 | * Returns available activated services for the given shipper |
||
| 566 | * |
||
| 567 | * @param string $shipper |
||
| 568 | * |
||
| 569 | * @return array<string,mixed> |
||
| 570 | * |
||
| 571 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 572 | */ |
||
| 573 | 7 | public function getActivatedServices(string $shipper): array |
|
| 579 | |||
| 580 | /** |
||
| 581 | * Order shipments from place B (typically supplier / previous consignee) to place A (shipping point) |
||
| 582 | * |
||
| 583 | * @param string $shipper |
||
| 584 | * @param array<array<string,mixed>> $packages |
||
| 585 | * |
||
| 586 | * @return array<array<string,mixed>> |
||
| 587 | * |
||
| 588 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 589 | */ |
||
| 590 | 13 | public function orderB2AShipment(string $shipper, array $packages): array |
|
| 602 | |||
| 603 | /** |
||
| 604 | * Get PDF link with signed consignment delivery document by the recipient |
||
| 605 | * |
||
| 606 | * @param string $shipper |
||
| 607 | * @param string $carrierId |
||
| 608 | * |
||
| 609 | * @return string |
||
| 610 | * |
||
| 611 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 612 | */ |
||
| 613 | 7 | public function getProofOfDelivery(string $shipper, string $carrierId): string |
|
| 617 | |||
| 618 | /** |
||
| 619 | * Get array of PDF links with signed consignment delivery document by the recipient |
||
| 620 | * |
||
| 621 | * @param string $shipper |
||
| 622 | * @param array<string> $carrierIds |
||
| 623 | * |
||
| 624 | * @return array<string> |
||
| 625 | * |
||
| 626 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 627 | */ |
||
| 628 | 21 | public function getProofOfDeliveries(string $shipper, array $carrierIds): array |
|
| 644 | |||
| 645 | /** |
||
| 646 | * Obtain the price of carriage at consignment level |
||
| 647 | * |
||
| 648 | * @param string $shipper |
||
| 649 | * @param array<array<string,mixed>> $packages |
||
| 650 | * |
||
| 651 | * @return array<array<string,mixed>> |
||
| 652 | * |
||
| 653 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 654 | */ |
||
| 655 | 12 | public function getTransportCosts(string $shipper, array $packages): array |
|
| 667 | |||
| 668 | /** |
||
| 669 | * Get information on individual countries of the world |
||
| 670 | * |
||
| 671 | * @return array<array<string,mixed>> |
||
| 672 | * |
||
| 673 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 674 | */ |
||
| 675 | 9 | public function getCountriesData(): array |
|
| 681 | |||
| 682 | /** |
||
| 683 | * Method for obtaining news in the Balikobot API |
||
| 684 | * |
||
| 685 | * @return array<string,mixed> |
||
| 686 | * |
||
| 687 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 688 | */ |
||
| 689 | 4 | public function getChangelog(): array |
|
| 695 | } |
||
| 696 |