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 |
||
| 37 | class StockMovement implements StockMovementInterface |
||
| 38 | { |
||
| 39 | use StockMovementTrait; |
||
| 40 | use Blameable; |
||
| 41 | use Timestampable; |
||
| 42 | |||
| 43 | const TYPE_SALE = 'sale'; |
||
| 44 | const TYPE_RETURN = 'return'; |
||
| 45 | const TYPE_REGULATION = 'regulation'; |
||
| 46 | const TYPE_DELIVERY = 'delivery'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var int |
||
| 50 | * |
||
| 51 | * @ORM\Column(type="integer") |
||
| 52 | * @ORM\GeneratedValue |
||
| 53 | * @ORM\Id |
||
| 54 | */ |
||
| 55 | protected $id; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * The number of items. |
||
| 59 | * |
||
| 60 | * If the quantity is negative it means an outgoing stock movement, i.e. you've sold a product |
||
| 61 | * Contrary a positive number means an ingoing stock movement, i.e. you had a return or a delivery |
||
| 62 | * |
||
| 63 | * @var int |
||
| 64 | * |
||
| 65 | * @FormAssert\NotBlank() |
||
| 66 | * @FormAssert\NotEqualTo(0) |
||
| 67 | * |
||
| 68 | * @ORM\Column(type="integer") |
||
| 69 | */ |
||
| 70 | protected $quantity; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Whether this stock movement is a complaint. |
||
| 74 | * |
||
| 75 | * @var bool |
||
| 76 | * |
||
| 77 | * @ORM\Column(name="complaint", type="boolean") |
||
| 78 | */ |
||
| 79 | protected $complaint; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * A small text describing this stock movement. |
||
| 83 | * |
||
| 84 | * @var string |
||
| 85 | * |
||
| 86 | * @FormAssert\Length(max="191") |
||
| 87 | * |
||
| 88 | * @ORM\Column(name="reference", type="string", length=191) |
||
| 89 | */ |
||
| 90 | protected $reference; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * A valid currency code. |
||
| 94 | * |
||
| 95 | * @var string |
||
| 96 | * |
||
| 97 | * @FormAssert\Currency() |
||
| 98 | * |
||
| 99 | * @ORM\Column(type="string", length=3) |
||
| 100 | */ |
||
| 101 | protected $currency; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * This is the retail price of the product on the time when the stock movement was created |
||
| 105 | * The price is excl vat. |
||
| 106 | * |
||
| 107 | * @var int |
||
| 108 | * |
||
| 109 | * @FormAssert\NotBlank() |
||
| 110 | * @FormAssert\GreaterThanOrEqual(0) |
||
| 111 | * |
||
| 112 | * @ORM\Column(type="integer") |
||
| 113 | */ |
||
| 114 | protected $retailPrice; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Effectively this is `$quantity * $retailPrice`. |
||
| 118 | * |
||
| 119 | * The price is excl vat |
||
| 120 | * |
||
| 121 | * @var int |
||
| 122 | * |
||
| 123 | * @FormAssert\NotBlank() |
||
| 124 | * @FormAssert\GreaterThanOrEqual(0) |
||
| 125 | * |
||
| 126 | * @ORM\Column(type="integer") |
||
| 127 | */ |
||
| 128 | protected $totalRetailPrice; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * This is the price excl vat. |
||
| 132 | * |
||
| 133 | * @var int |
||
| 134 | * |
||
| 135 | * @FormAssert\NotBlank() |
||
| 136 | * @FormAssert\GreaterThanOrEqual(0) |
||
| 137 | * |
||
| 138 | * @ORM\Column(type="integer") |
||
| 139 | */ |
||
| 140 | protected $price; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Effectively this is `$quantity * $price`. |
||
| 144 | * |
||
| 145 | * This is the total price excl vat |
||
| 146 | * |
||
| 147 | * @var int |
||
| 148 | * |
||
| 149 | * @FormAssert\NotBlank() |
||
| 150 | * @FormAssert\GreaterThanOrEqual(0) |
||
| 151 | * |
||
| 152 | * @ORM\Column(type="integer") |
||
| 153 | */ |
||
| 154 | protected $totalPrice; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * This is the discount on this stock movement. |
||
| 158 | * |
||
| 159 | * Effectively this is `$retailPrice - $price` |
||
| 160 | * |
||
| 161 | * @var int |
||
| 162 | * |
||
| 163 | * @FormAssert\NotBlank() |
||
| 164 | * |
||
| 165 | * @ORM\Column(type="integer") |
||
| 166 | */ |
||
| 167 | protected $discount; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * This is the total discount on this stock movement. |
||
| 171 | * |
||
| 172 | * Effectively this is `$totalRetailPrice - $totalPrice` |
||
| 173 | * |
||
| 174 | * @var int |
||
| 175 | * |
||
| 176 | * @FormAssert\NotBlank() |
||
| 177 | * |
||
| 178 | * @ORM\Column(type="integer") |
||
| 179 | */ |
||
| 180 | protected $totalDiscount; |
||
| 181 | |||
| 182 | /** |
||
| 183 | * This is the vat percentage. |
||
| 184 | * |
||
| 185 | * @var string |
||
| 186 | * |
||
| 187 | * @FormAssert\NotBlank() |
||
| 188 | * @FormAssert\GreaterThanOrEqual(0) |
||
| 189 | * |
||
| 190 | * @ORM\Column(type="decimal", precision=5, scale=2) |
||
| 191 | */ |
||
| 192 | protected $vatPercentage; |
||
| 193 | |||
| 194 | /** |
||
| 195 | * This is the type of the stock movement, i.e. 'sale', 'delivery', 'return' etc. |
||
| 196 | * |
||
| 197 | * @var string |
||
| 198 | * |
||
| 199 | * @FormAssert\Choice(callback="getTypes") |
||
| 200 | * |
||
| 201 | * @ORM\Column(name="`type`", type="string", length=191) |
||
| 202 | */ |
||
| 203 | protected $type; |
||
| 204 | |||
| 205 | /** |
||
| 206 | * This is the associated product. |
||
| 207 | * |
||
| 208 | * @var ProductInterface |
||
| 209 | * |
||
| 210 | * @FormAssert\NotBlank() |
||
| 211 | * |
||
| 212 | * @ORM\JoinColumn(nullable=false) |
||
| 213 | * @ORM\ManyToOne(targetEntity="Loevgaard\DandomainFoundation\Entity\Product") |
||
| 214 | */ |
||
| 215 | protected $product; |
||
| 216 | |||
| 217 | /** |
||
| 218 | * If the type equals 'sale' this will be the associated order line. |
||
| 219 | * |
||
| 220 | * @var OrderLineInterface|null |
||
| 221 | * |
||
| 222 | * @ORM\ManyToOne(targetEntity="Loevgaard\DandomainFoundation\Entity\OrderLine", inversedBy="stockMovements") |
||
| 223 | */ |
||
| 224 | protected $orderLine; |
||
| 225 | |||
| 226 | /** |
||
| 227 | * When an associated order line is removed this flag must be set |
||
| 228 | * |
||
| 229 | * @var bool |
||
| 230 | * |
||
| 231 | * @ORM\Column(type="boolean") |
||
| 232 | */ |
||
| 233 | protected $orderLineRemoved; |
||
| 234 | |||
| 235 | public function __construct() |
||
| 240 | |||
| 241 | public function __call($name, $arguments) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @ORM\PrePersist() |
||
| 261 | * @ORM\PreUpdate() |
||
| 262 | */ |
||
| 263 | public function validate() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param int $quantity |
||
| 315 | * @param Money $unitPrice |
||
| 316 | * @param float|string $vatPercent |
||
| 317 | * @param string $type |
||
| 318 | * @param ProductInterface $product |
||
| 319 | * @param string $reference |
||
| 320 | * |
||
| 321 | * @return StockMovementInterface |
||
| 322 | * |
||
| 323 | * @throws \Loevgaard\DandomainStock\Exception\CurrencyMismatchException |
||
| 324 | */ |
||
| 325 | public static function create(int $quantity, Money $unitPrice, $vatPercent, string $type, ProductInterface $product, string $reference): StockMovementInterface |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @param OrderLineInterface $orderLine |
||
| 351 | * |
||
| 352 | * @throws \Loevgaard\DandomainStock\Exception\CurrencyMismatchException |
||
| 353 | * @throws \Loevgaard\DandomainStock\Exception\UnsetProductException |
||
| 354 | */ |
||
| 355 | public function populateFromOrderLine(OrderLineInterface $orderLine) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @return StockMovement |
||
| 386 | * |
||
| 387 | * @throws \Loevgaard\DandomainStock\Exception\CurrencyMismatchException |
||
| 388 | * @throws \Loevgaard\DandomainStock\Exception\UnsetCurrencyException |
||
| 389 | */ |
||
| 390 | public function copy(): self |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @return StockMovementInterface |
||
| 411 | * |
||
| 412 | * @throws \Loevgaard\DandomainStock\Exception\CurrencyMismatchException |
||
| 413 | * @throws \Loevgaard\DandomainStock\Exception\UnsetCurrencyException |
||
| 414 | */ |
||
| 415 | public function inverse(): StockMovementInterface |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @param StockMovementInterface $stockMovement |
||
| 425 | * @return StockMovementInterface |
||
| 426 | * @throws \Loevgaard\DandomainStock\Exception\CurrencyMismatchException |
||
| 427 | * @throws \Loevgaard\DandomainStock\Exception\UnsetCurrencyException |
||
| 428 | * @throws \Loevgaard\DandomainStock\Exception\StockMovementProductMismatchException |
||
| 429 | */ |
||
| 430 | public function diff(StockMovementInterface $stockMovement): StockMovementInterface |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Returns the valid types. |
||
| 446 | * |
||
| 447 | * @return array |
||
| 448 | */ |
||
| 449 | public static function getTypes(): array |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Returns true if $type equals the type of the stock movement. |
||
| 461 | * |
||
| 462 | * @param string $type |
||
| 463 | * |
||
| 464 | * @return bool |
||
| 465 | */ |
||
| 466 | public function isType(string $type): bool |
||
| 470 | |||
| 471 | /********************* |
||
| 472 | * Getters / Setters * |
||
| 473 | ********************/ |
||
| 474 | |||
| 475 | /** |
||
| 476 | * @return int |
||
| 477 | */ |
||
| 478 | public function getId(): int |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @param int $id |
||
| 485 | * |
||
| 486 | * @return StockMovement |
||
| 487 | */ |
||
| 488 | public function setId(int $id): self |
||
| 494 | |||
| 495 | /** |
||
| 496 | * @return int |
||
| 497 | */ |
||
| 498 | public function getQuantity(): int |
||
| 502 | |||
| 503 | /** |
||
| 504 | * @param int $quantity |
||
| 505 | * |
||
| 506 | * @return StockMovement |
||
| 507 | */ |
||
| 508 | public function setQuantity(int $quantity): self |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @return bool |
||
| 519 | */ |
||
| 520 | public function isComplaint(): bool |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @param bool $complaint |
||
| 527 | * |
||
| 528 | * @return StockMovement |
||
| 529 | */ |
||
| 530 | public function setComplaint(bool $complaint): self |
||
| 536 | |||
| 537 | /** |
||
| 538 | * @return string |
||
| 539 | */ |
||
| 540 | public function getReference(): string |
||
| 544 | |||
| 545 | /** |
||
| 546 | * @param string $reference |
||
| 547 | * |
||
| 548 | * @return StockMovement |
||
| 549 | */ |
||
| 550 | public function setReference(string $reference): self |
||
| 556 | |||
| 557 | /** |
||
| 558 | * @return string |
||
| 559 | */ |
||
| 560 | public function getCurrency(): string |
||
| 564 | |||
| 565 | /** |
||
| 566 | * @return Money |
||
| 567 | * |
||
| 568 | * @throws \Loevgaard\DandomainStock\Exception\UnsetCurrencyException |
||
| 569 | */ |
||
| 570 | public function getRetailPrice(): Money |
||
| 574 | |||
| 575 | /** |
||
| 576 | * @param Money $retailPrice |
||
| 577 | * |
||
| 578 | * @return $this |
||
| 579 | * |
||
| 580 | * @throws \Loevgaard\DandomainStock\Exception\CurrencyMismatchException |
||
| 581 | */ |
||
| 582 | public function setRetailPrice(Money $retailPrice): self |
||
| 590 | |||
| 591 | /** |
||
| 592 | * @return Money |
||
| 593 | * |
||
| 594 | * @throws \Loevgaard\DandomainStock\Exception\UnsetCurrencyException |
||
| 595 | */ |
||
| 596 | public function getTotalRetailPrice(): Money |
||
| 600 | |||
| 601 | /** |
||
| 602 | * @return Money |
||
| 603 | * |
||
| 604 | * @throws \Loevgaard\DandomainStock\Exception\UnsetCurrencyException |
||
| 605 | */ |
||
| 606 | public function getPrice(): Money |
||
| 610 | |||
| 611 | /** |
||
| 612 | * @param Money $price |
||
| 613 | * |
||
| 614 | * @return $this |
||
| 615 | * |
||
| 616 | * @throws \Loevgaard\DandomainStock\Exception\CurrencyMismatchException |
||
| 617 | */ |
||
| 618 | public function setPrice(Money $price): self |
||
| 626 | |||
| 627 | /** |
||
| 628 | * @return Money |
||
| 629 | * |
||
| 630 | * @throws \Loevgaard\DandomainStock\Exception\UnsetCurrencyException |
||
| 631 | */ |
||
| 632 | public function getTotalPrice(): Money |
||
| 636 | |||
| 637 | /** |
||
| 638 | * @return Money |
||
| 639 | * |
||
| 640 | * @throws \Loevgaard\DandomainStock\Exception\UnsetCurrencyException |
||
| 641 | */ |
||
| 642 | public function getDiscount(): Money |
||
| 646 | |||
| 647 | /** |
||
| 648 | * @return Money |
||
| 649 | * |
||
| 650 | * @throws \Loevgaard\DandomainStock\Exception\UnsetCurrencyException |
||
| 651 | */ |
||
| 652 | public function getTotalDiscount(): Money |
||
| 656 | |||
| 657 | /** |
||
| 658 | * @return string |
||
| 659 | */ |
||
| 660 | public function getVatPercentage(): string |
||
| 664 | |||
| 665 | /** |
||
| 666 | * @param string $vatPercentage |
||
| 667 | * |
||
| 668 | * @return StockMovement |
||
| 669 | */ |
||
| 670 | public function setVatPercentage(string $vatPercentage): self |
||
| 676 | |||
| 677 | /** |
||
| 678 | * @return string |
||
| 679 | */ |
||
| 680 | public function getType(): string |
||
| 684 | |||
| 685 | /** |
||
| 686 | * @param string $type |
||
| 687 | * |
||
| 688 | * @return StockMovement |
||
| 689 | */ |
||
| 690 | public function setType(string $type): self |
||
| 696 | |||
| 697 | /** |
||
| 698 | * @return ProductInterface |
||
| 699 | */ |
||
| 700 | public function getProduct(): ?ProductInterface |
||
| 704 | |||
| 705 | /** |
||
| 706 | * @param ProductInterface $product |
||
| 707 | * |
||
| 708 | * @return StockMovement |
||
| 709 | */ |
||
| 710 | public function setProduct(ProductInterface $product): self |
||
| 716 | |||
| 717 | /** |
||
| 718 | * @return OrderLineInterface|null |
||
| 719 | */ |
||
| 720 | public function getOrderLine(): ?OrderLineInterface |
||
| 724 | |||
| 725 | /** |
||
| 726 | * @param OrderLineInterface|null $orderLine |
||
| 727 | * |
||
| 728 | * @return StockMovement |
||
| 729 | */ |
||
| 730 | public function setOrderLine(?OrderLineInterface $orderLine): self |
||
| 736 | |||
| 737 | /** |
||
| 738 | * @return bool |
||
| 739 | */ |
||
| 740 | public function isOrderLineRemoved(): bool |
||
| 744 | |||
| 745 | /** |
||
| 746 | * @param bool $orderLineRemoved |
||
| 747 | * |
||
| 748 | * @return StockMovement |
||
| 749 | */ |
||
| 750 | public function setOrderLineRemoved(bool $orderLineRemoved) |
||
| 756 | |||
| 757 | /**************************** |
||
| 758 | * Protected helper methods * |
||
| 759 | ***************************/ |
||
| 760 | |||
| 761 | protected function updateTotalPrice(): void |
||
| 769 | |||
| 770 | protected function updateTotalRetailPrice(): void |
||
| 778 | |||
| 779 | protected function updateDiscount(): void |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Updates the shared currency. |
||
| 789 | * |
||
| 790 | * If the currency is already set and the new currency is not the same, it throws an exception |
||
| 791 | * |
||
| 792 | * @param Money $money |
||
| 793 | * |
||
| 794 | * @return StockMovement |
||
| 795 | * |
||
| 796 | * @throws \Loevgaard\DandomainStock\Exception\CurrencyMismatchException |
||
| 797 | */ |
||
| 798 | protected function updateCurrency(Money $money): self |
||
| 808 | |||
| 809 | /** |
||
| 810 | * Returns a new Money object based on the shared currency |
||
| 811 | * If no currency is set, it throws an exception. |
||
| 812 | * |
||
| 813 | * @param int $val |
||
| 814 | * |
||
| 815 | * @return Money |
||
| 816 | * |
||
| 817 | * @throws \Loevgaard\DandomainStock\Exception\UnsetCurrencyException |
||
| 818 | */ |
||
| 819 | protected function money(int $val): Money |
||
| 827 | |||
| 828 | /** |
||
| 829 | * Returns a vat multiplier for this stock movement. |
||
| 830 | * |
||
| 831 | * Example: You have a vat percentage of (float)25.0 then this method will return (string)1.25 |
||
| 832 | * |
||
| 833 | * @return string |
||
| 834 | */ |
||
| 835 | protected function getVatMultiplier(): string |
||
| 839 | } |
||
| 840 |