Complex classes like OrderContext 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 OrderContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 47 | final class OrderContext implements Context |
||
| 48 | { |
||
| 49 | /** |
||
| 50 | * @var SharedStorageInterface |
||
| 51 | */ |
||
| 52 | private $sharedStorage; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var OrderRepositoryInterface |
||
| 56 | */ |
||
| 57 | private $orderRepository; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var FactoryInterface |
||
| 61 | */ |
||
| 62 | private $orderFactory; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var FactoryInterface |
||
| 66 | */ |
||
| 67 | private $orderItemFactory; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var OrderItemQuantityModifierInterface |
||
| 71 | */ |
||
| 72 | private $itemQuantityModifier; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var FactoryInterface |
||
| 76 | */ |
||
| 77 | private $customerFactory; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var RepositoryInterface |
||
| 81 | */ |
||
| 82 | private $customerRepository; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var ObjectManager |
||
| 86 | */ |
||
| 87 | private $objectManager; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var StateMachineFactoryInterface |
||
| 91 | */ |
||
| 92 | private $stateMachineFactory; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var ProductVariantResolverInterface |
||
| 96 | */ |
||
| 97 | private $variantResolver; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param SharedStorageInterface $sharedStorage |
||
| 101 | * @param OrderRepositoryInterface $orderRepository |
||
| 102 | * @param FactoryInterface $orderFactory |
||
| 103 | * @param FactoryInterface $orderItemFactory |
||
| 104 | * @param OrderItemQuantityModifierInterface $itemQuantityModifier |
||
| 105 | * @param FactoryInterface $customerFactory |
||
| 106 | * @param RepositoryInterface $customerRepository |
||
| 107 | * @param ObjectManager $objectManager |
||
| 108 | * @param StateMachineFactoryInterface $stateMachineFactory |
||
| 109 | * @param ProductVariantResolverInterface $variantResolver |
||
| 110 | */ |
||
| 111 | public function __construct( |
||
| 112 | SharedStorageInterface $sharedStorage, |
||
| 113 | OrderRepositoryInterface $orderRepository, |
||
| 114 | FactoryInterface $orderFactory, |
||
| 115 | FactoryInterface $orderItemFactory, |
||
| 116 | OrderItemQuantityModifierInterface $itemQuantityModifier, |
||
| 117 | FactoryInterface $customerFactory, |
||
| 118 | RepositoryInterface $customerRepository, |
||
| 119 | ObjectManager $objectManager, |
||
| 120 | StateMachineFactoryInterface $stateMachineFactory, |
||
| 121 | ProductVariantResolverInterface $variantResolver |
||
| 122 | ) { |
||
| 123 | $this->sharedStorage = $sharedStorage; |
||
| 124 | $this->orderRepository = $orderRepository; |
||
| 125 | $this->orderFactory = $orderFactory; |
||
| 126 | $this->orderItemFactory = $orderItemFactory; |
||
| 127 | $this->itemQuantityModifier = $itemQuantityModifier; |
||
| 128 | $this->customerFactory = $customerFactory; |
||
| 129 | $this->customerRepository = $customerRepository; |
||
| 130 | $this->objectManager = $objectManager; |
||
| 131 | $this->stateMachineFactory = $stateMachineFactory; |
||
| 132 | $this->variantResolver = $variantResolver; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @Given /^there is (?:a|another) (customer "[^"]+") that placed an order$/ |
||
| 137 | * @Given /^there is (?:a|another) (customer "[^"]+") that placed (an order "[^"]+")$/ |
||
| 138 | * @Given a customer :customer placed an order :orderNumber |
||
| 139 | * @Given the customer :customer has already placed an order :orderNumber |
||
| 140 | */ |
||
| 141 | public function thereIsCustomerThatPlacedOrder(CustomerInterface $customer, $orderNumber = null) |
||
| 142 | { |
||
| 143 | $order = $this->createOrder($customer, $orderNumber); |
||
| 144 | |||
| 145 | $this->sharedStorage->set('order', $order); |
||
| 146 | |||
| 147 | $this->orderRepository->add($order); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @Given /^the guest customer placed order with ("[^"]+" product) for "([^"]+)" and ("[^"]+" based shipping address) with ("[^"]+" shipping method) and ("[^"]+" payment)$/ |
||
| 152 | */ |
||
| 153 | public function theGuestCustomerPlacedOrderWithForAndBasedShippingAddress( |
||
| 154 | ProductInterface $product, |
||
| 155 | string $email, |
||
| 156 | AddressInterface $address, |
||
| 157 | ShippingMethodInterface $shippingMethod, |
||
| 158 | PaymentMethodInterface $paymentMethod |
||
| 159 | ) { |
||
| 160 | /** @var CustomerInterface $customer */ |
||
| 161 | $customer = $this->customerFactory->createNew(); |
||
| 162 | $customer->setEmail($email); |
||
| 163 | $customer->setFirstname('John'); |
||
| 164 | $customer->setLastname('Doe'); |
||
| 165 | |||
| 166 | $this->customerRepository->add($customer); |
||
| 167 | |||
| 168 | $this->placeOrder($product, $shippingMethod, $address, $paymentMethod, $customer, 1); |
||
| 169 | $this->objectManager->flush(); |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @Given a customer :customer added something to cart |
||
| 174 | */ |
||
| 175 | public function customerStartedCheckout(CustomerInterface $customer) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @Given /^(I) placed (an order "[^"]+")$/ |
||
| 186 | */ |
||
| 187 | public function iPlacedAnOrder(UserInterface $user, $orderNumber) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @Given /^the customer ("[^"]+" addressed it to "[^"]+", "[^"]+" "[^"]+" in the "[^"]+"(?:|, "[^"]+"))$/ |
||
| 199 | * @Given /^I (addressed it to "[^"]+", "[^"]+", "[^"]+" "[^"]+" in the "[^"]+"(?:|, "[^"]+"))$/ |
||
| 200 | */ |
||
| 201 | public function theCustomerAddressedItTo(AddressInterface $address) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @Given the customer changed shipping address' street to :street |
||
| 212 | */ |
||
| 213 | public function theCustomerChangedShippingAddressStreetTo($street) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @Given /^the customer set the billing (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)")$/ |
||
| 228 | * @Given /^for the billing address (of "[^"]+" in the "[^"]+", "[^"]+" "[^"]+", "[^"]+")$/ |
||
| 229 | * @Given /^for the billing address (of "[^"]+" in the "[^"]+", "[^"]+" "([^"]+)", "[^"]+", "[^"]+")$/ |
||
| 230 | */ |
||
| 231 | public function forTheBillingAddressOf(AddressInterface $address) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @Given /^the customer ("[^"]+" addressed it to "[^"]+", "[^"]+" "[^"]+" in the "[^"]+") with identical billing address$/ |
||
| 245 | * @Given /^I (addressed it to "[^"]+", "[^"]+", "[^"]+" "[^"]+" in the "[^"]+") with identical billing address$/ |
||
| 246 | */ |
||
| 247 | public function theCustomerAddressedItToWithIdenticalBillingAddress(AddressInterface $address) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @Given /^the customer chose ("[^"]+" shipping method) (to "[^"]+") with ("[^"]+" payment)$/ |
||
| 255 | * @Given /^I chose ("[^"]+" shipping method) (to "[^"]+") with ("[^"]+" payment)$/ |
||
| 256 | */ |
||
| 257 | public function theCustomerChoseShippingToWithPayment( |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @Given /^the customer chose ("[^"]+" shipping method) with ("[^"]+" payment)$/ |
||
| 272 | * @Given /^I chose ("[^"]+" shipping method) with ("[^"]+" payment)$/ |
||
| 273 | */ |
||
| 274 | public function theCustomerChoseShippingWithPayment( |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @Given /^the customer chose ("[^"]+" shipping method)$/ |
||
| 288 | */ |
||
| 289 | public function theCustomerChoseShippingMethod(ShippingMethodInterface $shippingMethod) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @Given /^the customer chose ("[^"]+" payment)$/ |
||
| 309 | */ |
||
| 310 | public function theCustomerChosePayment(PaymentMethodInterface $paymentMethod) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @Given the customer bought a single :product |
||
| 327 | * @Given I bought a single :product |
||
| 328 | */ |
||
| 329 | public function theCustomerBoughtSingleProduct(ProductInterface $product) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @Given /^the customer bought ((?:a|an) "[^"]+") and ((?:a|an) "[^"]+")$/ |
||
| 338 | * @Given /^I bought ((?:a|an) "[^"]+") and ((?:a|an) "[^"]+")$/ |
||
| 339 | */ |
||
| 340 | public function theCustomerBoughtProductAndProduct(ProductInterface $product, ProductInterface $secondProduct) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @Given /^the customer bought (\d+) ("[^"]+" products)$/ |
||
| 348 | */ |
||
| 349 | public function theCustomerBoughtSeveralProducts($quantity, ProductInterface $product) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @Given /^the customer bought ([^"]+) units of ("[^"]+" variant of product "[^"]+")$/ |
||
| 359 | */ |
||
| 360 | public function theCustomerBoughtSeveralVariantsOfProduct($quantity, ProductVariantInterface $variant) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @Given /^the customer bought a single ("[^"]+" variant of product "[^"]+")$/ |
||
| 369 | */ |
||
| 370 | public function theCustomerBoughtSingleProductVariant(ProductVariantInterface $productVariant) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @Given the customer bought a single :product using :coupon coupon |
||
| 379 | * @Given I bought a single :product using :coupon coupon |
||
| 380 | */ |
||
| 381 | public function theCustomerBoughtSingleUsing(ProductInterface $product, PromotionCouponInterface $coupon) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @Given I used :coupon coupon |
||
| 391 | */ |
||
| 392 | public function iUsedCoupon(PromotionCouponInterface $coupon) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @Given /^(I) have already placed (\d+) orders choosing ("[^"]+" product), ("[^"]+" shipping method) (to "[^"]+") with ("[^"]+" payment)$/ |
||
| 402 | */ |
||
| 403 | public function iHaveAlreadyPlacedOrderNthTimes( |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @Given /^(this customer) has(?:| also) placed (an order "[^"]+") at "([^"]+)"$/ |
||
| 421 | */ |
||
| 422 | public function thisCustomerHasPlacedAnOrderAtDate(CustomerInterface $customer, $number, $checkoutCompletedAt) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @Given /^(this customer) has(?:| also) placed (an order "[^"]+") on a (channel "[^"]+")$/ |
||
| 433 | */ |
||
| 434 | public function thisCustomerHasPlacedAnOrderOnAChannel(CustomerInterface $customer, $number, $channel) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @Given /^(this customer) has(?:| also) started checkout on a (channel "[^"]+")$/ |
||
| 445 | */ |
||
| 446 | public function thisCustomerHasStartedCheckoutOnAChannel(CustomerInterface $customer, $channel) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @Given /^(customer "[^"]+"|this customer) has(?:| also) placed (\d+) orders on the ("[^"]+" channel) in each buying (\d+) ("[^"]+" products?)$/ |
||
| 456 | */ |
||
| 457 | public function thisCustomerPlacedOrdersOnChannelBuyingProducts( |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @Given /^(customer "[^"]+"|this customer) has(?:| also) fulfilled (\d+) orders placed on the ("[^"]+" channel) in each buying (\d+) ("[^"]+" products?)$/ |
||
| 469 | */ |
||
| 470 | public function thisCustomerFulfilledOrdersPlacedOnChannelBuyingProducts( |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @Given :numberOfCustomers customers have added products to the cart for total of :total |
||
| 482 | */ |
||
| 483 | public function customersHaveAddedProductsToTheCartForTotalOf($numberOfCustomers, $total) |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @Given a single customer has placed an order for total of :total |
||
| 506 | * @Given :numberOfCustomers customers have placed :numberOfOrders orders for total of :total |
||
| 507 | * @Given then :numberOfCustomers more customers have placed :numberOfOrders orders for total of :total |
||
| 508 | */ |
||
| 509 | public function customersHavePlacedOrdersForTotalOf( |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @Given :numberOfCustomers customers have fulfilled :numberOfOrders orders placed for total of :total |
||
| 519 | * @Given then :numberOfCustomers more customers have fulfilled :numberOfOrders orders placed for total of :total |
||
| 520 | */ |
||
| 521 | public function customersHaveFulfilledOrdersPlacedForTotalOf( |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @Given :numberOfCustomers customers have placed :numberOfOrders orders for total of :total mostly :product product |
||
| 531 | * @Given then :numberOfCustomers more customers have placed :numberOfOrders orders for total of :total mostly :product product |
||
| 532 | */ |
||
| 533 | public function customersHavePlacedOrdersForTotalOfMostlyProduct( |
||
| 541 | |||
| 542 | /** |
||
| 543 | * @Given :numberOfCustomers customers have fulfilled :numberOfOrders orders placed for total of :total mostly :product product |
||
| 544 | * @Given then :numberOfCustomers more customers have fulfilled :numberOfOrders orders placed for total of :total mostly :product product |
||
| 545 | */ |
||
| 546 | public function customersHaveFulfilledOrdersPlacedForTotalOfMostlyProduct( |
||
| 554 | |||
| 555 | /** |
||
| 556 | * @Given /^(this customer) has(?:| also) placed (an order "[^"]+") buying a single ("[^"]+" product) for ("[^"]+") on the ("[^"]+" channel)$/ |
||
| 557 | */ |
||
| 558 | public function customerHasPlacedAnOrderBuyingASingleProductForOnTheChannel( |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @Given /^(this order) is already paid$/ |
||
| 576 | * @Given the order :order is already paid |
||
| 577 | */ |
||
| 578 | public function thisOrderIsAlreadyPaid(OrderInterface $order) |
||
| 584 | |||
| 585 | /** |
||
| 586 | * @Given /^(this order) has been refunded$/ |
||
| 587 | */ |
||
| 588 | public function thisOrderHasBeenRefunded(OrderInterface $order) |
||
| 594 | |||
| 595 | /** |
||
| 596 | * @Given /^the customer cancelled (this order)$/ |
||
| 597 | * @Given /^(this order) was cancelled$/ |
||
| 598 | * @Given the order :order was cancelled |
||
| 599 | * @Given /^I cancelled (this order)$/ |
||
| 600 | */ |
||
| 601 | public function theCustomerCancelledThisOrder(OrderInterface $order) |
||
| 607 | |||
| 608 | /** |
||
| 609 | * @Given /^I cancelled my last order$/ |
||
| 610 | */ |
||
| 611 | public function theCustomerCancelledMyLastOrder() |
||
| 618 | |||
| 619 | /** |
||
| 620 | * @Given /^(this order) has already been shipped$/ |
||
| 621 | */ |
||
| 622 | public function thisOrderHasAlreadyBeenShipped(OrderInterface $order) |
||
| 628 | |||
| 629 | /** |
||
| 630 | * @When the customer used coupon :coupon |
||
| 631 | */ |
||
| 632 | public function theCustomerUsedCoupon(PromotionCouponInterface $coupon) |
||
| 640 | |||
| 641 | /** |
||
| 642 | * @param OrderInterface $order |
||
| 643 | * @param string $transition |
||
| 644 | */ |
||
| 645 | private function applyShipmentTransitionOnOrder(OrderInterface $order, $transition) |
||
| 651 | |||
| 652 | /** |
||
| 653 | * @param OrderInterface $order |
||
| 654 | * @param string $transition |
||
| 655 | */ |
||
| 656 | private function applyPaymentTransitionOnOrder(OrderInterface $order, $transition) |
||
| 662 | |||
| 663 | /** |
||
| 664 | * @param OrderInterface $order |
||
| 665 | * @param string $transition |
||
| 666 | */ |
||
| 667 | private function applyTransitionOnOrderCheckout(OrderInterface $order, $transition) |
||
| 671 | |||
| 672 | /** |
||
| 673 | * @param OrderInterface $order |
||
| 674 | * @param string $transition |
||
| 675 | */ |
||
| 676 | private function applyTransitionOnOrder(OrderInterface $order, string $transition): void |
||
| 680 | |||
| 681 | /** |
||
| 682 | * @param ProductVariantInterface $productVariant |
||
| 683 | * @param int $quantity |
||
| 684 | * |
||
| 685 | * @return OrderInterface |
||
| 686 | */ |
||
| 687 | private function addProductVariantToOrder(ProductVariantInterface $productVariant, $quantity = 1) |
||
| 700 | |||
| 701 | /** |
||
| 702 | * @param OrderInterface $order |
||
| 703 | * @param ChannelInterface $channel |
||
| 704 | * @param ProductVariantInterface $productVariant |
||
| 705 | * @param int $quantity |
||
| 706 | */ |
||
| 707 | private function addProductVariantsToOrderWithChannelPrice( |
||
| 725 | |||
| 726 | /** |
||
| 727 | * @param CustomerInterface $customer |
||
| 728 | * @param string $number |
||
| 729 | * @param ChannelInterface|null $channel |
||
| 730 | * @param string|null $localeCode |
||
| 731 | * |
||
| 732 | * @return OrderInterface |
||
| 733 | */ |
||
| 734 | private function createOrder( |
||
| 750 | |||
| 751 | /** |
||
| 752 | * @param CustomerInterface $customer |
||
| 753 | * @param ChannelInterface|null $channel |
||
| 754 | * @param string|null $localeCode |
||
| 755 | * |
||
| 756 | * @return OrderInterface |
||
| 757 | */ |
||
| 758 | private function createCart( |
||
| 773 | |||
| 774 | /** |
||
| 775 | * @param int $count |
||
| 776 | * |
||
| 777 | * @return CustomerInterface[] |
||
| 778 | */ |
||
| 779 | private function generateCustomers($count) |
||
| 796 | |||
| 797 | /** |
||
| 798 | * @param string $price |
||
| 799 | * |
||
| 800 | * @return int |
||
| 801 | */ |
||
| 802 | private function getPriceFromString($price) |
||
| 806 | |||
| 807 | /** |
||
| 808 | * @param OrderInterface $order |
||
| 809 | * @param ShippingMethodInterface $shippingMethod |
||
| 810 | * @param AddressInterface $address |
||
| 811 | * @param PaymentMethodInterface $paymentMethod |
||
| 812 | */ |
||
| 813 | private function checkoutUsing( |
||
| 826 | |||
| 827 | /** |
||
| 828 | * @param OrderInterface $order |
||
| 829 | * @param ShippingMethodInterface $shippingMethod |
||
| 830 | * @param PaymentMethodInterface $paymentMethod |
||
| 831 | */ |
||
| 832 | private function proceedSelectingShippingAndPaymentMethod(OrderInterface $order, ShippingMethodInterface $shippingMethod, PaymentMethodInterface $paymentMethod) |
||
| 845 | |||
| 846 | /** |
||
| 847 | * @param OrderInterface $order |
||
| 848 | * @param ProductVariantInterface $variant |
||
| 849 | * @param int $price |
||
| 850 | */ |
||
| 851 | private function addVariantWithPriceToOrder(OrderInterface $order, ProductVariantInterface $variant, $price) |
||
| 861 | |||
| 862 | /** |
||
| 863 | * @param int $numberOfCustomers |
||
| 864 | * @param int $numberOfOrders |
||
| 865 | * @param string $total |
||
| 866 | * @param bool $isFulfilled |
||
| 867 | */ |
||
| 868 | private function createOrders( |
||
| 898 | |||
| 899 | /** |
||
| 900 | * @param int $numberOfCustomers |
||
| 901 | * @param int $numberOfOrders |
||
| 902 | * @param string $total |
||
| 903 | * @param ProductInterface $product |
||
| 904 | * @param bool $isFulfilled |
||
| 905 | */ |
||
| 906 | private function createOrdersWithProduct( |
||
| 936 | |||
| 937 | /** |
||
| 938 | * @param CustomerInterface $customer |
||
| 939 | * @param int $orderCount |
||
| 940 | * @param ChannelInterface $channel |
||
| 941 | * @param int $productCount |
||
| 942 | * @param ProductInterface $product |
||
| 943 | * @param bool $isFulfilled |
||
| 944 | */ |
||
| 945 | private function createOrdersForCustomer( |
||
| 970 | |||
| 971 | /** |
||
| 972 | * @param ProductInterface $product |
||
| 973 | * @param ShippingMethodInterface $shippingMethod |
||
| 974 | * @param AddressInterface $address |
||
| 975 | * @param PaymentMethodInterface $paymentMethod |
||
| 976 | * @param CustomerInterface $customer |
||
| 977 | * @param int $number |
||
| 978 | */ |
||
| 979 | private function placeOrder( |
||
| 1009 | } |
||
| 1010 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: