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 |
||
| 45 | final class OrderContext implements Context |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * @var SharedStorageInterface |
||
| 49 | */ |
||
| 50 | private $sharedStorage; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var OrderRepositoryInterface |
||
| 54 | */ |
||
| 55 | private $orderRepository; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var FactoryInterface |
||
| 59 | */ |
||
| 60 | private $orderFactory; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var FactoryInterface |
||
| 64 | */ |
||
| 65 | private $orderItemFactory; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var OrderItemQuantityModifierInterface |
||
| 69 | */ |
||
| 70 | private $itemQuantityModifier; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var FactoryInterface |
||
| 74 | */ |
||
| 75 | private $customerFactory; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var RepositoryInterface |
||
| 79 | */ |
||
| 80 | private $customerRepository; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var ObjectManager |
||
| 84 | */ |
||
| 85 | private $objectManager; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var StateMachineFactoryInterface |
||
| 89 | */ |
||
| 90 | private $stateMachineFactory; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var ProductVariantResolverInterface |
||
| 94 | */ |
||
| 95 | private $variantResolver; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param SharedStorageInterface $sharedStorage |
||
| 99 | * @param OrderRepositoryInterface $orderRepository |
||
| 100 | * @param FactoryInterface $orderFactory |
||
| 101 | * @param FactoryInterface $orderItemFactory |
||
| 102 | * @param OrderItemQuantityModifierInterface $itemQuantityModifier |
||
| 103 | * @param FactoryInterface $customerFactory |
||
| 104 | * @param RepositoryInterface $customerRepository |
||
| 105 | * @param ObjectManager $objectManager |
||
| 106 | * @param StateMachineFactoryInterface $stateMachineFactory |
||
| 107 | * @param ProductVariantResolverInterface $variantResolver |
||
| 108 | */ |
||
| 109 | public function __construct( |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @Given /^there is (?:a|another) (customer "[^"]+") that placed an order$/ |
||
| 135 | * @Given /^there is (?:a|another) (customer "[^"]+") that placed (an order "[^"]+")$/ |
||
| 136 | * @Given a customer :customer placed an order :orderNumber |
||
| 137 | * @Given the customer :customer has already placed an order :orderNumber |
||
| 138 | */ |
||
| 139 | public function thereIsCustomerThatPlacedOrder(CustomerInterface $customer, $orderNumber = null) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @Given a customer :customer added something to cart |
||
| 150 | */ |
||
| 151 | public function customerStartedCheckout(CustomerInterface $customer) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @Given /^(I) placed (an order "[^"]+")$/ |
||
| 162 | */ |
||
| 163 | public function iPlacedAnOrder(UserInterface $user, $orderNumber) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @Given /^the customer ("[^"]+" addressed it to "[^"]+", "[^"]+" "[^"]+" in the "[^"]+"(?:|, "[^"]+"))$/ |
||
| 175 | * @Given /^I (addressed it to "[^"]+", "[^"]+", "[^"]+" "[^"]+" in the "[^"]+"(?:|, "[^"]+"))$/ |
||
| 176 | */ |
||
| 177 | public function theCustomerAddressedItTo(AddressInterface $address) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @Given the customer changed shipping address' street to :street |
||
| 188 | */ |
||
| 189 | public function theCustomerChangedShippingAddressStreetTo($street) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @Given /^the customer set the billing (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)")$/ |
||
| 204 | * @Given /^for the billing address (of "[^"]+" in the "[^"]+", "[^"]+" "[^"]+", "[^"]+")$/ |
||
| 205 | * @Given /^for the billing address (of "[^"]+" in the "[^"]+", "[^"]+" "([^"]+)", "[^"]+", "[^"]+")$/ |
||
| 206 | */ |
||
| 207 | public function forTheBillingAddressOf(AddressInterface $address) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @Given /^the customer ("[^"]+" addressed it to "[^"]+", "[^"]+" "[^"]+" in the "[^"]+") with identical billing address$/ |
||
| 221 | * @Given /^I (addressed it to "[^"]+", "[^"]+", "[^"]+" "[^"]+" in the "[^"]+") with identical billing address$/ |
||
| 222 | */ |
||
| 223 | public function theCustomerAddressedItToWithIdenticalBillingAddress(AddressInterface $address) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @Given /^the customer chose ("[^"]+" shipping method) (to "[^"]+") with ("[^"]+" payment)$/ |
||
| 231 | * @Given /^I chose ("[^"]+" shipping method) (to "[^"]+") with ("[^"]+" payment)$/ |
||
| 232 | */ |
||
| 233 | public function theCustomerChoseShippingToWithPayment( |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @Given /^the customer chose ("[^"]+" shipping method) with ("[^"]+" payment)$/ |
||
| 248 | * @Given /^I chose ("[^"]+" shipping method) with ("[^"]+" payment)$/ |
||
| 249 | */ |
||
| 250 | public function theCustomerChoseShippingWithPayment( |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @Given /^the customer chose ("[^"]+" shipping method)$/ |
||
| 264 | */ |
||
| 265 | public function theCustomerChoseShippingMethod(ShippingMethodInterface $shippingMethod) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @Given the customer bought a single :product |
||
| 282 | * @Given I bought a single :product |
||
| 283 | */ |
||
| 284 | public function theCustomerBoughtSingleProduct(ProductInterface $product) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @Given /^the customer bought ((?:a|an) "[^"]+") and ((?:a|an) "[^"]+")$/ |
||
| 293 | * @Given /^I bought ((?:a|an) "[^"]+") and ((?:a|an) "[^"]+")$/ |
||
| 294 | */ |
||
| 295 | public function theCustomerBoughtProductAndProduct(ProductInterface $product, ProductInterface $secondProduct) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @Given /^the customer bought (\d+) ("[^"]+" products)$/ |
||
| 303 | */ |
||
| 304 | public function theCustomerBoughtSeveralProducts($quantity, ProductInterface $product) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @Given /^the customer bought ([^"]+) units of ("[^"]+" variant of product "[^"]+")$/ |
||
| 314 | */ |
||
| 315 | public function theCustomerBoughtSeveralVariantsOfProduct($quantity, ProductVariantInterface $variant) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @Given /^the customer bought a single ("[^"]+" variant of product "[^"]+")$/ |
||
| 324 | */ |
||
| 325 | public function theCustomerBoughtSingleProductVariant(ProductVariantInterface $productVariant) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @Given the customer bought a single :product using :coupon coupon |
||
| 334 | * @Given I bought a single :product using :coupon coupon |
||
| 335 | */ |
||
| 336 | public function theCustomerBoughtSingleUsing(ProductInterface $product, PromotionCouponInterface $coupon) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @Given I used :coupon coupon |
||
| 346 | */ |
||
| 347 | public function iUsedCoupon(PromotionCouponInterface $coupon) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @Given /^(I) have already placed (\d+) orders choosing ("[^"]+" product), ("[^"]+" shipping method) (to "[^"]+") with ("[^"]+" payment)$/ |
||
| 357 | */ |
||
| 358 | public function iHaveAlreadyPlacedOrderNthTimes( |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @Given /^(this customer) has(?:| also) placed (an order "[^"]+") at "([^"]+)"$/ |
||
| 396 | */ |
||
| 397 | public function thisCustomerHasPlacedAnOrderAtDate(CustomerInterface $customer, $number, $checkoutCompletedAt) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @Given /^(this customer) has(?:| also) placed (an order "[^"]+") on a (channel "[^"]+")$/ |
||
| 408 | */ |
||
| 409 | public function thisCustomerHasPlacedAnOrderOnAChannel(CustomerInterface $customer, $number, $channel) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @Given /^(customer "[^"]+"|this customer) has(?:| also) placed (\d+) orders on the ("[^"]+" channel) in each buying (\d+) ("[^"]+" products?)$/ |
||
| 419 | */ |
||
| 420 | public function thisCustomerPlacedOrdersOnChannelBuyingProducts( |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @Given :numberOfCustomers customers have added products to the cart for total of :total |
||
| 447 | */ |
||
| 448 | public function customersHaveAddedProductsToTheCartForTotalOf($numberOfCustomers, $total) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @Given :numberOfCustomers customers have placed :numberOfOrders orders for total of :total |
||
| 471 | * @Given then :numberOfCustomers more customers have placed :numberOfOrders orders for total of :total |
||
| 472 | */ |
||
| 473 | public function customersHavePlacedOrdersForTotalOf($numberOfCustomers, $numberOfOrders, $total) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @Given :numberOfCustomers customers have placed :numberOfOrders orders for total of :total mostly :product product |
||
| 498 | * @Given then :numberOfCustomers more customers have placed :numberOfOrders orders for total of :total mostly :product product |
||
| 499 | */ |
||
| 500 | public function customersHavePlacedOrdersForTotalOfMostlyProduct($numberOfCustomers, $numberOfOrders, $total, ProductInterface $product) |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @Given /^(this customer) has(?:| also) placed (an order "[^"]+") buying a single ("[^"]+" product) for ("[^"]+") on the ("[^"]+" channel)$/ |
||
| 524 | */ |
||
| 525 | public function customerHasPlacedAnOrderBuyingASingleProductForOnTheChannel( |
||
| 526 | CustomerInterface $customer, |
||
| 527 | $orderNumber, |
||
| 528 | ProductInterface $product, |
||
| 529 | $price, |
||
| 530 | ChannelInterface $channel |
||
| 531 | ) { |
||
| 532 | $order = $this->createOrder($customer, $orderNumber, $channel); |
||
| 533 | $order->setState(OrderInterface::STATE_NEW); |
||
| 534 | |||
| 535 | $this->addVariantWithPriceToOrder($order, $product->getVariants()->first(), $price); |
||
| 536 | |||
| 537 | $this->orderRepository->add($order); |
||
| 538 | } |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @Given /^(this order) is already paid$/ |
||
| 542 | * @Given the order :order is already paid |
||
| 543 | */ |
||
| 544 | public function thisOrderIsAlreadyPaid(OrderInterface $order) |
||
| 545 | { |
||
| 546 | $this->applyPaymentTransitionOnOrder($order, PaymentTransitions::TRANSITION_COMPLETE); |
||
| 547 | |||
| 548 | $this->objectManager->flush(); |
||
| 549 | } |
||
| 550 | |||
| 551 | /** |
||
| 552 | * @Given /^(this order) has been refunded$/ |
||
| 553 | */ |
||
| 554 | public function thisOrderHasBeenRefunded(OrderInterface $order) |
||
| 555 | { |
||
| 556 | $this->applyPaymentTransitionOnOrder($order, PaymentTransitions::TRANSITION_REFUND); |
||
| 557 | |||
| 558 | $this->objectManager->flush(); |
||
| 559 | } |
||
| 560 | |||
| 561 | /** |
||
| 562 | * @Given /^the customer cancelled (this order)$/ |
||
| 563 | * @Given /^(this order) was cancelled$/ |
||
| 564 | * @Given the order :order was cancelled |
||
| 565 | * @Given /^I cancelled (this order)$/ |
||
| 566 | */ |
||
| 567 | public function theCustomerCancelledThisOrder(OrderInterface $order) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @Given /^I cancelled my last order$/ |
||
| 576 | */ |
||
| 577 | public function theCustomerCancelledMyLastOrder() |
||
| 584 | |||
| 585 | /** |
||
| 586 | * @Given /^(this order) has already been shipped$/ |
||
| 587 | */ |
||
| 588 | public function thisOrderHasAlreadyBeenShipped(OrderInterface $order) |
||
| 594 | |||
| 595 | /** |
||
| 596 | * @param OrderInterface $order |
||
| 597 | * @param string $transition |
||
| 598 | */ |
||
| 599 | private function applyShipmentTransitionOnOrder(OrderInterface $order, $transition) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * @param OrderInterface $order |
||
| 608 | * @param string $transition |
||
| 609 | */ |
||
| 610 | private function applyPaymentTransitionOnOrder(OrderInterface $order, $transition) |
||
| 616 | |||
| 617 | /** |
||
| 618 | * @param OrderInterface $order |
||
| 619 | * @param string $transition |
||
| 620 | */ |
||
| 621 | private function applyTransitionOnOrderCheckout(OrderInterface $order, $transition) |
||
| 625 | |||
| 626 | /** |
||
| 627 | * @param ProductVariantInterface $productVariant |
||
| 628 | * @param int $quantity |
||
| 629 | * |
||
| 630 | * @return OrderInterface |
||
| 631 | */ |
||
| 632 | private function addProductVariantToOrder(ProductVariantInterface $productVariant, $quantity = 1) |
||
| 645 | |||
| 646 | /** |
||
| 647 | * @param OrderInterface $order |
||
| 648 | * @param ChannelInterface $channel |
||
| 649 | * @param ProductVariantInterface $productVariant |
||
| 650 | * @param int $quantity |
||
| 651 | */ |
||
| 652 | private function addProductVariantsToOrderWithChannelPrice( |
||
| 670 | |||
| 671 | /** |
||
| 672 | * @param CustomerInterface $customer |
||
| 673 | * @param string $number |
||
| 674 | * @param ChannelInterface|null $channel |
||
| 675 | * @param string|null $localeCode |
||
| 676 | * |
||
| 677 | * @return OrderInterface |
||
| 678 | */ |
||
| 679 | private function createOrder( |
||
| 695 | |||
| 696 | /** |
||
| 697 | * @param CustomerInterface $customer |
||
| 698 | * @param ChannelInterface|null $channel |
||
| 699 | * @param string|null $localeCode |
||
| 700 | * |
||
| 701 | * @return OrderInterface |
||
| 702 | */ |
||
| 703 | private function createCart( |
||
| 718 | |||
| 719 | /** |
||
| 720 | * @param int $count |
||
| 721 | * |
||
| 722 | * @return CustomerInterface[] |
||
| 723 | */ |
||
| 724 | private function generateCustomers($count) |
||
| 725 | { |
||
| 726 | $customers = []; |
||
| 727 | |||
| 728 | for ($i = 0; $i < $count; $i++) { |
||
| 729 | $customer = $this->customerFactory->createNew(); |
||
| 730 | $customer->setEmail(sprintf('john%[email protected]', uniqid())); |
||
| 731 | $customer->setFirstname('John'); |
||
| 732 | $customer->setLastname('Doe'.$i); |
||
| 733 | |||
| 734 | $customers[] = $customer; |
||
| 735 | |||
| 736 | $this->customerRepository->add($customer); |
||
| 737 | } |
||
| 738 | |||
| 739 | return $customers; |
||
| 740 | } |
||
| 741 | |||
| 742 | /** |
||
| 743 | * @param string $price |
||
| 744 | * |
||
| 745 | * @return int |
||
| 746 | */ |
||
| 747 | private function getPriceFromString($price) |
||
| 751 | |||
| 752 | /** |
||
| 753 | * @param OrderInterface $order |
||
| 754 | * @param ShippingMethodInterface $shippingMethod |
||
| 755 | * @param AddressInterface $address |
||
| 756 | * @param PaymentMethodInterface $paymentMethod |
||
| 757 | */ |
||
| 758 | private function checkoutUsing( |
||
| 771 | |||
| 772 | /** |
||
| 773 | * @param OrderInterface $order |
||
| 774 | * @param ShippingMethodInterface $shippingMethod |
||
| 775 | * @param PaymentMethodInterface $paymentMethod |
||
| 776 | */ |
||
| 777 | private function proceedSelectingShippingAndPaymentMethod(OrderInterface $order, ShippingMethodInterface $shippingMethod, PaymentMethodInterface $paymentMethod) |
||
| 790 | |||
| 791 | /** |
||
| 792 | * @param OrderInterface $order |
||
| 793 | * @param ProductVariantInterface $variant |
||
| 794 | * @param int $price |
||
| 795 | */ |
||
| 796 | private function addVariantWithPriceToOrder(OrderInterface $order, ProductVariantInterface $variant, $price) |
||
| 806 | } |
||
| 807 |
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: