Complex classes like StockMovement 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 StockMovement, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | abstract class StockMovement |
||
33 | { |
||
34 | use Blameable; |
||
35 | use Timestampable; |
||
36 | |||
37 | const TYPE_SALE = 'sale'; |
||
38 | const TYPE_RETURN = 'return'; |
||
39 | const TYPE_REGULATION = 'regulation'; |
||
40 | const TYPE_DELIVERY = 'delivery'; |
||
41 | |||
42 | /** |
||
43 | * @var int |
||
44 | * |
||
45 | * @ORM\Column(type="integer") |
||
46 | * @ORM\GeneratedValue |
||
47 | * @ORM\Id |
||
48 | */ |
||
49 | protected $id; |
||
50 | |||
51 | /** |
||
52 | * The number of items. |
||
53 | * |
||
54 | * If the quantity is negative it means an outgoing stock movement, i.e. you've sold a product |
||
55 | * Contrary a positive number means an ingoing stock movement, i.e. you had a return or a delivery |
||
56 | * |
||
57 | * @var int |
||
58 | * |
||
59 | * @FormAssert\NotBlank() |
||
60 | * @FormAssert\NotEqualTo(0) |
||
61 | * |
||
62 | * @ORM\Column(type="integer") |
||
63 | */ |
||
64 | protected $quantity; |
||
65 | |||
66 | /** |
||
67 | * Whether this stock movement is a complaint. |
||
68 | * |
||
69 | * @var bool |
||
70 | * |
||
71 | * @ORM\Column(name="complaint", type="boolean") |
||
72 | */ |
||
73 | protected $complaint; |
||
74 | |||
75 | /** |
||
76 | * A small text describing this stock movement. |
||
77 | * |
||
78 | * @var string |
||
79 | * |
||
80 | * @FormAssert\Length(max="191") |
||
81 | * |
||
82 | * @ORM\Column(name="reference", type="string", length=191) |
||
83 | */ |
||
84 | protected $reference; |
||
85 | |||
86 | /** |
||
87 | * A valid currency code. |
||
88 | * |
||
89 | * @var string |
||
90 | * |
||
91 | * @FormAssert\Currency() |
||
92 | * |
||
93 | * @ORM\Column(type="string", length=3) |
||
94 | */ |
||
95 | protected $currency; |
||
96 | |||
97 | /** |
||
98 | * This is the retail price of the product on the time when the stock movement was created |
||
99 | * The price is excl vat. |
||
100 | * |
||
101 | * @var int |
||
102 | * |
||
103 | * @FormAssert\NotBlank() |
||
104 | * @FormAssert\GreaterThanOrEqual(0) |
||
105 | * |
||
106 | * @ORM\Column(type="integer") |
||
107 | */ |
||
108 | protected $retailPrice; |
||
109 | |||
110 | /** |
||
111 | * Effectively this is `$quantity * $retailPrice`. |
||
112 | * |
||
113 | * The price is excl vat |
||
114 | * |
||
115 | * @var int |
||
116 | * |
||
117 | * @FormAssert\NotBlank() |
||
118 | * @FormAssert\GreaterThanOrEqual(0) |
||
119 | * |
||
120 | * @ORM\Column(type="integer") |
||
121 | */ |
||
122 | protected $totalRetailPrice; |
||
123 | |||
124 | /** |
||
125 | * This is the price excl vat. |
||
126 | * |
||
127 | * @var int |
||
128 | * |
||
129 | * @FormAssert\NotBlank() |
||
130 | * @FormAssert\GreaterThanOrEqual(0) |
||
131 | * |
||
132 | * @ORM\Column(type="integer") |
||
133 | */ |
||
134 | protected $price; |
||
135 | |||
136 | /** |
||
137 | * Effectively this is `$quantity * $price`. |
||
138 | * |
||
139 | * This is the total price excl vat |
||
140 | * |
||
141 | * @var int |
||
142 | * |
||
143 | * @FormAssert\NotBlank() |
||
144 | * @FormAssert\GreaterThanOrEqual(0) |
||
145 | * |
||
146 | * @ORM\Column(type="integer") |
||
147 | */ |
||
148 | protected $totalPrice; |
||
149 | |||
150 | /** |
||
151 | * This is the discount on this stock movement. |
||
152 | * |
||
153 | * Effectively this is `$retailPrice - $price` |
||
154 | * |
||
155 | * @var int |
||
156 | * |
||
157 | * @FormAssert\NotBlank() |
||
158 | * |
||
159 | * @ORM\Column(type="integer") |
||
160 | */ |
||
161 | protected $discount; |
||
162 | |||
163 | /** |
||
164 | * This is the total discount on this stock movement. |
||
165 | * |
||
166 | * Effectively this is `$totalRetailPrice - $totalPrice` |
||
167 | * |
||
168 | * @var int |
||
169 | * |
||
170 | * @FormAssert\NotBlank() |
||
171 | * |
||
172 | * @ORM\Column(type="integer") |
||
173 | */ |
||
174 | protected $totalDiscount; |
||
175 | |||
176 | /** |
||
177 | * This is the vat percentage. |
||
178 | * |
||
179 | * @var float |
||
180 | * |
||
181 | * @FormAssert\NotBlank() |
||
182 | * @FormAssert\GreaterThanOrEqual(0) |
||
183 | * |
||
184 | * @ORM\Column(type="decimal", precision=5, scale=2) |
||
185 | */ |
||
186 | protected $vatPercentage; |
||
187 | |||
188 | /** |
||
189 | * This is the type of the stock movement, i.e. 'sale', 'delivery', 'return' etc. |
||
190 | * |
||
191 | * @var string |
||
192 | * |
||
193 | * @FormAssert\Choice(callback="getTypes") |
||
194 | * |
||
195 | * @ORM\Column(type="string", length=191) |
||
196 | */ |
||
197 | protected $type; |
||
198 | |||
199 | /** |
||
200 | * This is the associated product. |
||
201 | * |
||
202 | * @var ProductInterface |
||
203 | * |
||
204 | * @FormAssert\NotBlank() |
||
205 | * |
||
206 | * @ORM\JoinColumn(nullable=false) |
||
207 | * @ORM\ManyToOne(targetEntity="Loevgaard\DandomainFoundation\Entity\Product") |
||
208 | */ |
||
209 | protected $product; |
||
210 | |||
211 | /** |
||
212 | * If the type equals 'sale' this will be the associated order line. |
||
213 | * |
||
214 | * @var OrderLineInterface|null |
||
215 | * |
||
216 | * @ORM\ManyToOne(targetEntity="Loevgaard\DandomainFoundation\Entity\OrderLine", inversedBy="stockMovements") |
||
217 | */ |
||
218 | protected $orderLine; |
||
219 | |||
220 | /** |
||
221 | * @var bool |
||
222 | * |
||
223 | * @ORM\Column(type="boolean") |
||
224 | */ |
||
225 | protected $orderLineRemoved; |
||
226 | |||
227 | 6 | public function __construct() |
|
232 | |||
233 | 3 | public function __call($name, $arguments) |
|
250 | |||
251 | /** |
||
252 | * @ORM\PrePersist() |
||
253 | * @ORM\PreUpdate() |
||
254 | */ |
||
255 | 1 | public function validate() |
|
288 | |||
289 | /** |
||
290 | * @param int $quantity |
||
291 | * @param Money $unitPrice |
||
292 | * @param float $vatPercent |
||
293 | * @param string $type |
||
294 | * @param ProductInterface $product |
||
295 | * @param string $reference |
||
296 | * |
||
297 | * @return StockMovement |
||
298 | * |
||
299 | * @throws CurrencyMismatchException |
||
300 | * @throws UndefinedPriceForCurrencyException |
||
301 | */ |
||
302 | public static function create(int $quantity, Money $unitPrice, float $vatPercent, string $type, ProductInterface $product, string $reference): self |
||
329 | |||
330 | /** |
||
331 | * @param OrderLineInterface $orderLine |
||
332 | * |
||
333 | * @throws CurrencyMismatchException |
||
334 | * @throws UndefinedPriceForCurrencyException |
||
335 | * @throws UnsetProductException |
||
336 | */ |
||
337 | public function populateFromOrderLine(OrderLineInterface $orderLine) |
||
370 | |||
371 | /** |
||
372 | * @return StockMovement |
||
373 | * |
||
374 | * @throws CurrencyMismatchException |
||
375 | * @throws UnsetCurrencyException |
||
376 | */ |
||
377 | 1 | public function copy(): self |
|
395 | |||
396 | /** |
||
397 | * @return StockMovement |
||
398 | * |
||
399 | * @throws CurrencyMismatchException |
||
400 | * @throws UnsetCurrencyException |
||
401 | */ |
||
402 | public function inverse(): self |
||
409 | |||
410 | /** |
||
411 | * @param StockMovement $stockMovement |
||
412 | * |
||
413 | * @return StockMovement |
||
414 | * |
||
415 | * @throws CurrencyMismatchException |
||
416 | * @throws StockMovementProductMismatchException |
||
417 | * @throws UnsetCurrencyException |
||
418 | */ |
||
419 | 1 | public function diff(self $stockMovement): self |
|
432 | |||
433 | /****************** |
||
434 | * Helper methods * |
||
435 | *****************/ |
||
436 | |||
437 | /** |
||
438 | * Returns the valid types. |
||
439 | * |
||
440 | * @return array |
||
441 | */ |
||
442 | 1 | public static function getTypes(): array |
|
451 | |||
452 | /** |
||
453 | * Returns true if $type equals the type of the stock movement. |
||
454 | * |
||
455 | * @param string $type |
||
456 | * |
||
457 | * @return bool |
||
458 | */ |
||
459 | 1 | public function isType(string $type): bool |
|
463 | |||
464 | /********************* |
||
465 | * Getters / Setters * |
||
466 | ********************/ |
||
467 | |||
468 | /** |
||
469 | * @return int |
||
470 | */ |
||
471 | 1 | public function getId(): int |
|
475 | |||
476 | /** |
||
477 | * @param int $id |
||
478 | * |
||
479 | * @return StockMovement |
||
480 | */ |
||
481 | 1 | public function setId(int $id): self |
|
487 | |||
488 | /** |
||
489 | * @return int |
||
490 | */ |
||
491 | 2 | public function getQuantity(): int |
|
495 | |||
496 | /** |
||
497 | * @param int $quantity |
||
498 | * |
||
499 | * @return StockMovement |
||
500 | */ |
||
501 | 2 | public function setQuantity(int $quantity): self |
|
509 | |||
510 | /** |
||
511 | * @return bool |
||
512 | */ |
||
513 | 2 | public function isComplaint(): bool |
|
517 | |||
518 | /** |
||
519 | * @param bool $complaint |
||
520 | * |
||
521 | * @return StockMovement |
||
522 | */ |
||
523 | 2 | public function setComplaint(bool $complaint): self |
|
529 | |||
530 | /** |
||
531 | * @return string |
||
532 | */ |
||
533 | 2 | public function getReference(): string |
|
537 | |||
538 | /** |
||
539 | * @param string $reference |
||
540 | * |
||
541 | * @return StockMovement |
||
542 | */ |
||
543 | 2 | public function setReference(string $reference): self |
|
549 | |||
550 | /** |
||
551 | * @return string |
||
552 | */ |
||
553 | 1 | public function getCurrency(): string |
|
557 | |||
558 | /** |
||
559 | * @return Money |
||
560 | * |
||
561 | * @throws UnsetCurrencyException |
||
562 | */ |
||
563 | 2 | public function getRetailPrice(): Money |
|
567 | |||
568 | /** |
||
569 | * @param Money $retailPrice |
||
570 | * |
||
571 | * @return $this |
||
572 | * |
||
573 | * @throws CurrencyMismatchException |
||
574 | */ |
||
575 | 3 | public function setRetailPrice(Money $retailPrice): self |
|
583 | |||
584 | /** |
||
585 | * @return Money |
||
586 | * |
||
587 | * @throws UnsetCurrencyException |
||
588 | */ |
||
589 | 1 | public function getTotalRetailPrice(): Money |
|
593 | |||
594 | /** |
||
595 | * @return Money |
||
596 | * |
||
597 | * @throws UnsetCurrencyException |
||
598 | */ |
||
599 | 3 | public function getPrice(): Money |
|
603 | |||
604 | /** |
||
605 | * @param Money $price |
||
606 | * |
||
607 | * @return $this |
||
608 | * |
||
609 | * @throws CurrencyMismatchException |
||
610 | */ |
||
611 | 3 | public function setPrice(Money $price): self |
|
619 | |||
620 | /** |
||
621 | * @return Money |
||
622 | * |
||
623 | * @throws UnsetCurrencyException |
||
624 | */ |
||
625 | 1 | public function getTotalPrice(): Money |
|
629 | |||
630 | /** |
||
631 | * @return Money |
||
632 | * |
||
633 | * @throws UnsetCurrencyException |
||
634 | */ |
||
635 | 1 | public function getDiscount(): Money |
|
639 | |||
640 | /** |
||
641 | * @return Money |
||
642 | * |
||
643 | * @throws UnsetCurrencyException |
||
644 | */ |
||
645 | 1 | public function getTotalDiscount(): Money |
|
649 | |||
650 | /** |
||
651 | * @return float |
||
652 | */ |
||
653 | 2 | public function getVatPercentage(): float |
|
657 | |||
658 | /** |
||
659 | * @param float $vatPercentage |
||
660 | * |
||
661 | * @return StockMovement |
||
662 | */ |
||
663 | 2 | public function setVatPercentage(float $vatPercentage): self |
|
669 | |||
670 | /** |
||
671 | * @return string |
||
672 | */ |
||
673 | 2 | public function getType(): string |
|
677 | |||
678 | /** |
||
679 | * @param string $type |
||
680 | * |
||
681 | * @return StockMovement |
||
682 | */ |
||
683 | 2 | public function setType(string $type): self |
|
689 | |||
690 | /** |
||
691 | * @return ProductInterface |
||
692 | */ |
||
693 | 2 | public function getProduct(): ?ProductInterface |
|
697 | |||
698 | /** |
||
699 | * @param ProductInterface $product |
||
700 | * |
||
701 | * @return StockMovement |
||
702 | */ |
||
703 | 2 | public function setProduct(ProductInterface $product): self |
|
709 | |||
710 | /** |
||
711 | * @return OrderLineInterface|null |
||
712 | */ |
||
713 | 2 | public function getOrderLine(): ?OrderLineInterface |
|
717 | |||
718 | /** |
||
719 | * @param OrderLineInterface|null $orderLine |
||
720 | * |
||
721 | * @return StockMovement |
||
722 | */ |
||
723 | 2 | public function setOrderLine(?OrderLineInterface $orderLine): self |
|
729 | |||
730 | /** |
||
731 | * @return bool |
||
732 | */ |
||
733 | 2 | public function isOrderLineRemoved(): bool |
|
737 | |||
738 | /** |
||
739 | * @param bool $orderLineRemoved |
||
740 | * |
||
741 | * @return StockMovement |
||
742 | */ |
||
743 | 1 | public function setOrderLineRemoved(bool $orderLineRemoved) |
|
749 | |||
750 | /**************************** |
||
751 | * Protected helper methods * |
||
752 | ***************************/ |
||
753 | |||
754 | 3 | protected function updateTotalPrice(): void |
|
762 | |||
763 | 2 | protected function updateTotalRetailPrice(): void |
|
771 | |||
772 | 2 | protected function updateDiscount(): void |
|
779 | |||
780 | /** |
||
781 | * Updates the shared currency. |
||
782 | * |
||
783 | * If the currency is already set and the new currency is not the same, it throws an exception |
||
784 | * |
||
785 | * @param Money $money |
||
786 | * |
||
787 | * @return StockMovement |
||
788 | * |
||
789 | * @throws CurrencyMismatchException |
||
790 | */ |
||
791 | 3 | protected function updateCurrency(Money $money): self |
|
801 | |||
802 | /** |
||
803 | * Returns a new Money object based on the shared currency |
||
804 | * If no currency is set, it throws an exception. |
||
805 | * |
||
806 | * @param int $val |
||
807 | * |
||
808 | * @return Money |
||
809 | * |
||
810 | * @throws UnsetCurrencyException |
||
811 | */ |
||
812 | 3 | protected function money(int $val): Money |
|
820 | |||
821 | /** |
||
822 | * Returns a vat multiplier for this stock movement. |
||
823 | * |
||
824 | * Example: You have a vat percentage of (float)25.0 then this method will return (string)1.25 |
||
825 | * |
||
826 | * @return string |
||
827 | */ |
||
828 | 1 | protected function getVatMultiplier(): string |
|
832 | } |
||
833 |