Complex classes like Invoice 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 Invoice, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Invoice implements CreatableFromArray |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | private $id; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var int |
||
| 22 | */ |
||
| 23 | private $invoiceNo; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var int |
||
| 27 | */ |
||
| 28 | private $ocrNumber; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var Customer |
||
| 32 | */ |
||
| 33 | private $customer; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var \Billogram\Model\Invoice\Item[] |
||
| 37 | */ |
||
| 38 | private $items; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var |
||
| 42 | */ |
||
| 43 | private $invoiceDate; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var \DateTime |
||
| 47 | */ |
||
| 48 | private $dueDate; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var int |
||
| 52 | */ |
||
| 53 | private $dueDays; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var int |
||
| 57 | */ |
||
| 58 | private $invoiceFee; |
||
| 59 | /** |
||
| 60 | * @var int |
||
| 61 | */ |
||
| 62 | private $invoiceFeeVat; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var int |
||
| 66 | */ |
||
| 67 | private $reminderFee; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var int |
||
| 71 | */ |
||
| 72 | private $interestRate; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var int |
||
| 76 | */ |
||
| 77 | private $interestFee; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | private $currency; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var AdditionalInformation |
||
| 86 | */ |
||
| 87 | private $info; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var RegionalInformation |
||
| 91 | */ |
||
| 92 | private $regionalSweden; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | private $deliveryMethod; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | private $state; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var string |
||
| 106 | */ |
||
| 107 | private $url; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var array |
||
| 111 | */ |
||
| 112 | private $flags; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var EventData |
||
| 116 | */ |
||
| 117 | private $events; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var int |
||
| 121 | */ |
||
| 122 | private $remainingSum; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var int |
||
| 126 | */ |
||
| 127 | private $totalSum; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var int |
||
| 131 | */ |
||
| 132 | private $roundingValue; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @var bool |
||
| 136 | */ |
||
| 137 | private $automaticReminders = true; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @var AutomaticReminder[] |
||
| 141 | */ |
||
| 142 | private $automaticRemindersSettings; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @var int |
||
| 146 | */ |
||
| 147 | private $reminderCount; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @var \DateTime |
||
| 151 | */ |
||
| 152 | private $createdAt; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @var \DateTime |
||
| 156 | */ |
||
| 157 | private $attestedAt; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @var \DateTime |
||
| 161 | */ |
||
| 162 | private $updateAt; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @var BillogramCallback |
||
| 166 | */ |
||
| 167 | private $callbacks; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @var DetailedSums |
||
| 171 | */ |
||
| 172 | private $detailedSums; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @var string |
||
| 176 | */ |
||
| 177 | private $attachment; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @var ReminderCollection |
||
| 181 | */ |
||
| 182 | private $automaticCollection; |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | public function getId(): string |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param string $id |
||
| 194 | * |
||
| 195 | * @return Invoice |
||
| 196 | */ |
||
| 197 | public function withId(string $id) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @return int |
||
| 207 | */ |
||
| 208 | public function getInvoiceNo(): int |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param int $invoiceNo |
||
| 215 | * |
||
| 216 | * @return Invoice |
||
| 217 | */ |
||
| 218 | public function withInvoiceNo(int $invoiceNo) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @return int |
||
| 228 | */ |
||
| 229 | public function getOcrNumber(): int |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param int $ocrNumber |
||
| 236 | * |
||
| 237 | * @return Invoice |
||
| 238 | */ |
||
| 239 | public function withOcrNumber(int $ocrNumber) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @return Customer |
||
| 249 | */ |
||
| 250 | public function getCustomer(): Customer |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param Customer $customer |
||
| 257 | * |
||
| 258 | * @return Invoice |
||
| 259 | */ |
||
| 260 | 2 | public function withCustomer(Customer $customer) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * @return \Billogram\Model\Invoice\Item[] |
||
| 270 | */ |
||
| 271 | public function getItems(): array |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param \Billogram\Model\Invoice\Item[] $items |
||
| 278 | * |
||
| 279 | * @return Invoice |
||
| 280 | */ |
||
| 281 | 2 | public function withItems(array $items) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * @return int |
||
| 291 | */ |
||
| 292 | public function getInvoiceDate(): int |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param string $invoiceDate |
||
| 299 | * |
||
| 300 | * @return Invoice |
||
| 301 | */ |
||
| 302 | 2 | public function withInvoiceDate(string $invoiceDate) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * @return \DateTime |
||
| 312 | */ |
||
| 313 | public function getDueDate(): \DateTime |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param \DateTime $dueDate |
||
| 320 | * |
||
| 321 | * @return Invoice |
||
| 322 | */ |
||
| 323 | public function withDueDate(\DateTime $dueDate) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @return int |
||
| 333 | */ |
||
| 334 | public function getDueDays(): int |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param int $dueDays |
||
| 341 | * |
||
| 342 | * @return Invoice |
||
| 343 | */ |
||
| 344 | public function withDueDays(int $dueDays) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @return int |
||
| 354 | */ |
||
| 355 | public function getInvoiceFee(): int |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @param int $invoiceFee |
||
| 362 | * |
||
| 363 | * @return Invoice |
||
| 364 | */ |
||
| 365 | public function withInvoiceFee(int $invoiceFee) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @return int |
||
| 375 | */ |
||
| 376 | public function getInvoiceFeeVat(): int |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param int $invoiceFeeVat |
||
| 383 | * |
||
| 384 | * @return Invoice |
||
| 385 | */ |
||
| 386 | public function withInvoiceFeeVat(int $invoiceFeeVat) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @return int |
||
| 396 | */ |
||
| 397 | public function getReminderFee(): int |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @param int $reminderFee |
||
| 404 | * |
||
| 405 | * @return Invoice |
||
| 406 | */ |
||
| 407 | public function withReminderFee(int $reminderFee) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @return int |
||
| 417 | */ |
||
| 418 | public function getInterestRate(): int |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @param int $interestRate |
||
| 425 | * |
||
| 426 | * @return Invoice |
||
| 427 | */ |
||
| 428 | public function withInterestRate(int $interestRate) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @return int |
||
| 438 | */ |
||
| 439 | public function getInterestFee(): int |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param int $interestFee |
||
| 446 | * |
||
| 447 | * @return Invoice |
||
| 448 | */ |
||
| 449 | public function withInterestFee(int $interestFee) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @return string |
||
| 459 | */ |
||
| 460 | public function getCurrency(): string |
||
| 464 | |||
| 465 | /** |
||
| 466 | * @param string $currency |
||
| 467 | * |
||
| 468 | * @return Invoice |
||
| 469 | */ |
||
| 470 | public function withCurrency(string $currency) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @return AdditionalInformation |
||
| 480 | */ |
||
| 481 | public function getInfo(): AdditionalInformation |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @param AdditionalInformation $info |
||
| 488 | * |
||
| 489 | * @return Invoice |
||
| 490 | */ |
||
| 491 | public function withInfo(AdditionalInformation $info) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @return RegionalInformation |
||
| 501 | */ |
||
| 502 | public function getRegionalSweden(): RegionalInformation |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @param RegionalInformation $regionalSweden |
||
| 509 | * |
||
| 510 | * @return Invoice |
||
| 511 | */ |
||
| 512 | public function withRegionalSweden(RegionalInformation $regionalSweden) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @return string |
||
| 522 | */ |
||
| 523 | public function getDeliveryMethod(): string |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @param string $deliveryMethod |
||
| 530 | * |
||
| 531 | * @return Invoice |
||
| 532 | */ |
||
| 533 | public function withDeliveryMethod(string $deliveryMethod) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @return string |
||
| 543 | */ |
||
| 544 | public function getState(): string |
||
| 548 | |||
| 549 | /** |
||
| 550 | * @param string $state |
||
| 551 | * |
||
| 552 | * @return Invoice |
||
| 553 | */ |
||
| 554 | public function swithState(string $state) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * @return string |
||
| 564 | */ |
||
| 565 | public function getUrl(): string |
||
| 569 | |||
| 570 | /** |
||
| 571 | * @param string $url |
||
| 572 | * |
||
| 573 | * @return Invoice |
||
| 574 | */ |
||
| 575 | public function withUrl(string $url) |
||
| 582 | |||
| 583 | /** |
||
| 584 | * @return array |
||
| 585 | */ |
||
| 586 | public function getFlags(): array |
||
| 590 | |||
| 591 | /** |
||
| 592 | * @param array $flags |
||
| 593 | * |
||
| 594 | * @return Invoice |
||
| 595 | */ |
||
| 596 | public function withFlags(array $flags) |
||
| 603 | |||
| 604 | /** |
||
| 605 | * @return EventData |
||
| 606 | */ |
||
| 607 | public function getEvents(): EventData |
||
| 611 | |||
| 612 | /** |
||
| 613 | * @param EventData $events |
||
| 614 | * |
||
| 615 | * @return Invoice |
||
| 616 | */ |
||
| 617 | public function withEvents(EventData $events) |
||
| 624 | |||
| 625 | /** |
||
| 626 | * @return int |
||
| 627 | */ |
||
| 628 | public function getRemainingSum(): int |
||
| 632 | |||
| 633 | /** |
||
| 634 | * @param int $remainingSum |
||
| 635 | * |
||
| 636 | * @return Invoice |
||
| 637 | */ |
||
| 638 | public function withRemainingSum(int $remainingSum) |
||
| 645 | |||
| 646 | /** |
||
| 647 | * @return int |
||
| 648 | */ |
||
| 649 | public function getTotalSum(): int |
||
| 653 | |||
| 654 | /** |
||
| 655 | * @param int $totalSum |
||
| 656 | * |
||
| 657 | * @return Invoice |
||
| 658 | */ |
||
| 659 | public function withTotalSum(int $totalSum) |
||
| 666 | |||
| 667 | /** |
||
| 668 | * @return bool |
||
| 669 | */ |
||
| 670 | public function isAutomaticReminders(): bool |
||
| 674 | |||
| 675 | /** |
||
| 676 | * @param bool $automaticReminders |
||
| 677 | * |
||
| 678 | * @return Invoice |
||
| 679 | */ |
||
| 680 | public function withAutomaticReminders(bool $automaticReminders) |
||
| 687 | |||
| 688 | /** |
||
| 689 | * @return AutomaticReminder[] |
||
| 690 | */ |
||
| 691 | public function getAutomaticRemindersSettings(): array |
||
| 695 | |||
| 696 | /** |
||
| 697 | * @param AutomaticReminder[] $automaticRemindersSettings |
||
| 698 | * |
||
| 699 | * @return Invoice |
||
| 700 | */ |
||
| 701 | public function withAutomaticRemindersSettings(array $automaticRemindersSettings) |
||
| 708 | |||
| 709 | /** |
||
| 710 | * @return int |
||
| 711 | */ |
||
| 712 | public function getReminderCount(): int |
||
| 716 | |||
| 717 | /** |
||
| 718 | * @param int $reminderCount |
||
| 719 | * |
||
| 720 | * @return Invoice |
||
| 721 | */ |
||
| 722 | public function withReminderCount(int $reminderCount) |
||
| 729 | |||
| 730 | /** |
||
| 731 | * @return \DateTime |
||
| 732 | */ |
||
| 733 | public function getCreatedAt(): \DateTime |
||
| 737 | |||
| 738 | /** |
||
| 739 | * @param \DateTime $createdAt |
||
| 740 | * |
||
| 741 | * @return Invoice |
||
| 742 | */ |
||
| 743 | public function withCreatedAt(\DateTime $createdAt) |
||
| 750 | |||
| 751 | /** |
||
| 752 | * @return \DateTime |
||
| 753 | */ |
||
| 754 | public function getAttestedAt(): \DateTime |
||
| 758 | |||
| 759 | /** |
||
| 760 | * @param \DateTime $attestedAt |
||
| 761 | * |
||
| 762 | * @return Invoice |
||
| 763 | */ |
||
| 764 | public function withAttestedAt(\DateTime $attestedAt) |
||
| 771 | |||
| 772 | /** |
||
| 773 | * @return \DateTime |
||
| 774 | */ |
||
| 775 | public function getUpdateAt(): \DateTime |
||
| 779 | |||
| 780 | /** |
||
| 781 | * @param \DateTime $updateAt |
||
| 782 | * |
||
| 783 | * @return Invoice |
||
| 784 | */ |
||
| 785 | public function withUpdateAt(\DateTime $updateAt) |
||
| 792 | |||
| 793 | /** |
||
| 794 | * @return BillogramCallback |
||
| 795 | */ |
||
| 796 | public function getCallbacks(): BillogramCallback |
||
| 800 | |||
| 801 | /** |
||
| 802 | * @param BillogramCallback $callbacks |
||
| 803 | * |
||
| 804 | * @return Invoice |
||
| 805 | */ |
||
| 806 | public function withCallbacks(BillogramCallback $callbacks) |
||
| 813 | |||
| 814 | /** |
||
| 815 | * @return DetailedSums |
||
| 816 | */ |
||
| 817 | public function getDetailedSums(): DetailedSums |
||
| 821 | |||
| 822 | /** |
||
| 823 | * @param DetailedSums $detailedSums |
||
| 824 | * |
||
| 825 | * @return Invoice |
||
| 826 | */ |
||
| 827 | public function withDetailedSums(DetailedSums $detailedSums) |
||
| 834 | |||
| 835 | /** |
||
| 836 | * @return string |
||
| 837 | */ |
||
| 838 | public function getAttachment(): string |
||
| 842 | |||
| 843 | /** |
||
| 844 | * @param string $attachment |
||
| 845 | * |
||
| 846 | * @return Invoice |
||
| 847 | */ |
||
| 848 | public function withAttachment(string $attachment) |
||
| 855 | |||
| 856 | /** |
||
| 857 | * @return ReminderCollection |
||
| 858 | */ |
||
| 859 | public function getAutomaticCollection(): ReminderCollection |
||
| 863 | |||
| 864 | /** |
||
| 865 | * @param ReminderCollection $automaticCollection |
||
| 866 | * |
||
| 867 | * @return Invoice |
||
| 868 | */ |
||
| 869 | public function withAutomaticCollection(ReminderCollection $automaticCollection) |
||
| 876 | |||
| 877 | /** |
||
| 878 | * @return int |
||
| 879 | */ |
||
| 880 | public function getRoundingValue(): int |
||
| 884 | |||
| 885 | 2 | public function toArray() |
|
| 903 | |||
| 904 | /** |
||
| 905 | * Create an API response object from the HTTP response from the API server. |
||
| 906 | * |
||
| 907 | * @param array $data |
||
| 908 | * |
||
| 909 | * @return self |
||
| 910 | */ |
||
| 911 | 4 | public static function createFromArray(array $data) |
|
| 975 | } |
||
| 976 |