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 RepositoryInterface |
||
| 74 | */ |
||
| 75 | private $currencyRepository; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var FactoryInterface |
||
| 79 | */ |
||
| 80 | private $customerFactory; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var RepositoryInterface |
||
| 84 | */ |
||
| 85 | private $customerRepository; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var ObjectManager |
||
| 89 | */ |
||
| 90 | private $objectManager; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var StateMachineFactoryInterface |
||
| 94 | */ |
||
| 95 | private $stateMachineFactory; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var ProductVariantResolverInterface |
||
| 99 | */ |
||
| 100 | private $variantResolver; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param SharedStorageInterface $sharedStorage |
||
| 104 | * @param OrderRepositoryInterface $orderRepository |
||
| 105 | * @param FactoryInterface $orderFactory |
||
| 106 | * @param FactoryInterface $orderItemFactory |
||
| 107 | * @param OrderItemQuantityModifierInterface $itemQuantityModifier |
||
| 108 | * @param RepositoryInterface $currencyRepository |
||
| 109 | * @param FactoryInterface $customerFactory |
||
| 110 | * @param RepositoryInterface $customerRepository |
||
| 111 | * @param ObjectManager $objectManager |
||
| 112 | * @param StateMachineFactoryInterface $stateMachineFactory |
||
| 113 | * @param ProductVariantResolverInterface $variantResolver |
||
| 114 | */ |
||
| 115 | public function __construct( |
||
| 116 | SharedStorageInterface $sharedStorage, |
||
| 117 | OrderRepositoryInterface $orderRepository, |
||
| 118 | FactoryInterface $orderFactory, |
||
| 119 | FactoryInterface $orderItemFactory, |
||
| 120 | OrderItemQuantityModifierInterface $itemQuantityModifier, |
||
| 121 | RepositoryInterface $currencyRepository, |
||
| 122 | FactoryInterface $customerFactory, |
||
| 123 | RepositoryInterface $customerRepository, |
||
| 124 | ObjectManager $objectManager, |
||
| 125 | StateMachineFactoryInterface $stateMachineFactory, |
||
| 126 | ProductVariantResolverInterface $variantResolver |
||
| 127 | ) { |
||
| 128 | $this->sharedStorage = $sharedStorage; |
||
| 129 | $this->orderRepository = $orderRepository; |
||
| 130 | $this->orderFactory = $orderFactory; |
||
| 131 | $this->orderItemFactory = $orderItemFactory; |
||
| 132 | $this->itemQuantityModifier = $itemQuantityModifier; |
||
| 133 | $this->currencyRepository = $currencyRepository; |
||
| 134 | $this->customerFactory = $customerFactory; |
||
| 135 | $this->customerRepository = $customerRepository; |
||
| 136 | $this->objectManager = $objectManager; |
||
| 137 | $this->stateMachineFactory = $stateMachineFactory; |
||
| 138 | $this->variantResolver = $variantResolver; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @Given /^there is (?:a|another) (customer "[^"]+") that placed an order$/ |
||
| 143 | * @Given /^there is (?:a|another) (customer "[^"]+") that placed (an order "[^"]+")$/ |
||
| 144 | * @Given a customer :customer placed an order :orderNumber |
||
| 145 | * @Given the customer :customer has already placed an order :orderNumber |
||
| 146 | */ |
||
| 147 | public function thereIsCustomerThatPlacedOrder(CustomerInterface $customer, $orderNumber = null) |
||
| 148 | { |
||
| 149 | $order = $this->createOrder($customer, $orderNumber); |
||
| 150 | |||
| 151 | $this->sharedStorage->set('order', $order); |
||
| 152 | |||
| 153 | $this->orderRepository->add($order); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @Given a customer :customer added something to cart |
||
| 158 | */ |
||
| 159 | public function customerStartedCheckout(CustomerInterface $customer) |
||
| 160 | { |
||
| 161 | $cart = $this->createCart($customer); |
||
| 162 | |||
| 163 | $this->sharedStorage->set('cart', $cart); |
||
| 164 | |||
| 165 | $this->orderRepository->add($cart); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @Given /^(I) placed (an order "[^"]+")$/ |
||
| 170 | */ |
||
| 171 | public function iPlacedAnOrder(UserInterface $user, $orderNumber) |
||
| 172 | { |
||
| 173 | $customer = $user->getCustomer(); |
||
|
|
|||
| 174 | $order = $this->createOrder($customer, $orderNumber); |
||
| 175 | |||
| 176 | $this->sharedStorage->set('order', $order); |
||
| 177 | |||
| 178 | $this->orderRepository->add($order); |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @Given /^the customer ("[^"]+" addressed it to "[^"]+", "[^"]+" "[^"]+" in the "[^"]+"(?:|, "[^"]+"))$/ |
||
| 183 | * @Given /^I (addressed it to "[^"]+", "[^"]+", "[^"]+" "[^"]+" in the "[^"]+"(?:|, "[^"]+"))$/ |
||
| 184 | */ |
||
| 185 | public function theCustomerAddressedItTo(AddressInterface $address) |
||
| 186 | { |
||
| 187 | /** @var OrderInterface $order */ |
||
| 188 | $order = $this->sharedStorage->get('order'); |
||
| 189 | $order->setShippingAddress($address); |
||
| 190 | |||
| 191 | $this->objectManager->flush(); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @Given the customer changed shipping address' street to :street |
||
| 196 | */ |
||
| 197 | public function theCustomerChangedShippingAddressStreetTo($street) |
||
| 198 | { |
||
| 199 | /** @var OrderInterface $order */ |
||
| 200 | $order = $this->sharedStorage->get('order'); |
||
| 201 | |||
| 202 | $shippingAddress = $order->getShippingAddress(); |
||
| 203 | $shippingAddress->setStreet($street); |
||
| 204 | |||
| 205 | $this->objectManager->flush(); |
||
| 206 | |||
| 207 | $this->applyTransitionOnOrderCheckout($order, OrderCheckoutTransitions::TRANSITION_ADDRESS); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @Given /^the customer set the billing (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)")$/ |
||
| 212 | * @Given /^for the billing address (of "[^"]+" in the "[^"]+", "[^"]+" "[^"]+", "[^"]+")$/ |
||
| 213 | * @Given /^for the billing address (of "[^"]+" in the "[^"]+", "[^"]+" "([^"]+)", "[^"]+", "[^"]+")$/ |
||
| 214 | */ |
||
| 215 | public function forTheBillingAddressOf(AddressInterface $address) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @Given /^the customer ("[^"]+" addressed it to "[^"]+", "[^"]+" "[^"]+" in the "[^"]+") with identical billing address$/ |
||
| 229 | * @Given /^I (addressed it to "[^"]+", "[^"]+", "[^"]+" "[^"]+" in the "[^"]+") with identical billing address$/ |
||
| 230 | */ |
||
| 231 | public function theCustomerAddressedItToWithIdenticalBillingAddress(AddressInterface $address) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @Given /^the customer chose ("[^"]+" shipping method) (to "[^"]+") with ("[^"]+" payment)$/ |
||
| 239 | * @Given /^I chose ("[^"]+" shipping method) (to "[^"]+") with ("[^"]+" payment)$/ |
||
| 240 | */ |
||
| 241 | public function theCustomerChoseShippingToWithPayment( |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @Given /^the customer chose ("[^"]+" shipping method) with ("[^"]+" payment)$/ |
||
| 256 | * @Given /^I chose ("[^"]+" shipping method) with ("[^"]+" payment)$/ |
||
| 257 | */ |
||
| 258 | public function theCustomerChoseShippingWithPayment( |
||
| 259 | ShippingMethodInterface $shippingMethod, |
||
| 260 | PaymentMethodInterface $paymentMethod |
||
| 261 | ) { |
||
| 262 | /** @var OrderInterface $order */ |
||
| 263 | $order = $this->sharedStorage->get('order'); |
||
| 264 | |||
| 265 | $this->proceedSelectingShippingAndPaymentMethod($order, $shippingMethod, $paymentMethod); |
||
| 266 | |||
| 267 | $this->objectManager->flush(); |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @Given the customer bought a single :product |
||
| 272 | * @Given I bought a single :product |
||
| 273 | */ |
||
| 274 | public function theCustomerBoughtSingleProduct(ProductInterface $product) |
||
| 275 | { |
||
| 276 | $this->addProductVariantToOrder($this->variantResolver->getVariant($product), 1); |
||
| 277 | |||
| 278 | $this->objectManager->flush(); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @Given /^the customer bought ((?:a|an) "[^"]+") and ((?:a|an) "[^"]+")$/ |
||
| 283 | * @Given /^I bought ((?:a|an) "[^"]+") and ((?:a|an) "[^"]+")$/ |
||
| 284 | */ |
||
| 285 | public function theCustomerBoughtProductAndProduct(ProductInterface $product, ProductInterface $secondProduct) |
||
| 286 | { |
||
| 287 | $this->theCustomerBoughtSingleProduct($product); |
||
| 288 | $this->theCustomerBoughtSingleProduct($secondProduct); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @Given /^the customer bought (\d+) ("[^"]+" products)$/ |
||
| 293 | */ |
||
| 294 | public function theCustomerBoughtSeveralProducts($quantity, ProductInterface $product) |
||
| 295 | { |
||
| 296 | $variant = $this->variantResolver->getVariant($product); |
||
| 297 | $this->addProductVariantToOrder($variant, $quantity); |
||
| 298 | |||
| 299 | $this->objectManager->flush(); |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @Given /^the customer bought ([^"]+) units of ("[^"]+" variant of product "[^"]+")$/ |
||
| 304 | */ |
||
| 305 | public function theCustomerBoughtSeveralVariantsOfProduct($quantity, ProductVariantInterface $variant) |
||
| 306 | { |
||
| 307 | $this->addProductVariantToOrder($variant, $quantity); |
||
| 308 | |||
| 309 | $this->objectManager->flush(); |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @Given /^the customer bought a single ("[^"]+" variant of product "[^"]+")$/ |
||
| 314 | */ |
||
| 315 | public function theCustomerBoughtSingleProductVariant(ProductVariantInterface $productVariant) |
||
| 316 | { |
||
| 317 | $this->addProductVariantToOrder($productVariant); |
||
| 318 | |||
| 319 | $this->objectManager->flush(); |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @Given the customer bought a single :product using :coupon coupon |
||
| 324 | * @Given I bought a single :product using :coupon coupon |
||
| 325 | */ |
||
| 326 | public function theCustomerBoughtSingleUsing(ProductInterface $product, PromotionCouponInterface $coupon) |
||
| 327 | { |
||
| 328 | $order = $this->addProductVariantToOrder($this->variantResolver->getVariant($product)); |
||
| 329 | $order->setPromotionCoupon($coupon); |
||
| 330 | |||
| 331 | $this->objectManager->flush(); |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @Given I used :coupon coupon |
||
| 336 | */ |
||
| 337 | public function iUsedCoupon(PromotionCouponInterface $coupon) |
||
| 338 | { |
||
| 339 | $order = $this->sharedStorage->get('order'); |
||
| 340 | $order->setPromotionCoupon($coupon); |
||
| 341 | |||
| 342 | $this->objectManager->flush(); |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @Given /^(I) have already placed (\d+) orders choosing ("[^"]+" shipping method) (to "[^"]+") with ("[^"]+" payment)$/ |
||
| 347 | */ |
||
| 348 | public function iHaveAlreadyPlacedOrderNthTimes( |
||
| 349 | UserInterface $user, |
||
| 350 | $numberOfOrders, |
||
| 351 | ShippingMethodInterface $shippingMethod, |
||
| 352 | AddressInterface $address, |
||
| 353 | PaymentMethodInterface $paymentMethod |
||
| 354 | ) { |
||
| 355 | $customer = $user->getCustomer(); |
||
| 356 | for ($i = 0; $i < $numberOfOrders; $i++) { |
||
| 357 | $order = $this->createOrder($customer, '#00000'.$i); |
||
| 358 | $this->checkoutUsing($order, $shippingMethod, clone $address, $paymentMethod); |
||
| 359 | $this->applyPaymentTransitionOnOrder($order, PaymentTransitions::TRANSITION_COMPLETE); |
||
| 360 | |||
| 361 | $this->objectManager->persist($order); |
||
| 362 | $this->sharedStorage->set('order', $order); |
||
| 363 | } |
||
| 364 | |||
| 365 | $this->objectManager->flush(); |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @Given /^(this customer) has(?:| also) placed (an order "[^"]+") at "([^"]+)"$/ |
||
| 370 | */ |
||
| 371 | public function thisCustomerHasPlacedAnOrderAtDate(CustomerInterface $customer, $number, $checkoutCompletedAt) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @Given /^(this customer) has(?:| also) placed (an order "[^"]+") on a (channel "[^"]+")$/ |
||
| 382 | */ |
||
| 383 | public function thisCustomerHasPlacedAnOrderOnAChannel(CustomerInterface $customer, $number, $channel) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @Given /^(customer "[^"]+"|this customer) has(?:| also) placed (\d+) orders on the ("[^"]+" channel) in each buying (\d+) ("[^"]+" products?)$/ |
||
| 393 | */ |
||
| 394 | public function thisCustomerPlacedOrdersOnChannelBuyingProducts( |
||
| 395 | CustomerInterface $customer, |
||
| 396 | $orderCount, |
||
| 397 | ChannelInterface $channel, |
||
| 398 | $productCount, |
||
| 399 | ProductInterface $product |
||
| 400 | ) { |
||
| 401 | for ($i = 0; $i < $orderCount; $i++) { |
||
| 402 | $order = $this->createOrder($customer, uniqid('#'), $channel, $channel->getBaseCurrency()->getCode()); |
||
| 403 | |||
| 404 | $this->addProductVariantsToOrderWithChannelPrice( |
||
| 405 | $order, |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @Given :numberOfCustomers customers have added products to the cart for total of :total |
||
| 421 | */ |
||
| 422 | public function customersHaveAddedProductsToTheCartForTotalOf($numberOfCustomers, $total) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @Given :numberOfCustomers customers have placed :numberOfOrders orders for total of :total |
||
| 445 | * @Given then :numberOfCustomers more customers have placed :numberOfOrders orders for total of :total |
||
| 446 | */ |
||
| 447 | public function customersHavePlacedOrdersForTotalOf($numberOfCustomers, $numberOfOrders, $total) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @Given :numberOfCustomers customers have placed :numberOfOrders orders for total of :total mostly :product product |
||
| 472 | * @Given then :numberOfCustomers more customers have placed :numberOfOrders orders for total of :total mostly :product product |
||
| 473 | */ |
||
| 474 | public function customersHavePlacedOrdersForTotalOfMostlyProduct($numberOfCustomers, $numberOfOrders, $total, ProductInterface $product) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @Given /^(this customer) has(?:| also) placed (an order "[^"]+") buying a single ("[^"]+" product) for ("[^"]+") in ("[^"]+" currency)$/ |
||
| 498 | */ |
||
| 499 | public function customerHasPlacedAnOrderBuyingASingleProductForInCurrency( |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @Given /^(this order) is already paid$/ |
||
| 516 | * @Given the order :order is already paid |
||
| 517 | */ |
||
| 518 | public function thisOrderIsAlreadyPaid(OrderInterface $order) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @Given /^the customer cancelled (this order)$/ |
||
| 527 | * @Given /^(this order) was cancelled$/ |
||
| 528 | * @Given the order :order was cancelled |
||
| 529 | * @Given /^I cancelled (this order)$/ |
||
| 530 | */ |
||
| 531 | public function theCustomerCancelledThisOrder(OrderInterface $order) |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @Given /^I cancelled my last order$/ |
||
| 540 | */ |
||
| 541 | public function theCustomerCancelledMyLastOrder() |
||
| 548 | |||
| 549 | /** |
||
| 550 | * @Given /^(this order) has already been shipped$/ |
||
| 551 | */ |
||
| 552 | public function thisOrderHasAlreadyBeenShipped(OrderInterface $order) |
||
| 558 | |||
| 559 | /** |
||
| 560 | * @param OrderInterface $order |
||
| 561 | * @param string $transition |
||
| 562 | */ |
||
| 563 | private function applyShipmentTransitionOnOrder(OrderInterface $order, $transition) |
||
| 569 | |||
| 570 | /** |
||
| 571 | * @param OrderInterface $order |
||
| 572 | * @param string $transition |
||
| 573 | */ |
||
| 574 | private function applyPaymentTransitionOnOrder(OrderInterface $order, $transition) |
||
| 580 | |||
| 581 | /** |
||
| 582 | * @param OrderInterface $order |
||
| 583 | * @param string $transition |
||
| 584 | */ |
||
| 585 | private function applyTransitionOnOrderCheckout(OrderInterface $order, $transition) |
||
| 589 | |||
| 590 | /** |
||
| 591 | * @param ProductVariantInterface $productVariant |
||
| 592 | * @param int $quantity |
||
| 593 | * |
||
| 594 | * @return OrderInterface |
||
| 595 | */ |
||
| 596 | private function addProductVariantToOrder(ProductVariantInterface $productVariant, $quantity = 1) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * @param OrderInterface $order |
||
| 612 | * @param ChannelInterface $channel |
||
| 613 | * @param ProductVariantInterface $productVariant |
||
| 614 | * @param int $quantity |
||
| 615 | */ |
||
| 616 | private function addProductVariantsToOrderWithChannelPrice( |
||
| 634 | |||
| 635 | /** |
||
| 636 | * @param CustomerInterface $customer |
||
| 637 | * @param string $number |
||
| 638 | * @param ChannelInterface|null $channel |
||
| 639 | * @param string|null $currencyCode |
||
| 640 | * @param string|null $localeCode |
||
| 641 | * |
||
| 642 | * @return OrderInterface |
||
| 643 | */ |
||
| 644 | private function createOrder( |
||
| 661 | |||
| 662 | /** |
||
| 663 | * @param CustomerInterface $customer |
||
| 664 | * @param ChannelInterface|null $channel |
||
| 665 | * @param string|null $currencyCode |
||
| 666 | * @param string|null $localeCode |
||
| 667 | * |
||
| 668 | * @return OrderInterface |
||
| 669 | */ |
||
| 670 | private function createCart( |
||
| 690 | |||
| 691 | /** |
||
| 692 | * @param int $count |
||
| 693 | * |
||
| 694 | * @return CustomerInterface[] |
||
| 695 | */ |
||
| 696 | private function generateCustomers($count) |
||
| 713 | |||
| 714 | /** |
||
| 715 | * @param string $price |
||
| 716 | * |
||
| 717 | * @return int |
||
| 718 | */ |
||
| 719 | private function getPriceFromString($price) |
||
| 723 | |||
| 724 | /** |
||
| 725 | * @param OrderInterface $order |
||
| 726 | * @param ShippingMethodInterface $shippingMethod |
||
| 727 | * @param AddressInterface $address |
||
| 728 | * @param PaymentMethodInterface $paymentMethod |
||
| 729 | */ |
||
| 730 | private function checkoutUsing( |
||
| 743 | |||
| 744 | /** |
||
| 745 | * @param OrderInterface $order |
||
| 746 | * @param ShippingMethodInterface $shippingMethod |
||
| 747 | * @param PaymentMethodInterface $paymentMethod |
||
| 748 | */ |
||
| 749 | private function proceedSelectingShippingAndPaymentMethod(OrderInterface $order, ShippingMethodInterface $shippingMethod, PaymentMethodInterface $paymentMethod) |
||
| 762 | |||
| 763 | /** |
||
| 764 | * @param OrderInterface $order |
||
| 765 | * @param ProductVariantInterface $variant |
||
| 766 | * @param int $price |
||
| 767 | */ |
||
| 768 | private function addVariantWithPriceToOrder(OrderInterface $order, ProductVariantInterface $variant, $price) |
||
| 778 | } |
||
| 779 |
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: