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 |
||
| 36 | class StockMovement implements StockMovementInterface |
||
| 37 | { |
||
| 38 | use StockMovementTrait; |
||
| 39 | use Blameable; |
||
| 40 | use Timestampable; |
||
| 41 | |||
| 42 | const TYPE_SALE = 'sale'; |
||
| 43 | const TYPE_RETURN = 'return'; |
||
| 44 | const TYPE_REGULATION = 'regulation'; |
||
| 45 | const TYPE_DELIVERY = 'delivery'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var int |
||
| 49 | * |
||
| 50 | * @ORM\Column(type="integer") |
||
| 51 | * @ORM\GeneratedValue |
||
| 52 | * @ORM\Id |
||
| 53 | */ |
||
| 54 | protected $id; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The number of items. |
||
| 58 | * |
||
| 59 | * If the quantity is negative it means an outgoing stock movement, i.e. you've sold a product |
||
| 60 | * Contrary a positive number means an ingoing stock movement, i.e. you had a return or a delivery |
||
| 61 | * |
||
| 62 | * @var int |
||
| 63 | * |
||
| 64 | * @FormAssert\NotBlank() |
||
| 65 | * @FormAssert\NotEqualTo(0) |
||
| 66 | * |
||
| 67 | * @ORM\Column(type="integer") |
||
| 68 | */ |
||
| 69 | protected $quantity; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Whether this stock movement is a complaint. |
||
| 73 | * |
||
| 74 | * @var bool |
||
| 75 | * |
||
| 76 | * @ORM\Column(name="complaint", type="boolean") |
||
| 77 | */ |
||
| 78 | protected $complaint; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * A small text describing this stock movement. |
||
| 82 | * |
||
| 83 | * @var string |
||
| 84 | * |
||
| 85 | * @FormAssert\Length(max="191") |
||
| 86 | * |
||
| 87 | * @ORM\Column(name="reference", type="string", length=191) |
||
| 88 | */ |
||
| 89 | protected $reference; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * A valid currency code. |
||
| 93 | * |
||
| 94 | * @var string |
||
| 95 | * |
||
| 96 | * @FormAssert\Currency() |
||
| 97 | * |
||
| 98 | * @ORM\Column(type="string", length=3) |
||
| 99 | */ |
||
| 100 | protected $currency; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * This is the retail price of the product on the time when the stock movement was created |
||
| 104 | * The price is excl vat. |
||
| 105 | * |
||
| 106 | * @var int |
||
| 107 | * |
||
| 108 | * @FormAssert\NotBlank() |
||
| 109 | * @FormAssert\GreaterThanOrEqual(0) |
||
| 110 | * |
||
| 111 | * @ORM\Column(type="integer") |
||
| 112 | */ |
||
| 113 | protected $retailPrice; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Effectively this is `$quantity * $retailPrice`. |
||
| 117 | * |
||
| 118 | * The price is excl vat |
||
| 119 | * |
||
| 120 | * @var int |
||
| 121 | * |
||
| 122 | * @FormAssert\NotBlank() |
||
| 123 | * @FormAssert\GreaterThanOrEqual(0) |
||
| 124 | * |
||
| 125 | * @ORM\Column(type="integer") |
||
| 126 | */ |
||
| 127 | protected $totalRetailPrice; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * This is the price excl vat. |
||
| 131 | * |
||
| 132 | * @var int |
||
| 133 | * |
||
| 134 | * @FormAssert\NotBlank() |
||
| 135 | * @FormAssert\GreaterThanOrEqual(0) |
||
| 136 | * |
||
| 137 | * @ORM\Column(type="integer") |
||
| 138 | */ |
||
| 139 | protected $price; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Effectively this is `$quantity * $price`. |
||
| 143 | * |
||
| 144 | * This is the total price excl vat |
||
| 145 | * |
||
| 146 | * @var int |
||
| 147 | * |
||
| 148 | * @FormAssert\NotBlank() |
||
| 149 | * @FormAssert\GreaterThanOrEqual(0) |
||
| 150 | * |
||
| 151 | * @ORM\Column(type="integer") |
||
| 152 | */ |
||
| 153 | protected $totalPrice; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * This is the discount on this stock movement. |
||
| 157 | * |
||
| 158 | * Effectively this is `$retailPrice - $price` |
||
| 159 | * |
||
| 160 | * @var int |
||
| 161 | * |
||
| 162 | * @FormAssert\NotBlank() |
||
| 163 | * |
||
| 164 | * @ORM\Column(type="integer") |
||
| 165 | */ |
||
| 166 | protected $discount; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * This is the total discount on this stock movement. |
||
| 170 | * |
||
| 171 | * Effectively this is `$totalRetailPrice - $totalPrice` |
||
| 172 | * |
||
| 173 | * @var int |
||
| 174 | * |
||
| 175 | * @FormAssert\NotBlank() |
||
| 176 | * |
||
| 177 | * @ORM\Column(type="integer") |
||
| 178 | */ |
||
| 179 | protected $totalDiscount; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * This is the vat percentage. |
||
| 183 | * |
||
| 184 | * @var float |
||
| 185 | * |
||
| 186 | * @FormAssert\NotBlank() |
||
| 187 | * @FormAssert\GreaterThanOrEqual(0) |
||
| 188 | * |
||
| 189 | * @ORM\Column(type="decimal", precision=5, scale=2) |
||
| 190 | */ |
||
| 191 | protected $vatPercentage; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * This is the type of the stock movement, i.e. 'sale', 'delivery', 'return' etc. |
||
| 195 | * |
||
| 196 | * @var string |
||
| 197 | * |
||
| 198 | * @FormAssert\Choice(callback="getTypes") |
||
| 199 | * |
||
| 200 | * @ORM\Column(name="`type`", type="string", length=191) |
||
| 201 | */ |
||
| 202 | protected $type; |
||
| 203 | |||
| 204 | /** |
||
| 205 | * This is the associated product. |
||
| 206 | * |
||
| 207 | * @var ProductInterface |
||
| 208 | * |
||
| 209 | * @FormAssert\NotBlank() |
||
| 210 | * |
||
| 211 | * @ORM\JoinColumn(nullable=false) |
||
| 212 | * @ORM\ManyToOne(targetEntity="Loevgaard\DandomainFoundation\Entity\Product") |
||
| 213 | */ |
||
| 214 | protected $product; |
||
| 215 | |||
| 216 | /** |
||
| 217 | * If the type equals 'sale' this will be the associated order line. |
||
| 218 | * |
||
| 219 | * @var OrderLineInterface|null |
||
| 220 | * |
||
| 221 | * @ORM\ManyToOne(targetEntity="Loevgaard\DandomainFoundation\Entity\OrderLine", inversedBy="stockMovements") |
||
| 222 | */ |
||
| 223 | protected $orderLine; |
||
| 224 | |||
| 225 | /** |
||
| 226 | * When an associated order line is removed this flag must be set |
||
| 227 | * |
||
| 228 | * @var bool |
||
| 229 | * |
||
| 230 | * @ORM\Column(type="boolean") |
||
| 231 | */ |
||
| 232 | protected $orderLineRemoved; |
||
| 233 | |||
| 234 | public function __construct() |
||
| 239 | |||
| 240 | public function __call($name, $arguments) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @ORM\PrePersist() |
||
| 260 | * @ORM\PreUpdate() |
||
| 261 | */ |
||
| 262 | public function validate() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param int $quantity |
||
| 308 | * @param Money $unitPrice |
||
| 309 | * @param float $vatPercent |
||
| 310 | * @param string $type |
||
| 311 | * @param ProductInterface $product |
||
| 312 | * @param string $reference |
||
| 313 | * |
||
| 314 | * @return StockMovementInterface |
||
| 315 | * |
||
| 316 | * @throws \Loevgaard\DandomainStock\Exception\CurrencyMismatchException |
||
| 317 | * @throws \Loevgaard\DandomainStock\Exception\UndefinedPriceForCurrencyException |
||
| 318 | */ |
||
| 319 | public static function create(int $quantity, Money $unitPrice, float $vatPercent, string $type, ProductInterface $product, string $reference): StockMovementInterface |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @param OrderLineInterface $orderLine |
||
| 348 | * |
||
| 349 | * @throws \Loevgaard\DandomainStock\Exception\CurrencyMismatchException |
||
| 350 | * @throws \Loevgaard\DandomainStock\Exception\UndefinedPriceForCurrencyException |
||
| 351 | * @throws \Loevgaard\DandomainStock\Exception\UnsetProductException |
||
| 352 | */ |
||
| 353 | public function populateFromOrderLine(OrderLineInterface $orderLine) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @return StockMovement |
||
| 388 | * |
||
| 389 | * @throws \Loevgaard\DandomainStock\Exception\CurrencyMismatchException |
||
| 390 | * @throws \Loevgaard\DandomainStock\Exception\UnsetCurrencyException |
||
| 391 | */ |
||
| 392 | public function copy(): self |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @return StockMovementInterface |
||
| 413 | * |
||
| 414 | * @throws \Loevgaard\DandomainStock\Exception\CurrencyMismatchException |
||
| 415 | * @throws \Loevgaard\DandomainStock\Exception\UnsetCurrencyException |
||
| 416 | */ |
||
| 417 | public function inverse(): StockMovementInterface |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @param StockMovementInterface $stockMovement |
||
| 427 | * @return StockMovementInterface |
||
| 428 | * @throws \Loevgaard\DandomainStock\Exception\CurrencyMismatchException |
||
| 429 | * @throws \Loevgaard\DandomainStock\Exception\UnsetCurrencyException |
||
| 430 | * @throws \Loevgaard\DandomainStock\Exception\StockMovementProductMismatchException |
||
| 431 | */ |
||
| 432 | public function diff(StockMovementInterface $stockMovement): StockMovementInterface |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Returns the valid types. |
||
| 448 | * |
||
| 449 | * @return array |
||
| 450 | */ |
||
| 451 | public static function getTypes(): array |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Returns true if $type equals the type of the stock movement. |
||
| 463 | * |
||
| 464 | * @param string $type |
||
| 465 | * |
||
| 466 | * @return bool |
||
| 467 | */ |
||
| 468 | public function isType(string $type): bool |
||
| 472 | |||
| 473 | /********************* |
||
| 474 | * Getters / Setters * |
||
| 475 | ********************/ |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @return int |
||
| 479 | */ |
||
| 480 | public function getId(): int |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @param int $id |
||
| 487 | * |
||
| 488 | * @return StockMovement |
||
| 489 | */ |
||
| 490 | public function setId(int $id): self |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @return int |
||
| 499 | */ |
||
| 500 | public function getQuantity(): int |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @param int $quantity |
||
| 507 | * |
||
| 508 | * @return StockMovement |
||
| 509 | */ |
||
| 510 | public function setQuantity(int $quantity): self |
||
| 518 | |||
| 519 | /** |
||
| 520 | * @return bool |
||
| 521 | */ |
||
| 522 | public function isComplaint(): bool |
||
| 526 | |||
| 527 | /** |
||
| 528 | * @param bool $complaint |
||
| 529 | * |
||
| 530 | * @return StockMovement |
||
| 531 | */ |
||
| 532 | public function setComplaint(bool $complaint): self |
||
| 538 | |||
| 539 | /** |
||
| 540 | * @return string |
||
| 541 | */ |
||
| 542 | public function getReference(): string |
||
| 546 | |||
| 547 | /** |
||
| 548 | * @param string $reference |
||
| 549 | * |
||
| 550 | * @return StockMovement |
||
| 551 | */ |
||
| 552 | public function setReference(string $reference): self |
||
| 558 | |||
| 559 | /** |
||
| 560 | * @return string |
||
| 561 | */ |
||
| 562 | public function getCurrency(): string |
||
| 566 | |||
| 567 | /** |
||
| 568 | * @return Money |
||
| 569 | * |
||
| 570 | * @throws \Loevgaard\DandomainStock\Exception\UnsetCurrencyException |
||
| 571 | */ |
||
| 572 | public function getRetailPrice(): Money |
||
| 576 | |||
| 577 | /** |
||
| 578 | * @param Money $retailPrice |
||
| 579 | * |
||
| 580 | * @return $this |
||
| 581 | * |
||
| 582 | * @throws \Loevgaard\DandomainStock\Exception\CurrencyMismatchException |
||
| 583 | */ |
||
| 584 | public function setRetailPrice(Money $retailPrice): self |
||
| 592 | |||
| 593 | /** |
||
| 594 | * @return Money |
||
| 595 | * |
||
| 596 | * @throws \Loevgaard\DandomainStock\Exception\UnsetCurrencyException |
||
| 597 | */ |
||
| 598 | public function getTotalRetailPrice(): Money |
||
| 602 | |||
| 603 | /** |
||
| 604 | * @return Money |
||
| 605 | * |
||
| 606 | * @throws \Loevgaard\DandomainStock\Exception\UnsetCurrencyException |
||
| 607 | */ |
||
| 608 | public function getPrice(): Money |
||
| 612 | |||
| 613 | /** |
||
| 614 | * @param Money $price |
||
| 615 | * |
||
| 616 | * @return $this |
||
| 617 | * |
||
| 618 | * @throws \Loevgaard\DandomainStock\Exception\CurrencyMismatchException |
||
| 619 | */ |
||
| 620 | public function setPrice(Money $price): self |
||
| 628 | |||
| 629 | /** |
||
| 630 | * @return Money |
||
| 631 | * |
||
| 632 | * @throws \Loevgaard\DandomainStock\Exception\UnsetCurrencyException |
||
| 633 | */ |
||
| 634 | public function getTotalPrice(): Money |
||
| 638 | |||
| 639 | /** |
||
| 640 | * @return Money |
||
| 641 | * |
||
| 642 | * @throws \Loevgaard\DandomainStock\Exception\UnsetCurrencyException |
||
| 643 | */ |
||
| 644 | public function getDiscount(): Money |
||
| 648 | |||
| 649 | /** |
||
| 650 | * @return Money |
||
| 651 | * |
||
| 652 | * @throws \Loevgaard\DandomainStock\Exception\UnsetCurrencyException |
||
| 653 | */ |
||
| 654 | public function getTotalDiscount(): Money |
||
| 658 | |||
| 659 | /** |
||
| 660 | * @return float |
||
| 661 | */ |
||
| 662 | public function getVatPercentage(): float |
||
| 666 | |||
| 667 | /** |
||
| 668 | * @param float $vatPercentage |
||
| 669 | * |
||
| 670 | * @return StockMovement |
||
| 671 | */ |
||
| 672 | public function setVatPercentage(float $vatPercentage): self |
||
| 678 | |||
| 679 | /** |
||
| 680 | * @return string |
||
| 681 | */ |
||
| 682 | public function getType(): string |
||
| 686 | |||
| 687 | /** |
||
| 688 | * @param string $type |
||
| 689 | * |
||
| 690 | * @return StockMovement |
||
| 691 | */ |
||
| 692 | public function setType(string $type): self |
||
| 698 | |||
| 699 | /** |
||
| 700 | * @return ProductInterface |
||
| 701 | */ |
||
| 702 | public function getProduct(): ?ProductInterface |
||
| 706 | |||
| 707 | /** |
||
| 708 | * @param ProductInterface $product |
||
| 709 | * |
||
| 710 | * @return StockMovement |
||
| 711 | */ |
||
| 712 | public function setProduct(ProductInterface $product): self |
||
| 718 | |||
| 719 | /** |
||
| 720 | * @return OrderLineInterface|null |
||
| 721 | */ |
||
| 722 | public function getOrderLine(): ?OrderLineInterface |
||
| 726 | |||
| 727 | /** |
||
| 728 | * @param OrderLineInterface|null $orderLine |
||
| 729 | * |
||
| 730 | * @return StockMovement |
||
| 731 | */ |
||
| 732 | public function setOrderLine(?OrderLineInterface $orderLine): self |
||
| 738 | |||
| 739 | /** |
||
| 740 | * @return bool |
||
| 741 | */ |
||
| 742 | public function isOrderLineRemoved(): bool |
||
| 746 | |||
| 747 | /** |
||
| 748 | * @param bool $orderLineRemoved |
||
| 749 | * |
||
| 750 | * @return StockMovement |
||
| 751 | */ |
||
| 752 | public function setOrderLineRemoved(bool $orderLineRemoved) |
||
| 758 | |||
| 759 | /**************************** |
||
| 760 | * Protected helper methods * |
||
| 761 | ***************************/ |
||
| 762 | |||
| 763 | protected function updateTotalPrice(): void |
||
| 771 | |||
| 772 | protected function updateTotalRetailPrice(): void |
||
| 780 | |||
| 781 | protected function updateDiscount(): void |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Updates the shared currency. |
||
| 791 | * |
||
| 792 | * If the currency is already set and the new currency is not the same, it throws an exception |
||
| 793 | * |
||
| 794 | * @param Money $money |
||
| 795 | * |
||
| 796 | * @return StockMovement |
||
| 797 | * |
||
| 798 | * @throws \Loevgaard\DandomainStock\Exception\CurrencyMismatchException |
||
| 799 | */ |
||
| 800 | protected function updateCurrency(Money $money): self |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Returns a new Money object based on the shared currency |
||
| 813 | * If no currency is set, it throws an exception. |
||
| 814 | * |
||
| 815 | * @param int $val |
||
| 816 | * |
||
| 817 | * @return Money |
||
| 818 | * |
||
| 819 | * @throws \Loevgaard\DandomainStock\Exception\UnsetCurrencyException |
||
| 820 | */ |
||
| 821 | protected function money(int $val): Money |
||
| 829 | |||
| 830 | /** |
||
| 831 | * Returns a vat multiplier for this stock movement. |
||
| 832 | * |
||
| 833 | * Example: You have a vat percentage of (float)25.0 then this method will return (string)1.25 |
||
| 834 | * |
||
| 835 | * @return string |
||
| 836 | */ |
||
| 837 | protected function getVatMultiplier(): string |
||
| 841 | } |
||
| 842 |