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 OrderProcessorInterface |
||
| 66 | */ |
||
| 67 | private $orderProcessor; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var FactoryInterface |
||
| 71 | */ |
||
| 72 | private $orderItemFactory; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var OrderItemQuantityModifierInterface |
||
| 76 | */ |
||
| 77 | private $itemQuantityModifier; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var RepositoryInterface |
||
| 81 | */ |
||
| 82 | private $currencyRepository; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var CurrencyStorageInterface |
||
| 86 | */ |
||
| 87 | private $currencyStorage; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var FactoryInterface |
||
| 91 | */ |
||
| 92 | private $customerFactory; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var RepositoryInterface |
||
| 96 | */ |
||
| 97 | private $customerRepository; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var ObjectManager |
||
| 101 | */ |
||
| 102 | private $objectManager; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var StateMachineFactoryInterface |
||
| 106 | */ |
||
| 107 | private $stateMachineFactory; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var ProductVariantResolverInterface |
||
| 111 | */ |
||
| 112 | private $variantResolver; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param SharedStorageInterface $sharedStorage |
||
| 116 | * @param OrderRepositoryInterface $orderRepository |
||
| 117 | * @param FactoryInterface $orderFactory |
||
| 118 | * @param OrderProcessorInterface $orderProcessor |
||
| 119 | * @param FactoryInterface $orderItemFactory |
||
| 120 | * @param OrderItemQuantityModifierInterface $itemQuantityModifier |
||
| 121 | * @param RepositoryInterface $currencyRepository |
||
| 122 | * @param CurrencyStorageInterface $currencyStorage |
||
| 123 | * @param FactoryInterface $customerFactory |
||
| 124 | * @param RepositoryInterface $customerRepository |
||
| 125 | * @param ObjectManager $objectManager |
||
| 126 | * @param StateMachineFactoryInterface $stateMachineFactory |
||
| 127 | * @param ProductVariantResolverInterface $variantResolver |
||
| 128 | */ |
||
| 129 | public function __construct( |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @Given /^there is (?:a|another) (customer "[^"]+") that placed an order$/ |
||
| 161 | * @Given /^there is (?:a|another) (customer "[^"]+") that placed (an order "[^"]+")$/ |
||
| 162 | * @Given a customer :customer placed an order :orderNumber |
||
| 163 | * @Given the customer :customer has already placed an order :orderNumber |
||
| 164 | */ |
||
| 165 | public function thereIsCustomerThatPlacedOrder(CustomerInterface $customer, $orderNumber = null) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @Given a customer :customer added something to cart |
||
| 176 | */ |
||
| 177 | public function customerStartedCheckout(CustomerInterface $customer) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @Given /^(I) placed (an order "[^"]+")$/ |
||
| 188 | */ |
||
| 189 | public function iPlacedAnOrder(UserInterface $user, $orderNumber) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @Given /^the customer ("[^"]+" addressed it to "[^"]+", "[^"]+" "[^"]+" in the "[^"]+"(?:|, "[^"]+"))$/ |
||
| 201 | * @Given /^I (addressed it to "[^"]+", "[^"]+", "[^"]+" "[^"]+" in the "[^"]+"(?:|, "[^"]+"))$/ |
||
| 202 | */ |
||
| 203 | public function theCustomerAddressedItTo(AddressInterface $address) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @Given /^the customer set the billing (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)")$/ |
||
| 214 | * @Given /^for the billing address (of "[^"]+" in the "[^"]+", "[^"]+" "[^"]+", "[^"]+")$/ |
||
| 215 | * @Given /^for the billing address (of "[^"]+" in the "[^"]+", "[^"]+" "([^"]+)", "[^"]+", "[^"]+")$/ |
||
| 216 | */ |
||
| 217 | public function forTheBillingAddressOf(AddressInterface $address) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @Given /^the customer ("[^"]+" addressed it to "[^"]+", "[^"]+" "[^"]+" in the "[^"]+") with identical billing address$/ |
||
| 231 | * @Given /^I (addressed it to "[^"]+", "[^"]+", "[^"]+" "[^"]+" in the "[^"]+") with identical billing address$/ |
||
| 232 | */ |
||
| 233 | public function theCustomerAddressedItToWithIdenticalBillingAddress(AddressInterface $address) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @Given /^the customer chose ("[^"]+" shipping method) (to "[^"]+") with ("[^"]+" payment)$/ |
||
| 241 | * @Given /^I chose ("[^"]+" shipping method) (to "[^"]+") with ("[^"]+" payment)$/ |
||
| 242 | */ |
||
| 243 | public function theCustomerChoseShippingToWithPayment( |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @Given /^the customer chose ("[^"]+" shipping method) with ("[^"]+" payment)$/ |
||
| 258 | * @Given /^I chose ("[^"]+" shipping method) with ("[^"]+" payment)$/ |
||
| 259 | */ |
||
| 260 | public function theCustomerChoseShippingWithPayment( |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @Given the customer bought a single :product |
||
| 274 | * @Given I bought a single :product |
||
| 275 | */ |
||
| 276 | public function theCustomerBoughtSingleProduct(ProductInterface $product) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @Given /^the customer bought ((?:a|an) "[^"]+") and ((?:a|an) "[^"]+")$/ |
||
| 285 | * @Given /^I bought ((?:a|an) "[^"]+") and ((?:a|an) "[^"]+")$/ |
||
| 286 | */ |
||
| 287 | public function theCustomerBoughtProductAndProduct(ProductInterface $product, ProductInterface $secondProduct) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @Given /^the customer bought (\d+) ("[^"]+" products)$/ |
||
| 295 | */ |
||
| 296 | public function theCustomerBoughtSeveralProducts($quantity, ProductInterface $product) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @Given /^the customer bought ([^"]+) units of ("[^"]+" variant of product "[^"]+")$/ |
||
| 306 | */ |
||
| 307 | public function theCustomerBoughtSeveralVariantsOfProduct($quantity, ProductVariantInterface $variant) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @Given /^the customer bought a single ("[^"]+" variant of product "[^"]+")$/ |
||
| 316 | */ |
||
| 317 | public function theCustomerBoughtSingleProductVariant(ProductVariantInterface $productVariant) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @Given the customer bought a single :product using :coupon coupon |
||
| 326 | * @Given I bought a single :product using :coupon coupon |
||
| 327 | */ |
||
| 328 | public function theCustomerBoughtSingleUsing(ProductInterface $product, PromotionCouponInterface $coupon) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @Given I used :coupon coupon |
||
| 338 | */ |
||
| 339 | public function iUsedCoupon(PromotionCouponInterface $coupon) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @Given /^(I) have already placed (\d+) orders choosing ("[^"]+" shipping method) (to "[^"]+") with ("[^"]+" payment)$/ |
||
| 349 | */ |
||
| 350 | public function iHaveAlreadyPlacedOrderNthTimes( |
||
| 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 :numberOfCustomers customers have added products to the cart for total of :total |
||
| 393 | */ |
||
| 394 | public function customersHaveAddedProductsToTheCartForTotalOf($numberOfCustomers, $total) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @Given :numberOfCustomers customers have placed :numberOfOrders orders for total of :total |
||
| 415 | * @Given then :numberOfCustomers more customers have placed :numberOfOrders orders for total of :total |
||
| 416 | */ |
||
| 417 | public function customersHavePlacedOrdersForTotalOf($numberOfCustomers, $numberOfOrders, $total) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @Given :numberOfCustomers customers have placed :numberOfOrders orders for total of :total mostly :product product |
||
| 440 | * @Given then :numberOfCustomers more customers have placed :numberOfOrders orders for total of :total mostly :product product |
||
| 441 | */ |
||
| 442 | public function customersHavePlacedOrdersForTotalOfMostlyProduct($numberOfCustomers, $numberOfOrders, $total, ProductInterface $product) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @Given /^(this customer) has(?:| also) placed (an order "[^"]+") buying a single ("[^"]+" product) for ("[^"]+") in ("[^"]+" currency)$/ |
||
| 464 | */ |
||
| 465 | public function customerHasPlacedAnOrderBuyingASingleProductForInCurrency( |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @Given /^(this order) is already paid$/ |
||
| 482 | * @Given the order :order is already paid |
||
| 483 | */ |
||
| 484 | public function thisOrderIsAlreadyPaid(OrderInterface $order) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @Given /^the customer cancelled (this order)$/ |
||
| 493 | * @Given /^(this order) was cancelled$/ |
||
| 494 | * @Given the order :order was cancelled |
||
| 495 | * @Given /^I cancelled (this order)$/ |
||
| 496 | */ |
||
| 497 | public function theCustomerCancelledThisOrder(OrderInterface $order) |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @Given /^I cancelled my last order$/ |
||
| 506 | */ |
||
| 507 | public function theCustomerCancelledMyLastOrder() |
||
| 514 | |||
| 515 | /** |
||
| 516 | * @Given /^(this order) has already been shipped$/ |
||
| 517 | */ |
||
| 518 | public function thisOrderHasAlreadyBeenShipped(OrderInterface $order) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @param OrderInterface $order |
||
| 527 | * @param string $transition |
||
| 528 | */ |
||
| 529 | private function applyShipmentTransitionOnOrder(OrderInterface $order, $transition) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * @param OrderInterface $order |
||
| 538 | * @param string $transition |
||
| 539 | */ |
||
| 540 | private function applyPaymentTransitionOnOrder(OrderInterface $order, $transition) |
||
| 546 | |||
| 547 | /** |
||
| 548 | * @param OrderInterface $order |
||
| 549 | * @param string $transition |
||
| 550 | */ |
||
| 551 | private function applyTransitionOnOrderCheckout(OrderInterface $order, $transition) |
||
| 555 | |||
| 556 | /** |
||
| 557 | * @param ProductVariantInterface $productVariant |
||
| 558 | * @param int $quantity |
||
| 559 | * |
||
| 560 | * @return OrderInterface |
||
| 561 | */ |
||
| 562 | private function addProductVariantToOrder(ProductVariantInterface $productVariant, $quantity = 1) |
||
| 580 | |||
| 581 | /** |
||
| 582 | * @param CustomerInterface $customer |
||
| 583 | * @param string $number |
||
| 584 | * @param ChannelInterface|null $channel |
||
| 585 | * @param string|null $currencyCode |
||
| 586 | * @param string|null $localeCode |
||
| 587 | * |
||
| 588 | * @return OrderInterface |
||
| 589 | */ |
||
| 590 | private function createOrder( |
||
| 607 | |||
| 608 | /** |
||
| 609 | * @param CustomerInterface $customer |
||
| 610 | * @param ChannelInterface|null $channel |
||
| 611 | * @param string|null $currencyCode |
||
| 612 | * @param string|null $localeCode |
||
| 613 | * |
||
| 614 | * @return OrderInterface |
||
| 615 | */ |
||
| 616 | private function createCart( |
||
| 636 | |||
| 637 | /** |
||
| 638 | * @param int $count |
||
| 639 | * |
||
| 640 | * @return CustomerInterface[] |
||
| 641 | */ |
||
| 642 | private function generateCustomers($count) |
||
| 659 | |||
| 660 | /** |
||
| 661 | * @param string $price |
||
| 662 | * |
||
| 663 | * @return int |
||
| 664 | */ |
||
| 665 | private function getPriceFromString($price) |
||
| 669 | |||
| 670 | /** |
||
| 671 | * @param OrderInterface $order |
||
| 672 | * @param ShippingMethodInterface $shippingMethod |
||
| 673 | * @param AddressInterface $address |
||
| 674 | * @param PaymentMethodInterface $paymentMethod |
||
| 675 | */ |
||
| 676 | private function checkoutUsing( |
||
| 689 | |||
| 690 | /** |
||
| 691 | * @param OrderInterface $order |
||
| 692 | * @param ShippingMethodInterface $shippingMethod |
||
| 693 | * @param PaymentMethodInterface $paymentMethod |
||
| 694 | */ |
||
| 695 | private function proceedSelectingShippingAndPaymentMethod(OrderInterface $order, ShippingMethodInterface $shippingMethod, PaymentMethodInterface $paymentMethod) |
||
| 708 | |||
| 709 | /** |
||
| 710 | * @param OrderInterface $order |
||
| 711 | * @param ProductVariantInterface $variant |
||
| 712 | * @param int $price |
||
| 713 | */ |
||
| 714 | private function addVariantWithPriceToOrder(OrderInterface $order, ProductVariantInterface $variant, $price) |
||
| 724 | } |
||
| 725 |
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: