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 |
||
7 | class Invoice extends Base implements InvoiceInterface |
||
8 | { |
||
9 | const STATUS_DRAFT = 1; |
||
10 | const STATUS_SENT = 2; |
||
11 | const STATUS_VIEWED = 3; |
||
12 | const STATUS_APPROVED = 4; |
||
13 | const STATUS_PARTIAL = 5; |
||
14 | const STATUS_PAID = 6; |
||
15 | const STATUS_OVERDUE = -1; |
||
16 | const STATUS_UNPAID = -2; |
||
17 | |||
18 | const TYPE_STANDARD = 1; |
||
19 | const TYPE_QUOTE = 2; |
||
20 | |||
21 | const FREQUENCY_WEEKLY = 1; |
||
22 | const FREQUENCY_TWO_WEEKS = 2; |
||
23 | const FREQUENCY_FOUR_WEEKS = 3; |
||
24 | const FREQUENCY_MONTHLY = 4; |
||
25 | const FREQUENCY_TWO_MONTHS = 5; |
||
26 | const FREQUENCY_THREE_MONTHS = 6; |
||
27 | const FREQUENCY_SIX_MONTHS = 7; |
||
28 | const FREQUENCY_ANNUALLY = 8; |
||
29 | |||
30 | const INVOICE_NR = 'invoice_number'; |
||
31 | |||
32 | /** @var double */ |
||
33 | private $amount; |
||
34 | /** @var double */ |
||
35 | private $balance; |
||
36 | /** @var int */ |
||
37 | private $clientId; |
||
38 | /** @var int */ |
||
39 | private $invoiceStatusId; |
||
40 | /** @var string */ |
||
41 | private $invoiceNumber; |
||
42 | /** @var double */ |
||
43 | private $discount; |
||
44 | /** @var string */ |
||
45 | private $poNumber; |
||
46 | /** @var string */ |
||
47 | private $invoiceDate; |
||
48 | /** @var string */ |
||
49 | private $dueDate; |
||
50 | /** @var string */ |
||
51 | private $terms; |
||
52 | /** @var string */ |
||
53 | private $publicNotes; |
||
54 | /** @var int */ |
||
55 | private $invoiceTypeId; |
||
56 | /** @var bool */ |
||
57 | private $isRecurring; |
||
58 | /** @var int */ |
||
59 | private $frequencyId; |
||
60 | /** @var string */ |
||
61 | private $startDate; |
||
62 | /** @var string */ |
||
63 | private $endDate; |
||
64 | /** @var string */ |
||
65 | private $lastSentDate; |
||
66 | /** @var int */ |
||
67 | private $recurringInvoiceId; |
||
68 | /** @var string */ |
||
69 | private $taxName1; |
||
70 | /** @var double */ |
||
71 | private $taxRate1; |
||
72 | /** @var string */ |
||
73 | private $taxName2; |
||
74 | /** @var double */ |
||
75 | private $taxRate2; |
||
76 | /** @var bool */ |
||
77 | private $isAmountDiscount; |
||
78 | /** @var string */ |
||
79 | private $invoiceFooter; |
||
80 | /** @var double */ |
||
81 | private $partial; |
||
82 | /** @var bool */ |
||
83 | private $hasTasks; |
||
84 | /** @var bool */ |
||
85 | private $autoBill; |
||
86 | /** @var int */ |
||
87 | private $customValue1; |
||
88 | /** @var int */ |
||
89 | private $customValue2; |
||
90 | /** @var bool */ |
||
91 | private $customTaxes1; |
||
92 | /** @var bool */ |
||
93 | private $customTaxes2; |
||
94 | /** @var bool */ |
||
95 | private $hasExpenses; |
||
96 | /** @var int */ |
||
97 | private $quoteInvoiceId; |
||
98 | /** @var string */ |
||
99 | private $customTextValue1; |
||
100 | /** @var string */ |
||
101 | private $customTextValue2; |
||
102 | /** @var bool */ |
||
103 | private $isQuote; |
||
104 | /** @var bool */ |
||
105 | private $isPublic; |
||
106 | /** @var InvoiceItem[] */ |
||
107 | private $invoiceItems; |
||
108 | |||
109 | /** |
||
110 | * @return float |
||
111 | */ |
||
112 | public function getAmount() |
||
116 | |||
117 | /** |
||
118 | * @param float $amount |
||
119 | */ |
||
120 | public function setAmount($amount) |
||
124 | |||
125 | /** |
||
126 | * @return float |
||
127 | */ |
||
128 | public function getBalance() |
||
132 | |||
133 | /** |
||
134 | * @param float $balance |
||
135 | */ |
||
136 | public function setBalance($balance) |
||
140 | |||
141 | /** |
||
142 | * @return int |
||
143 | */ |
||
144 | public function getClientId() |
||
148 | |||
149 | /** |
||
150 | * @param int $clientId |
||
151 | */ |
||
152 | public function setClientId($clientId) |
||
156 | |||
157 | /** |
||
158 | * @return int |
||
159 | */ |
||
160 | public function getInvoiceStatusId() |
||
164 | |||
165 | /** |
||
166 | * @param int $invoiceStatusId |
||
167 | */ |
||
168 | public function setInvoiceStatusId($invoiceStatusId) |
||
172 | |||
173 | /** |
||
174 | * @return string |
||
175 | */ |
||
176 | public function getInvoiceNumber() |
||
180 | |||
181 | /** |
||
182 | * @param string $invoiceNumber |
||
183 | */ |
||
184 | public function setInvoiceNumber($invoiceNumber) |
||
188 | |||
189 | /** |
||
190 | * @return float |
||
191 | */ |
||
192 | public function getDiscount() |
||
196 | |||
197 | /** |
||
198 | * @param float $discount |
||
199 | */ |
||
200 | public function setDiscount($discount) |
||
204 | |||
205 | /** |
||
206 | * @return string |
||
207 | */ |
||
208 | public function getPoNumber() |
||
212 | |||
213 | /** |
||
214 | * @param string $poNumber |
||
215 | */ |
||
216 | public function setPoNumber($poNumber) |
||
220 | |||
221 | /** |
||
222 | * @return string |
||
223 | */ |
||
224 | public function getInvoiceDate() |
||
228 | |||
229 | /** |
||
230 | * @param string $invoiceDate |
||
231 | */ |
||
232 | public function setInvoiceDate($invoiceDate) |
||
236 | |||
237 | /** |
||
238 | * @return string |
||
239 | */ |
||
240 | public function getDueDate() |
||
244 | |||
245 | /** |
||
246 | * @param string $dueDate |
||
247 | */ |
||
248 | public function setDueDate($dueDate) |
||
252 | |||
253 | /** |
||
254 | * @return string |
||
255 | */ |
||
256 | public function getTerms() |
||
260 | |||
261 | /** |
||
262 | * @param string $terms |
||
263 | */ |
||
264 | public function setTerms($terms) |
||
268 | |||
269 | /** |
||
270 | * @return string |
||
271 | */ |
||
272 | public function getPublicNotes() |
||
276 | |||
277 | /** |
||
278 | * @param string $publicNotes |
||
279 | */ |
||
280 | public function setPublicNotes($publicNotes) |
||
284 | |||
285 | /** |
||
286 | * @return int |
||
287 | */ |
||
288 | public function getInvoiceTypeId() |
||
292 | |||
293 | /** |
||
294 | * @param int $invoiceTypeId |
||
295 | */ |
||
296 | public function setInvoiceTypeId($invoiceTypeId) |
||
300 | |||
301 | /** |
||
302 | * @return bool |
||
303 | */ |
||
304 | public function isRecurring() |
||
308 | |||
309 | /** |
||
310 | * @param bool $isRecurring |
||
311 | */ |
||
312 | public function setRecurring($isRecurring) |
||
316 | |||
317 | /** |
||
318 | * @return int |
||
319 | */ |
||
320 | public function getFrequencyId() |
||
324 | |||
325 | /** |
||
326 | * @param int $frequencyId |
||
327 | */ |
||
328 | public function setFrequencyId($frequencyId) |
||
332 | |||
333 | /** |
||
334 | * @return string |
||
335 | */ |
||
336 | public function getStartDate() |
||
340 | |||
341 | /** |
||
342 | * @param string $startDate |
||
343 | */ |
||
344 | public function setStartDate($startDate) |
||
348 | |||
349 | /** |
||
350 | * @return string |
||
351 | */ |
||
352 | public function getEndDate() |
||
356 | |||
357 | /** |
||
358 | * @param string $endDate |
||
359 | */ |
||
360 | public function setEndDate($endDate) |
||
364 | |||
365 | /** |
||
366 | * @return string |
||
367 | */ |
||
368 | public function getLastSentDate() |
||
372 | |||
373 | /** |
||
374 | * @param string $lastSentDate |
||
375 | */ |
||
376 | public function setLastSentDate($lastSentDate) |
||
380 | |||
381 | /** |
||
382 | * @return int |
||
383 | */ |
||
384 | public function getRecurringInvoiceId() |
||
388 | |||
389 | /** |
||
390 | * @param int $recurringInvoiceId |
||
391 | */ |
||
392 | public function setRecurringInvoiceId($recurringInvoiceId) |
||
396 | |||
397 | /** |
||
398 | * @return string |
||
399 | */ |
||
400 | public function getTaxName1() |
||
404 | |||
405 | /** |
||
406 | * @param string $taxName1 |
||
407 | */ |
||
408 | public function setTaxName1($taxName1) |
||
412 | |||
413 | /** |
||
414 | * @return float |
||
415 | */ |
||
416 | public function getTaxRate1() |
||
420 | |||
421 | /** |
||
422 | * @param float $taxRate1 |
||
423 | */ |
||
424 | public function setTaxRate1($taxRate1) |
||
428 | |||
429 | /** |
||
430 | * @return string |
||
431 | */ |
||
432 | public function getTaxName2() |
||
436 | |||
437 | /** |
||
438 | * @param string $taxName2 |
||
439 | */ |
||
440 | public function setTaxName2($taxName2) |
||
444 | |||
445 | /** |
||
446 | * @return float |
||
447 | */ |
||
448 | public function getTaxRate2() |
||
452 | |||
453 | /** |
||
454 | * @param float $taxRate2 |
||
455 | */ |
||
456 | public function setTaxRate2($taxRate2) |
||
460 | |||
461 | /** |
||
462 | * @return bool |
||
463 | */ |
||
464 | public function isAmountDiscount() |
||
468 | |||
469 | /** |
||
470 | * @param bool $isAmountDiscount |
||
471 | */ |
||
472 | public function setAmountDiscount($isAmountDiscount) |
||
476 | |||
477 | /** |
||
478 | * @return string |
||
479 | */ |
||
480 | public function getInvoiceFooter() |
||
484 | |||
485 | /** |
||
486 | * @param string $invoiceFooter |
||
487 | */ |
||
488 | public function setInvoiceFooter($invoiceFooter) |
||
492 | |||
493 | /** |
||
494 | * @return float |
||
495 | */ |
||
496 | public function getPartial() |
||
500 | |||
501 | /** |
||
502 | * @param float $partial |
||
503 | */ |
||
504 | public function setPartial($partial) |
||
508 | |||
509 | /** |
||
510 | * @return bool |
||
511 | */ |
||
512 | public function hasTasks() |
||
516 | |||
517 | /** |
||
518 | * @param bool $hasTasks |
||
519 | */ |
||
520 | public function setHasTasks($hasTasks) |
||
524 | |||
525 | /** |
||
526 | * @return bool |
||
527 | */ |
||
528 | public function isAutoBill() |
||
532 | |||
533 | /** |
||
534 | * @param bool $autoBill |
||
535 | */ |
||
536 | public function setAutoBill($autoBill) |
||
540 | |||
541 | /** |
||
542 | * @return int |
||
543 | */ |
||
544 | public function getCustomValue1() |
||
548 | |||
549 | /** |
||
550 | * @param int $customValue1 |
||
551 | */ |
||
552 | public function setCustomValue1($customValue1) |
||
556 | |||
557 | /** |
||
558 | * @return int |
||
559 | */ |
||
560 | public function getCustomValue2() |
||
564 | |||
565 | /** |
||
566 | * @param int $customValue2 |
||
567 | */ |
||
568 | public function setCustomValue2($customValue2) |
||
572 | |||
573 | /** |
||
574 | * @return bool |
||
575 | */ |
||
576 | public function isCustomTaxes1() |
||
580 | |||
581 | /** |
||
582 | * @param bool $customTaxes1 |
||
583 | */ |
||
584 | public function setCustomTaxes1($customTaxes1) |
||
588 | |||
589 | /** |
||
590 | * @return bool |
||
591 | */ |
||
592 | public function isCustomTaxes2() |
||
596 | |||
597 | /** |
||
598 | * @param bool $customTaxes2 |
||
599 | */ |
||
600 | public function setCustomTaxes2($customTaxes2) |
||
604 | |||
605 | /** |
||
606 | * @return bool |
||
607 | */ |
||
608 | public function hasExpenses() |
||
612 | |||
613 | /** |
||
614 | * @param bool $hasExpenses |
||
615 | */ |
||
616 | public function setHasExpenses($hasExpenses) |
||
620 | |||
621 | /** |
||
622 | * @return int |
||
623 | */ |
||
624 | public function getQuoteInvoiceId() |
||
628 | |||
629 | /** |
||
630 | * @param int $quoteInvoiceId |
||
631 | */ |
||
632 | public function setQuoteInvoiceId($quoteInvoiceId) |
||
636 | |||
637 | /** |
||
638 | * @return string |
||
639 | */ |
||
640 | public function getCustomTextValue1() |
||
644 | |||
645 | /** |
||
646 | * @param string $customTextValue1 |
||
647 | */ |
||
648 | public function setCustomTextValue1($customTextValue1) |
||
652 | |||
653 | /** |
||
654 | * @return string |
||
655 | */ |
||
656 | public function getCustomTextValue2() |
||
660 | |||
661 | /** |
||
662 | * @param string $customTextValue2 |
||
663 | */ |
||
664 | public function setCustomTextValue2($customTextValue2) |
||
668 | |||
669 | /** |
||
670 | * @return bool |
||
671 | */ |
||
672 | public function isQuote() |
||
676 | |||
677 | /** |
||
678 | * @param bool $isQuote |
||
679 | */ |
||
680 | public function setQuote($isQuote) |
||
684 | |||
685 | /** |
||
686 | * @return bool |
||
687 | */ |
||
688 | public function isPublic() |
||
692 | |||
693 | /** |
||
694 | * @param bool $isPublic |
||
695 | */ |
||
696 | public function setPublic($isPublic) |
||
700 | |||
701 | /** |
||
702 | * @return InvoiceItem[] |
||
703 | */ |
||
704 | public function getInvoiceItems() |
||
708 | |||
709 | /** |
||
710 | * @param InvoiceItem[] $invoiceItems |
||
711 | */ |
||
712 | public function setInvoiceItems($invoiceItems) |
||
716 | } |
||
717 |