Complex classes like Balikobot 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 Balikobot, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Balikobot |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Balikobot API |
||
| 24 | * |
||
| 25 | * @var \Inspirum\Balikobot\Services\Client |
||
| 26 | */ |
||
| 27 | private $client; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Balikobot constructor |
||
| 31 | * |
||
| 32 | * @param \Inspirum\Balikobot\Contracts\RequesterInterface $requester |
||
| 33 | */ |
||
| 34 | 115 | public function __construct(RequesterInterface $requester) |
|
| 38 | |||
| 39 | /** |
||
| 40 | * All supported shipper services |
||
| 41 | * |
||
| 42 | * @return array<string> |
||
| 43 | */ |
||
| 44 | 1 | public function getShippers(): array |
|
| 48 | |||
| 49 | /** |
||
| 50 | * Add packages |
||
| 51 | * |
||
| 52 | * @param \Inspirum\Balikobot\Model\Aggregates\PackageCollection $packages |
||
| 53 | * |
||
| 54 | * @return \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection|\Inspirum\Balikobot\Model\Values\OrderedPackage[] |
||
| 55 | * |
||
| 56 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 57 | */ |
||
| 58 | 11 | public function addPackages(PackageCollection $packages): OrderedPackageCollection |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Exports order into Balikobot system |
||
| 75 | * |
||
| 76 | * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages |
||
| 77 | * |
||
| 78 | * @return void |
||
| 79 | * |
||
| 80 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 81 | */ |
||
| 82 | 3 | public function dropPackages(OrderedPackageCollection $packages): void |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Exports order into Balikobot system |
||
| 89 | * |
||
| 90 | * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package |
||
| 91 | * |
||
| 92 | * @return void |
||
| 93 | * |
||
| 94 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 95 | */ |
||
| 96 | 1 | public function dropPackage(OrderedPackage $package): void |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Order shipment |
||
| 103 | * |
||
| 104 | * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages |
||
| 105 | * |
||
| 106 | * @return \Inspirum\Balikobot\Model\Values\OrderedShipment |
||
| 107 | * |
||
| 108 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 109 | */ |
||
| 110 | 5 | public function orderShipment(OrderedPackageCollection $packages): OrderedShipment |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Track package |
||
| 119 | * |
||
| 120 | * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package |
||
| 121 | * |
||
| 122 | * @return array<\Inspirum\Balikobot\Model\Values\PackageStatus>|\Inspirum\Balikobot\Model\Values\PackageStatus[] |
||
| 123 | * |
||
| 124 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 125 | */ |
||
| 126 | 3 | public function trackPackage(OrderedPackage $package): array |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Track packages |
||
| 136 | * |
||
| 137 | * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages |
||
| 138 | * |
||
| 139 | * @return array<array<\Inspirum\Balikobot\Model\Values\PackageStatus>>|\Inspirum\Balikobot\Model\Values\PackageStatus[][] |
||
| 140 | * |
||
| 141 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 142 | */ |
||
| 143 | 6 | public function trackPackages(OrderedPackageCollection $packages): array |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Track package last status |
||
| 158 | * |
||
| 159 | * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package |
||
| 160 | * |
||
| 161 | * @return \Inspirum\Balikobot\Model\Values\PackageStatus |
||
| 162 | * |
||
| 163 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 164 | */ |
||
| 165 | 2 | public function trackPackageLastStatus(OrderedPackage $package): PackageStatus |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Track package last status |
||
| 175 | * |
||
| 176 | * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages |
||
| 177 | * |
||
| 178 | * @return array<\Inspirum\Balikobot\Model\Values\PackageStatus>|\Inspirum\Balikobot\Model\Values\PackageStatus[] |
||
| 179 | * |
||
| 180 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 181 | */ |
||
| 182 | 5 | public function trackPackagesLastStatus(OrderedPackageCollection $packages): array |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Create package statuses collection for package |
||
| 191 | * |
||
| 192 | * @param array<array<string,float|string|null>> $response |
||
| 193 | * |
||
| 194 | * @return array<\Inspirum\Balikobot\Model\Values\PackageStatus> |
||
| 195 | */ |
||
| 196 | 10 | private function createPackageStatusesCollection(array $response): array |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Get overview for given shipper |
||
| 209 | * |
||
| 210 | * @param string $shipper |
||
| 211 | * |
||
| 212 | * @return \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection|\Inspirum\Balikobot\Model\Values\OrderedPackage[] |
||
| 213 | * |
||
| 214 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 215 | */ |
||
| 216 | 3 | public function getOverview(string $shipper): OrderedPackageCollection |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Get labels for orders |
||
| 234 | * |
||
| 235 | * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages |
||
| 236 | * |
||
| 237 | * @return string |
||
| 238 | * |
||
| 239 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 240 | */ |
||
| 241 | 4 | public function getLabels(OrderedPackageCollection $packages): string |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Gets complete information about a package |
||
| 248 | * |
||
| 249 | * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package |
||
| 250 | * |
||
| 251 | * @return \Inspirum\Balikobot\Model\Values\Package |
||
| 252 | * |
||
| 253 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 254 | */ |
||
| 255 | 4 | public function getPackageInfo(OrderedPackage $package): Package |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Gets complete information about a package |
||
| 277 | * |
||
| 278 | * @param string $shipper |
||
| 279 | * @param string $orderId |
||
| 280 | * |
||
| 281 | * @return \Inspirum\Balikobot\Model\Values\OrderedShipment |
||
| 282 | * |
||
| 283 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 284 | */ |
||
| 285 | 4 | public function getOrder(string $shipper, string $orderId): OrderedShipment |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Returns available services for the given shipper |
||
| 294 | * |
||
| 295 | * @param string $shipper |
||
| 296 | * |
||
| 297 | * @return array<string,string> |
||
| 298 | * |
||
| 299 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 300 | */ |
||
| 301 | 6 | public function getServices(string $shipper): array |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Returns available B2A services for the given shipper |
||
| 308 | * |
||
| 309 | * @param string $shipper |
||
| 310 | * |
||
| 311 | * @return array<string,string> |
||
| 312 | * |
||
| 313 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 314 | */ |
||
| 315 | 3 | public function getB2AServices(string $shipper): array |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Returns all manipulation units for the given shipper |
||
| 322 | * |
||
| 323 | * @param string $shipper |
||
| 324 | * @param bool $fullData |
||
| 325 | * |
||
| 326 | * @return array<string|array> |
||
| 327 | * |
||
| 328 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 329 | */ |
||
| 330 | 5 | public function getManipulationUnits(string $shipper, bool $fullData = false): array |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Returns available manipulation units for the given shipper |
||
| 337 | * |
||
| 338 | * @param string $shipper |
||
| 339 | * @param bool $fullData |
||
| 340 | * |
||
| 341 | * @return array<string|array> |
||
| 342 | * |
||
| 343 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 344 | */ |
||
| 345 | 5 | public function getActivatedManipulationUnits(string $shipper, bool $fullData = false): array |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Get all available branches |
||
| 352 | * |
||
| 353 | * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[] |
||
| 354 | * |
||
| 355 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 356 | */ |
||
| 357 | 1 | public function getBranches(): iterable |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Get all available branches for countries |
||
| 366 | * |
||
| 367 | * @param array<string> $countries |
||
| 368 | * |
||
| 369 | * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[] |
||
| 370 | * |
||
| 371 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 372 | */ |
||
| 373 | 1 | public function getBranchesForCountries(array $countries): iterable |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Get all available branches for given shipper |
||
| 382 | * |
||
| 383 | * @param string $shipper |
||
| 384 | * |
||
| 385 | * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[] |
||
| 386 | * |
||
| 387 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 388 | */ |
||
| 389 | 2 | public function getBranchesForShipper(string $shipper): iterable |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Get all available branches for given shipper for countries |
||
| 398 | * |
||
| 399 | * @param string $shipper |
||
| 400 | * @param array<string> $countries |
||
| 401 | * |
||
| 402 | * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[] |
||
| 403 | * |
||
| 404 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 405 | */ |
||
| 406 | 2 | public function getBranchesForShipperForCountries(string $shipper, array $countries): iterable |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Get services for shipper |
||
| 415 | * |
||
| 416 | * @param string $shipper |
||
| 417 | * |
||
| 418 | * @return array<string|null> |
||
| 419 | * |
||
| 420 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 421 | */ |
||
| 422 | 4 | private function getServicesForShipper(string $shipper): array |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Get all available branches for given shipper and service type for countries |
||
| 429 | * |
||
| 430 | * @param string $shipper |
||
| 431 | * @param string|null $service |
||
| 432 | * @param array<string> $countries |
||
| 433 | * |
||
| 434 | * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[] |
||
| 435 | * |
||
| 436 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 437 | */ |
||
| 438 | 3 | public function getBranchesForShipperServiceForCountries( |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Get all available branches for given shipper and service type filtered by countries if possible |
||
| 452 | * |
||
| 453 | * @param string $shipper |
||
| 454 | * @param string|null $service |
||
| 455 | * @param array<string> $countries |
||
| 456 | * |
||
| 457 | * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[] |
||
| 458 | * |
||
| 459 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 460 | */ |
||
| 461 | 3 | private function getAllBranchesForShipperServiceForCountries( |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Get all available branches for given shipper and service type |
||
| 477 | * |
||
| 478 | * @param string $shipper |
||
| 479 | * @param string|null $service |
||
| 480 | * @param string|null $country |
||
| 481 | * |
||
| 482 | * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[] |
||
| 483 | * |
||
| 484 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 485 | */ |
||
| 486 | 7 | public function getBranchesForShipperService(string $shipper, ?string $service, string $country = null): iterable |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Get all available branches for given shipper |
||
| 497 | * |
||
| 498 | * @param string $shipper |
||
| 499 | * @param string $country |
||
| 500 | * @param string $city |
||
| 501 | * @param string|null $postcode |
||
| 502 | * @param string|null $street |
||
| 503 | * @param int|null $maxResults |
||
| 504 | * @param float|null $radius |
||
| 505 | * @param string|null $type |
||
| 506 | * |
||
| 507 | * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[] |
||
| 508 | * |
||
| 509 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 510 | */ |
||
| 511 | 5 | public function getBranchesForLocation( |
|
| 536 | |||
| 537 | /** |
||
| 538 | * Returns list of countries where service with cash-on-delivery payment type is available in |
||
| 539 | * |
||
| 540 | * @param string $shipper |
||
| 541 | * |
||
| 542 | * @return array<array<int|string,array<string,array>>> |
||
| 543 | * |
||
| 544 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 545 | */ |
||
| 546 | 3 | public function getCodCountries(string $shipper): array |
|
| 550 | |||
| 551 | /** |
||
| 552 | * Returns list of countries where service is available in |
||
| 553 | * |
||
| 554 | * @param string $shipper |
||
| 555 | * |
||
| 556 | * @return array<array<int|string,string>> |
||
| 557 | * |
||
| 558 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 559 | */ |
||
| 560 | 3 | public function getCountries(string $shipper): array |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Returns available branches for the given shipper and its service |
||
| 567 | * |
||
| 568 | * @param string $shipper |
||
| 569 | * @param string $service |
||
| 570 | * @param string|null $country |
||
| 571 | * |
||
| 572 | * @return \Generator|\Inspirum\Balikobot\Model\Values\PostCode[] |
||
| 573 | * |
||
| 574 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 575 | */ |
||
| 576 | 3 | public function getPostCodes(string $shipper, string $service, string $country = null): iterable |
|
| 582 | |||
| 583 | /** |
||
| 584 | * Check package(s) data |
||
| 585 | * |
||
| 586 | * @param \Inspirum\Balikobot\Model\Aggregates\PackageCollection $packages |
||
| 587 | * |
||
| 588 | * @return void |
||
| 589 | * |
||
| 590 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 591 | */ |
||
| 592 | 3 | public function checkPackages(PackageCollection $packages): void |
|
| 596 | |||
| 597 | /** |
||
| 598 | * Returns available manipulation units for the given shipper |
||
| 599 | * |
||
| 600 | * @param string $shipper |
||
| 601 | * @param bool $fullData |
||
| 602 | * |
||
| 603 | * @return array<string|array> |
||
| 604 | * |
||
| 605 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 606 | */ |
||
| 607 | 5 | public function getAdrUnits(string $shipper, bool $fullData = false): array |
|
| 611 | |||
| 612 | /** |
||
| 613 | * Returns available activated services for the given shipper |
||
| 614 | * |
||
| 615 | * @param string $shipper |
||
| 616 | * |
||
| 617 | * @return array<string,mixed> |
||
| 618 | * |
||
| 619 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 620 | */ |
||
| 621 | 3 | public function getActivatedServices(string $shipper): array |
|
| 625 | |||
| 626 | /** |
||
| 627 | * Order shipments from place B (typically supplier / previous consignee) to place A (shipping point) |
||
| 628 | * |
||
| 629 | * @param \Inspirum\Balikobot\Model\Aggregates\PackageCollection $packages |
||
| 630 | * |
||
| 631 | * @return \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection|\Inspirum\Balikobot\Model\Values\OrderedPackage[] |
||
| 632 | * |
||
| 633 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 634 | */ |
||
| 635 | 5 | public function orderB2AShipment(PackageCollection $packages): OrderedPackageCollection |
|
| 649 | |||
| 650 | /** |
||
| 651 | * Get PDF link with signed consignment delivery document by the recipient |
||
| 652 | * |
||
| 653 | * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package |
||
| 654 | * |
||
| 655 | * @return string |
||
| 656 | * |
||
| 657 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 658 | */ |
||
| 659 | 2 | public function getProofOfDelivery(OrderedPackage $package): string |
|
| 666 | |||
| 667 | /** |
||
| 668 | * Get array of PDF links with signed consignment delivery document by the recipient |
||
| 669 | * |
||
| 670 | * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages |
||
| 671 | * |
||
| 672 | * @return array<string> |
||
| 673 | * |
||
| 674 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 675 | */ |
||
| 676 | 6 | public function getProofOfDeliveries(OrderedPackageCollection $packages): array |
|
| 680 | |||
| 681 | /** |
||
| 682 | * Obtain the price of carriage at consignment level |
||
| 683 | * |
||
| 684 | * @param \Inspirum\Balikobot\Model\Aggregates\PackageCollection $packages |
||
| 685 | * |
||
| 686 | * @return \Inspirum\Balikobot\Model\Aggregates\PackageTransportCostCollection|\Inspirum\Balikobot\Model\Values\PackageTransportCost[] |
||
| 687 | * |
||
| 688 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 689 | */ |
||
| 690 | 4 | public function getTransportCosts(PackageCollection $packages): PackageTransportCostCollection |
|
| 703 | |||
| 704 | /** |
||
| 705 | * Get information on individual countries of the world |
||
| 706 | * |
||
| 707 | * @return array<string,\Inspirum\Balikobot\Model\Values\Country>|\Inspirum\Balikobot\Model\Values\Country[] |
||
| 708 | * |
||
| 709 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 710 | */ |
||
| 711 | 3 | public function getCountriesData(): array |
|
| 723 | |||
| 724 | /** |
||
| 725 | * Method for obtaining news in the Balikobot API |
||
| 726 | * |
||
| 727 | * @return array<string,mixed> |
||
| 728 | * |
||
| 729 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 730 | */ |
||
| 731 | public function getChangelog(): array |
||
| 735 | } |
||
| 736 |