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 |
||
| 12 | final class Invoice extends Base implements InvoiceInterface |
||
| 13 | { |
||
| 14 | /** @var float */ |
||
| 15 | private $amount = 0; |
||
| 16 | /** @var float */ |
||
| 17 | private $balance = 0; |
||
| 18 | /** @var int */ |
||
| 19 | private $clientId = 0; |
||
| 20 | /** @var int */ |
||
| 21 | private $invoiceStatusId = InvoiceInterface::STATUS_DRAFT; |
||
| 22 | /** @var string */ |
||
| 23 | private $invoiceNumber = ''; |
||
| 24 | /** @var float */ |
||
| 25 | private $discount = 0; |
||
| 26 | /** @var string */ |
||
| 27 | private $poNumber = ''; |
||
| 28 | /** @var string */ |
||
| 29 | private $invoiceDate = ''; |
||
| 30 | /** @var string */ |
||
| 31 | private $dueDate = ''; |
||
| 32 | /** @var string */ |
||
| 33 | private $terms = ''; |
||
| 34 | /** @var string */ |
||
| 35 | private $publicNotes = ''; |
||
| 36 | /** @var int */ |
||
| 37 | private $invoiceTypeId = 0; |
||
| 38 | /** @var bool */ |
||
| 39 | private $isRecurring = false; |
||
| 40 | /** @var int */ |
||
| 41 | private $frequencyId = 0; |
||
| 42 | /** @var string */ |
||
| 43 | private $startDate; |
||
| 44 | /** @var string */ |
||
| 45 | private $endDate; |
||
| 46 | /** @var string */ |
||
| 47 | private $lastSentDate; |
||
| 48 | /** @var int */ |
||
| 49 | private $recurringInvoiceId = 0; |
||
| 50 | /** @var string */ |
||
| 51 | private $taxName1 = ''; |
||
| 52 | /** @var float */ |
||
| 53 | private $taxRate1 = 0; |
||
| 54 | /** @var string */ |
||
| 55 | private $taxName2 = ''; |
||
| 56 | /** @var float */ |
||
| 57 | private $taxRate2 = 0; |
||
| 58 | /** @var bool */ |
||
| 59 | private $isAmountDiscount; |
||
| 60 | /** @var string */ |
||
| 61 | private $invoiceFooter = ''; |
||
| 62 | /** @var float */ |
||
| 63 | private $partial = 0; |
||
| 64 | /** @var bool */ |
||
| 65 | private $hasTasks = false; |
||
| 66 | /** @var bool */ |
||
| 67 | private $autoBill = false; |
||
| 68 | /** @var int */ |
||
| 69 | private $customValue1; |
||
| 70 | /** @var int */ |
||
| 71 | private $customValue2; |
||
| 72 | /** @var bool */ |
||
| 73 | private $customTaxes1 = false; |
||
| 74 | /** @var bool */ |
||
| 75 | private $customTaxes2 = false; |
||
| 76 | /** @var bool */ |
||
| 77 | private $hasExpenses = false; |
||
| 78 | /** @var int */ |
||
| 79 | private $quoteInvoiceId = 0; |
||
| 80 | /** @var string */ |
||
| 81 | private $customTextValue1; |
||
| 82 | /** @var string */ |
||
| 83 | private $customTextValue2; |
||
| 84 | /** @var bool */ |
||
| 85 | private $isQuote = false; |
||
| 86 | /** @var bool */ |
||
| 87 | private $isPublic = false; |
||
| 88 | /** @var InvoiceItem[] */ |
||
| 89 | private $invoiceItems; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @return float |
||
| 93 | */ |
||
| 94 | public function getAmount() : float |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param float $amount |
||
| 101 | */ |
||
| 102 | public function setAmount(float $amount) : void |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return float |
||
| 109 | */ |
||
| 110 | public function getBalance() : float |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param float $balance |
||
| 117 | */ |
||
| 118 | public function setBalance(float $balance) : void |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return int |
||
| 125 | */ |
||
| 126 | public function getClientId() : int |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param int $clientId |
||
| 133 | */ |
||
| 134 | public function setClientId(int $clientId) : void |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return int |
||
| 141 | */ |
||
| 142 | public function getInvoiceStatusId() : int |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param int $invoiceStatusId |
||
| 149 | */ |
||
| 150 | public function setInvoiceStatusId(int $invoiceStatusId) : void |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | public function getInvoiceNumber() : string |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param string $invoiceNumber |
||
| 165 | */ |
||
| 166 | public function setInvoiceNumber(string $invoiceNumber) : void |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @return float |
||
| 173 | */ |
||
| 174 | public function getDiscount() : float |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param float $discount |
||
| 181 | */ |
||
| 182 | public function setDiscount(float $discount) : void |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function getPoNumber() : string |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param string $poNumber |
||
| 197 | */ |
||
| 198 | public function setPoNumber(string $poNumber) : void |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return \DateTime |
||
| 205 | */ |
||
| 206 | public function getInvoiceDate() : \DateTime |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param \DateTime $invoiceDate |
||
| 213 | */ |
||
| 214 | public function setInvoiceDate(\DateTime $invoiceDate) : void |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | public function getDueDate() : string |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param string $dueDate |
||
| 229 | */ |
||
| 230 | public function setDueDate(string $dueDate) : void |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | public function getTerms() : string |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param string $terms |
||
| 245 | */ |
||
| 246 | public function setTerms(string $terms) : void |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | public function getPublicNotes() : string |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param string $publicNotes |
||
| 261 | */ |
||
| 262 | public function setPublicNotes(string $publicNotes) : void |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return int |
||
| 269 | */ |
||
| 270 | public function getInvoiceTypeId() : int |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param int $invoiceTypeId |
||
| 277 | */ |
||
| 278 | public function setInvoiceTypeId(int $invoiceTypeId) : void |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return bool |
||
| 285 | */ |
||
| 286 | public function isRecurring() : bool |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param bool $isRecurring |
||
| 293 | */ |
||
| 294 | public function setRecurring(bool $isRecurring) : void |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @return int |
||
| 301 | */ |
||
| 302 | public function getFrequencyId() : int |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param int $frequencyId |
||
| 309 | */ |
||
| 310 | public function setFrequencyId(int $frequencyId) : void |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | public function getStartDate() : string |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param string $startDate |
||
| 325 | */ |
||
| 326 | public function setStartDate(string $startDate) : void |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @return string |
||
| 333 | */ |
||
| 334 | public function getEndDate() : string |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param string $endDate |
||
| 341 | */ |
||
| 342 | public function setEndDate(string $endDate) : void |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | public function getLastSentDate() : string |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param string $lastSentDate |
||
| 357 | */ |
||
| 358 | public function setLastSentDate(string $lastSentDate) : void |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @return int |
||
| 365 | */ |
||
| 366 | public function getRecurringInvoiceId() : int |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @param int $recurringInvoiceId |
||
| 373 | */ |
||
| 374 | public function setRecurringInvoiceId(int $recurringInvoiceId) : void |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @return string |
||
| 381 | */ |
||
| 382 | public function getTaxName1() : string |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @param string $taxName1 |
||
| 389 | */ |
||
| 390 | public function setTaxName1(string $taxName1) : void |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @return float |
||
| 397 | */ |
||
| 398 | public function getTaxRate1() : float |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @param float $taxRate1 |
||
| 405 | */ |
||
| 406 | public function setTaxRate1(float $taxRate1) : void |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @return string |
||
| 413 | */ |
||
| 414 | public function getTaxName2() : string |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @param string $taxName2 |
||
| 421 | */ |
||
| 422 | public function setTaxName2(string $taxName2) : void |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @return float |
||
| 429 | */ |
||
| 430 | public function getTaxRate2() : float |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @param float $taxRate2 |
||
| 437 | */ |
||
| 438 | public function setTaxRate2(float $taxRate2) : void |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @return bool |
||
| 445 | */ |
||
| 446 | public function isAmountDiscount() : bool |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @param bool $isAmountDiscount |
||
| 453 | */ |
||
| 454 | public function setAmountDiscount(bool $isAmountDiscount) : void |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @return string |
||
| 461 | */ |
||
| 462 | public function getInvoiceFooter() : string |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @param string $invoiceFooter |
||
| 469 | */ |
||
| 470 | public function setInvoiceFooter(string $invoiceFooter) : void |
||
| 474 | |||
| 475 | /** |
||
| 476 | * @return float |
||
| 477 | */ |
||
| 478 | public function getPartial() : float |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @param float $partial |
||
| 485 | */ |
||
| 486 | public function setPartial(float $partial) : void |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @return bool |
||
| 493 | */ |
||
| 494 | public function hasTasks() : bool |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @param bool $hasTasks |
||
| 501 | */ |
||
| 502 | public function setHasTasks(bool $hasTasks) : void |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @return bool |
||
| 509 | */ |
||
| 510 | public function isAutoBill() : bool |
||
| 514 | |||
| 515 | /** |
||
| 516 | * @param bool $autoBill |
||
| 517 | */ |
||
| 518 | public function setAutoBill(bool $autoBill) : void |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @return int |
||
| 525 | */ |
||
| 526 | public function getCustomValue1() : int |
||
| 530 | |||
| 531 | /** |
||
| 532 | * @param int $customValue1 |
||
| 533 | */ |
||
| 534 | public function setCustomValue1(int $customValue1) : void |
||
| 538 | |||
| 539 | /** |
||
| 540 | * @return int |
||
| 541 | */ |
||
| 542 | public function getCustomValue2() : int |
||
| 546 | |||
| 547 | /** |
||
| 548 | * @param int $customValue2 |
||
| 549 | */ |
||
| 550 | public function setCustomValue2(int $customValue2) : void |
||
| 554 | |||
| 555 | /** |
||
| 556 | * @return bool |
||
| 557 | */ |
||
| 558 | public function isCustomTaxes1() : bool |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @param bool $customTaxes1 |
||
| 565 | */ |
||
| 566 | public function setCustomTaxes1(bool $customTaxes1) : void |
||
| 570 | |||
| 571 | /** |
||
| 572 | * @return bool |
||
| 573 | */ |
||
| 574 | public function isCustomTaxes2() : bool |
||
| 578 | |||
| 579 | /** |
||
| 580 | * @param bool $customTaxes2 |
||
| 581 | */ |
||
| 582 | public function setCustomTaxes2(bool $customTaxes2) : void |
||
| 586 | |||
| 587 | /** |
||
| 588 | * @return bool |
||
| 589 | */ |
||
| 590 | public function hasExpenses() : bool |
||
| 594 | |||
| 595 | /** |
||
| 596 | * @param bool $hasExpenses |
||
| 597 | */ |
||
| 598 | public function setHasExpenses(bool $hasExpenses) : void |
||
| 602 | |||
| 603 | /** |
||
| 604 | * @return int |
||
| 605 | */ |
||
| 606 | public function getQuoteInvoiceId() : int |
||
| 610 | |||
| 611 | /** |
||
| 612 | * @param int $quoteInvoiceId |
||
| 613 | */ |
||
| 614 | public function setQuoteInvoiceId(int $quoteInvoiceId) : void |
||
| 618 | |||
| 619 | /** |
||
| 620 | * @return string |
||
| 621 | */ |
||
| 622 | public function getCustomTextValue1() : string |
||
| 626 | |||
| 627 | /** |
||
| 628 | * @param string $customTextValue1 |
||
| 629 | */ |
||
| 630 | public function setCustomTextValue1(string $customTextValue1) : void |
||
| 634 | |||
| 635 | /** |
||
| 636 | * @return string |
||
| 637 | */ |
||
| 638 | public function getCustomTextValue2() : string |
||
| 642 | |||
| 643 | /** |
||
| 644 | * @param string $customTextValue2 |
||
| 645 | */ |
||
| 646 | public function setCustomTextValue2(string $customTextValue2) : void |
||
| 650 | |||
| 651 | /** |
||
| 652 | * @return bool |
||
| 653 | */ |
||
| 654 | public function isQuote() : bool |
||
| 658 | |||
| 659 | /** |
||
| 660 | * @param bool $isQuote |
||
| 661 | */ |
||
| 662 | public function setQuote(bool $isQuote) : void |
||
| 666 | |||
| 667 | /** |
||
| 668 | * @return bool |
||
| 669 | */ |
||
| 670 | public function isPublic() : bool |
||
| 674 | |||
| 675 | /** |
||
| 676 | * @param bool $isPublic |
||
| 677 | */ |
||
| 678 | public function setPublic(bool $isPublic) : void |
||
| 682 | |||
| 683 | /** |
||
| 684 | * @return InvoiceItem[] |
||
| 685 | */ |
||
| 686 | public function getInvoiceItems() : array |
||
| 690 | |||
| 691 | /** |
||
| 692 | * @param InvoiceItem[] $invoiceItems |
||
| 693 | */ |
||
| 694 | public function setInvoiceItems(array $invoiceItems) : void |
||
| 698 | } |
||
| 699 |