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 |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Track package |
||
| 123 | * |
||
| 124 | * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package |
||
| 125 | * |
||
| 126 | * @return array<\Inspirum\Balikobot\Model\Values\PackageStatus>|\Inspirum\Balikobot\Model\Values\PackageStatus[] |
||
| 127 | * |
||
| 128 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 129 | */ |
||
| 130 | 3 | public function trackPackage(OrderedPackage $package): array |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Track packages |
||
| 140 | * |
||
| 141 | * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages |
||
| 142 | * |
||
| 143 | * @return array<array<\Inspirum\Balikobot\Model\Values\PackageStatus>>|\Inspirum\Balikobot\Model\Values\PackageStatus[][] |
||
| 144 | * |
||
| 145 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 146 | */ |
||
| 147 | 6 | public function trackPackages(OrderedPackageCollection $packages): array |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Track package last status |
||
| 166 | * |
||
| 167 | * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package |
||
| 168 | * |
||
| 169 | * @return \Inspirum\Balikobot\Model\Values\PackageStatus |
||
| 170 | * |
||
| 171 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 172 | */ |
||
| 173 | 2 | public function trackPackageLastStatus(OrderedPackage $package): PackageStatus |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Track package last status |
||
| 183 | * |
||
| 184 | * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages |
||
| 185 | * |
||
| 186 | * @return array<\Inspirum\Balikobot\Model\Values\PackageStatus>|\Inspirum\Balikobot\Model\Values\PackageStatus[] |
||
| 187 | * |
||
| 188 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 189 | */ |
||
| 190 | 5 | public function trackPackagesLastStatus(OrderedPackageCollection $packages): array |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Get overview for given shipper |
||
| 205 | * |
||
| 206 | * @param string $shipper |
||
| 207 | * |
||
| 208 | * @return \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection|\Inspirum\Balikobot\Model\Values\OrderedPackage[] |
||
| 209 | * |
||
| 210 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 211 | */ |
||
| 212 | 3 | public function getOverview(string $shipper): OrderedPackageCollection |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Get labels for orders |
||
| 230 | * |
||
| 231 | * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages |
||
| 232 | * |
||
| 233 | * @return string |
||
| 234 | * |
||
| 235 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 236 | */ |
||
| 237 | 4 | public function getLabels(OrderedPackageCollection $packages): string |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Gets complete information about a package |
||
| 244 | * |
||
| 245 | * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package |
||
| 246 | * |
||
| 247 | * @return \Inspirum\Balikobot\Model\Values\Package |
||
| 248 | * |
||
| 249 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 250 | */ |
||
| 251 | 4 | public function getPackageInfo(OrderedPackage $package): Package |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Gets complete information about a package |
||
| 273 | * |
||
| 274 | * @param string $shipper |
||
| 275 | * @param string $orderId |
||
| 276 | * |
||
| 277 | * @return \Inspirum\Balikobot\Model\Values\OrderedShipment |
||
| 278 | * |
||
| 279 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 280 | */ |
||
| 281 | 4 | public function getOrder(string $shipper, string $orderId): OrderedShipment |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Returns available services for the given shipper |
||
| 290 | * |
||
| 291 | * @param string $shipper |
||
| 292 | * |
||
| 293 | * @return array<string,string> |
||
| 294 | * |
||
| 295 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 296 | */ |
||
| 297 | 6 | public function getServices(string $shipper): array |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Returns available B2A services for the given shipper |
||
| 304 | * |
||
| 305 | * @param string $shipper |
||
| 306 | * |
||
| 307 | * @return array<string,string> |
||
| 308 | * |
||
| 309 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 310 | */ |
||
| 311 | 3 | public function getB2AServices(string $shipper): array |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Returns all manipulation units for the given shipper |
||
| 318 | * |
||
| 319 | * @param string $shipper |
||
| 320 | * @param bool $fullData |
||
| 321 | * |
||
| 322 | * @return array<string|array> |
||
| 323 | * |
||
| 324 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 325 | */ |
||
| 326 | 5 | public function getManipulationUnits(string $shipper, bool $fullData = false): array |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Returns available manipulation units for the given shipper |
||
| 333 | * |
||
| 334 | * @param string $shipper |
||
| 335 | * @param bool $fullData |
||
| 336 | * |
||
| 337 | * @return array<string|array> |
||
| 338 | * |
||
| 339 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 340 | */ |
||
| 341 | 5 | public function getActivatedManipulationUnits(string $shipper, bool $fullData = false): array |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Get all available branches |
||
| 348 | * |
||
| 349 | * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[] |
||
| 350 | * |
||
| 351 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 352 | */ |
||
| 353 | 1 | public function getBranches(): iterable |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Get all available branches for countries |
||
| 362 | * |
||
| 363 | * @param array<string> $countries |
||
| 364 | * |
||
| 365 | * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[] |
||
| 366 | * |
||
| 367 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 368 | */ |
||
| 369 | 1 | public function getBranchesForCountries(array $countries): iterable |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Get all available branches for given shipper |
||
| 378 | * |
||
| 379 | * @param string $shipper |
||
| 380 | * |
||
| 381 | * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[] |
||
| 382 | * |
||
| 383 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 384 | */ |
||
| 385 | 2 | public function getBranchesForShipper(string $shipper): iterable |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Get all available branches for given shipper for countries |
||
| 394 | * |
||
| 395 | * @param string $shipper |
||
| 396 | * @param array<string> $countries |
||
| 397 | * |
||
| 398 | * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[] |
||
| 399 | * |
||
| 400 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 401 | */ |
||
| 402 | 2 | public function getBranchesForShipperForCountries(string $shipper, array $countries): iterable |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Get services for shipper |
||
| 411 | * |
||
| 412 | * @param string $shipper |
||
| 413 | * |
||
| 414 | * @return array<string|null> |
||
| 415 | * |
||
| 416 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 417 | */ |
||
| 418 | 4 | private function getServicesForShipper(string $shipper): array |
|
| 422 | |||
| 423 | /** |
||
| 424 | * Get all available branches for given shipper and service type for countries |
||
| 425 | * |
||
| 426 | * @param string $shipper |
||
| 427 | * @param string|null $service |
||
| 428 | * @param array<string> $countries |
||
| 429 | * |
||
| 430 | * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[] |
||
| 431 | * |
||
| 432 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 433 | */ |
||
| 434 | 3 | public function getBranchesForShipperServiceForCountries( |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Get all available branches for given shipper and service type filtered by countries if possible |
||
| 448 | * |
||
| 449 | * @param string $shipper |
||
| 450 | * @param string|null $service |
||
| 451 | * @param array<string> $countries |
||
| 452 | * |
||
| 453 | * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[] |
||
| 454 | * |
||
| 455 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 456 | */ |
||
| 457 | 3 | private function getAllBranchesForShipperServiceForCountries( |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Get all available branches for given shipper and service type |
||
| 473 | * |
||
| 474 | * @param string $shipper |
||
| 475 | * @param string|null $service |
||
| 476 | * @param string|null $country |
||
| 477 | * |
||
| 478 | * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[] |
||
| 479 | * |
||
| 480 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 481 | */ |
||
| 482 | 7 | public function getBranchesForShipperService(string $shipper, ?string $service, string $country = null): iterable |
|
| 490 | |||
| 491 | /** |
||
| 492 | * Get all available branches for given shipper |
||
| 493 | * |
||
| 494 | * @param string $shipper |
||
| 495 | * @param string $country |
||
| 496 | * @param string $city |
||
| 497 | * @param string|null $postcode |
||
| 498 | * @param string|null $street |
||
| 499 | * @param int|null $maxResults |
||
| 500 | * @param float|null $radius |
||
| 501 | * @param string|null $type |
||
| 502 | * |
||
| 503 | * @return \Generator|\Inspirum\Balikobot\Model\Values\Branch[] |
||
| 504 | * |
||
| 505 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 506 | */ |
||
| 507 | 5 | public function getBranchesForLocation( |
|
| 532 | |||
| 533 | /** |
||
| 534 | * Returns list of countries where service with cash-on-delivery payment type is available in |
||
| 535 | * |
||
| 536 | * @param string $shipper |
||
| 537 | * |
||
| 538 | * @return array<array<int|string,array<string,array>>> |
||
| 539 | * |
||
| 540 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 541 | */ |
||
| 542 | 3 | public function getCodCountries(string $shipper): array |
|
| 546 | |||
| 547 | /** |
||
| 548 | * Returns list of countries where service is available in |
||
| 549 | * |
||
| 550 | * @param string $shipper |
||
| 551 | * |
||
| 552 | * @return array<array<int|string,string>> |
||
| 553 | * |
||
| 554 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 555 | */ |
||
| 556 | 3 | public function getCountries(string $shipper): array |
|
| 560 | |||
| 561 | /** |
||
| 562 | * Returns available branches for the given shipper and its service |
||
| 563 | * |
||
| 564 | * @param string $shipper |
||
| 565 | * @param string $service |
||
| 566 | * @param string|null $country |
||
| 567 | * |
||
| 568 | * @return \Generator|\Inspirum\Balikobot\Model\Values\PostCode[] |
||
| 569 | * |
||
| 570 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 571 | */ |
||
| 572 | 3 | public function getPostCodes(string $shipper, string $service, string $country = null): iterable |
|
| 578 | |||
| 579 | /** |
||
| 580 | * Check package(s) data |
||
| 581 | * |
||
| 582 | * @param \Inspirum\Balikobot\Model\Aggregates\PackageCollection $packages |
||
| 583 | * |
||
| 584 | * @return void |
||
| 585 | * |
||
| 586 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 587 | */ |
||
| 588 | 3 | public function checkPackages(PackageCollection $packages): void |
|
| 592 | |||
| 593 | /** |
||
| 594 | * Returns available manipulation units for the given shipper |
||
| 595 | * |
||
| 596 | * @param string $shipper |
||
| 597 | * @param bool $fullData |
||
| 598 | * |
||
| 599 | * @return array<string|array> |
||
| 600 | * |
||
| 601 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 602 | */ |
||
| 603 | 5 | public function getAdrUnits(string $shipper, bool $fullData = false): array |
|
| 607 | |||
| 608 | /** |
||
| 609 | * Returns available activated services for the given shipper |
||
| 610 | * |
||
| 611 | * @param string $shipper |
||
| 612 | * |
||
| 613 | * @return array<string,mixed> |
||
| 614 | * |
||
| 615 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 616 | */ |
||
| 617 | 3 | public function getActivatedServices(string $shipper): array |
|
| 621 | |||
| 622 | /** |
||
| 623 | * Order shipments from place B (typically supplier / previous consignee) to place A (shipping point) |
||
| 624 | * |
||
| 625 | * @param \Inspirum\Balikobot\Model\Aggregates\PackageCollection $packages |
||
| 626 | * |
||
| 627 | * @return \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection|\Inspirum\Balikobot\Model\Values\OrderedPackage[] |
||
| 628 | * |
||
| 629 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 630 | */ |
||
| 631 | 5 | public function orderB2AShipment(PackageCollection $packages): OrderedPackageCollection |
|
| 645 | |||
| 646 | /** |
||
| 647 | * Get PDF link with signed consignment delivery document by the recipient |
||
| 648 | * |
||
| 649 | * @param \Inspirum\Balikobot\Model\Values\OrderedPackage $package |
||
| 650 | * |
||
| 651 | * @return string |
||
| 652 | * |
||
| 653 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 654 | */ |
||
| 655 | 2 | public function getProofOfDelivery(OrderedPackage $package): string |
|
| 662 | |||
| 663 | /** |
||
| 664 | * Get array of PDF links with signed consignment delivery document by the recipient |
||
| 665 | * |
||
| 666 | * @param \Inspirum\Balikobot\Model\Aggregates\OrderedPackageCollection $packages |
||
| 667 | * |
||
| 668 | * @return array<string> |
||
| 669 | * |
||
| 670 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 671 | */ |
||
| 672 | 6 | public function getProofOfDeliveries(OrderedPackageCollection $packages): array |
|
| 676 | |||
| 677 | /** |
||
| 678 | * Obtain the price of carriage at consignment level |
||
| 679 | * |
||
| 680 | * @param \Inspirum\Balikobot\Model\Aggregates\PackageCollection $packages |
||
| 681 | * |
||
| 682 | * @return \Inspirum\Balikobot\Model\Aggregates\PackageTransportCostCollection|\Inspirum\Balikobot\Model\Values\PackageTransportCost[] |
||
| 683 | * |
||
| 684 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 685 | */ |
||
| 686 | 4 | public function getTransportCosts(PackageCollection $packages): PackageTransportCostCollection |
|
| 699 | |||
| 700 | /** |
||
| 701 | * Get information on individual countries of the world |
||
| 702 | * |
||
| 703 | * @return array<string,\Inspirum\Balikobot\Model\Values\Country>|\Inspirum\Balikobot\Model\Values\Country[] |
||
| 704 | * |
||
| 705 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 706 | */ |
||
| 707 | 3 | public function getCountriesData(): array |
|
| 719 | |||
| 720 | /** |
||
| 721 | * Method for obtaining news in the Balikobot API |
||
| 722 | * |
||
| 723 | * @return array<string,mixed> |
||
| 724 | * |
||
| 725 | * @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
||
| 726 | */ |
||
| 727 | public function getChangelog(): array |
||
| 731 | } |
||
| 732 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: