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 OrderProcessorInterface |
||
| 64 | */ |
||
| 65 | private $orderProcessor; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var FactoryInterface |
||
| 69 | */ |
||
| 70 | private $orderItemFactory; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var OrderItemQuantityModifierInterface |
||
| 74 | */ |
||
| 75 | private $itemQuantityModifier; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var RepositoryInterface |
||
| 79 | */ |
||
| 80 | private $currencyRepository; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var CurrencyStorageInterface |
||
| 84 | */ |
||
| 85 | private $currencyStorage; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var FactoryInterface |
||
| 89 | */ |
||
| 90 | private $customerFactory; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var RepositoryInterface |
||
| 94 | */ |
||
| 95 | private $customerRepository; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var ObjectManager |
||
| 99 | */ |
||
| 100 | private $objectManager; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var StateMachineFactoryInterface |
||
| 104 | */ |
||
| 105 | private $stateMachineFactory; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var ProductVariantResolverInterface |
||
| 109 | */ |
||
| 110 | private $variantResolver; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param SharedStorageInterface $sharedStorage |
||
| 114 | * @param OrderRepositoryInterface $orderRepository |
||
| 115 | * @param FactoryInterface $orderFactory |
||
| 116 | * @param OrderProcessorInterface $orderProcessor |
||
| 117 | * @param FactoryInterface $orderItemFactory |
||
| 118 | * @param OrderItemQuantityModifierInterface $itemQuantityModifier |
||
| 119 | * @param RepositoryInterface $currencyRepository |
||
| 120 | * @param CurrencyStorageInterface $currencyStorage |
||
| 121 | * @param FactoryInterface $customerFactory |
||
| 122 | * @param RepositoryInterface $customerRepository |
||
| 123 | * @param ObjectManager $objectManager |
||
| 124 | * @param StateMachineFactoryInterface $stateMachineFactory |
||
| 125 | * @param ProductVariantResolverInterface $variantResolver |
||
| 126 | */ |
||
| 127 | public function __construct( |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @Given /^there is (?:a|another) (customer "[^"]+") that placed an order$/ |
||
| 159 | * @Given /^there is (?:a|another) (customer "[^"]+") that placed (an order "[^"]+")$/ |
||
| 160 | * @Given a customer :customer placed an order :orderNumber |
||
| 161 | * @Given the customer :customer has already placed an order :orderNumber |
||
| 162 | */ |
||
| 163 | public function thereIsCustomerThatPlacedOrder(CustomerInterface $customer, $orderNumber = null) |
||
| 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 /^for the billing address (of "[^"]+" in the "[^"]+", "[^"]+" "[^"]+", "[^"]+")$/ |
||
| 212 | * @Given /^for the billing address (of "[^"]+" in the "[^"]+", "[^"]+" "([^"]+)", "[^"]+", "[^"]+")$/ |
||
| 213 | */ |
||
| 214 | public function forTheBillingAddressOf(AddressInterface $address) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @Given /^the customer ("[^"]+" addressed it to "[^"]+", "[^"]+" "[^"]+" in the "[^"]+") with identical billing address$/ |
||
| 228 | * @Given /^I (addressed it to "[^"]+", "[^"]+", "[^"]+" "[^"]+" in the "[^"]+") with identical billing address$/ |
||
| 229 | */ |
||
| 230 | public function theCustomerAddressedItToWithIdenticalBillingAddress(AddressInterface $address) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @Given /^the customer chose ("[^"]+" shipping method) (to "[^"]+") with ("[^"]+" payment)$/ |
||
| 238 | * @Given /^I chose ("[^"]+" shipping method) (to "[^"]+") with ("[^"]+" payment)$/ |
||
| 239 | */ |
||
| 240 | public function theCustomerChoseShippingToWithPayment( |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @Given the customer has chosen to order in the :currencyCode currency |
||
| 255 | * @Given I have chosen to order in the :currencyCode currency |
||
| 256 | */ |
||
| 257 | public function theCustomerChoseTheCurrency($currencyCode) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @Given /^the customer chose ("[^"]+" shipping method) with ("[^"]+" payment)$/ |
||
| 270 | * @Given /^I chose ("[^"]+" shipping method) with ("[^"]+" payment)$/ |
||
| 271 | */ |
||
| 272 | public function theCustomerChoseShippingWithPayment( |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @Given the customer bought a single :product |
||
| 286 | * @Given I bought a single :product |
||
| 287 | */ |
||
| 288 | public function theCustomerBoughtSingleProduct(ProductInterface $product) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @Given /^the customer bought ((?:a|an) "[^"]+") and ((?:a|an) "[^"]+")$/ |
||
| 297 | * @Given /^I bought ((?:a|an) "[^"]+") and ((?:a|an) "[^"]+")$/ |
||
| 298 | */ |
||
| 299 | public function theCustomerBoughtProductAndProduct(ProductInterface $product, ProductInterface $secondProduct) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @Given /^the customer bought (\d+) ("[^"]+" products)$/ |
||
| 307 | */ |
||
| 308 | public function theCustomerBoughtSeveralProducts($quantity, ProductInterface $product) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @Given /^the customer bought ([^"]+) units of ("[^"]+" variant of product "[^"]+")$/ |
||
| 318 | */ |
||
| 319 | public function theCustomerBoughtSeveralVariantsOfProduct($quantity, ProductVariantInterface $variant) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @Given /^the customer bought a single ("[^"]+" variant of product "[^"]+")$/ |
||
| 328 | */ |
||
| 329 | public function theCustomerBoughtSingleProductVariant(ProductVariantInterface $productVariant) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @Given the customer bought a single :product using :coupon coupon |
||
| 338 | * @Given I bought a single :product using :coupon coupon |
||
| 339 | */ |
||
| 340 | public function theCustomerBoughtSingleUsing(ProductInterface $product, PromotionCouponInterface $coupon) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @Given I used :coupon coupon |
||
| 350 | */ |
||
| 351 | public function iUsedCoupon(PromotionCouponInterface $coupon) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @Given /^(I) have already placed (\d+) orders choosing ("[^"]+" shipping method) (to "[^"]+") with ("[^"]+" payment)$/ |
||
| 361 | */ |
||
| 362 | public function iHaveAlreadyPlacedOrderNthTimes( |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @Given :numberOfCustomers customers have added products to the cart for total of :total |
||
| 381 | */ |
||
| 382 | public function customersHaveAddedProductsToTheCartForTotalOf($numberOfCustomers, $total) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @Given :numberOfCustomers customers have placed :numberOfOrders orders for total of :total |
||
| 409 | * @Given then :numberOfCustomers more customers have placed :numberOfOrders orders for total of :total |
||
| 410 | */ |
||
| 411 | public function customersHavePlacedOrdersForTotalOf($numberOfCustomers, $numberOfOrders, $total) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @Given :numberOfCustomers customers have placed :numberOfOrders orders for total of :total mostly :product product |
||
| 439 | * @Given then :numberOfCustomers more customers have placed :numberOfOrders orders for total of :total mostly :product product |
||
| 440 | */ |
||
| 441 | public function customersHavePlacedOrdersForTotalOfMostlyProduct($numberOfCustomers, $numberOfOrders, $total, ProductInterface $product) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @Given /^(this order) is already paid$/ |
||
| 469 | * @Given the order :order is already paid |
||
| 470 | */ |
||
| 471 | public function thisOrderIsAlreadyPaid(OrderInterface $order) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @Given /^the customer cancelled (this order)$/ |
||
| 480 | * @Given /^(this order) was cancelled$/ |
||
| 481 | * @Given the order :order was cancelled |
||
| 482 | * @Given /^I cancelled (this order)$/ |
||
| 483 | */ |
||
| 484 | public function theCustomerCancelledThisOrder(OrderInterface $order) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @Given /^(this order) has already been shipped$/ |
||
| 493 | */ |
||
| 494 | public function thisOrderHasAlreadyBeenShipped(OrderInterface $order) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @param OrderInterface $order |
||
| 503 | * @param string $transition |
||
| 504 | */ |
||
| 505 | private function applyShipmentTransitionOnOrder(OrderInterface $order, $transition) |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @param OrderInterface $order |
||
| 514 | * @param string $transition |
||
| 515 | */ |
||
| 516 | private function applyPaymentTransitionOnOrder(OrderInterface $order, $transition) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @param OrderInterface $order |
||
| 525 | * @param string $transition |
||
| 526 | */ |
||
| 527 | private function applyTransitionOnOrderCheckout(OrderInterface $order, $transition) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @param ProductVariantInterface $productVariant |
||
| 534 | * @param int $quantity |
||
| 535 | * |
||
| 536 | * @return OrderInterface |
||
| 537 | */ |
||
| 538 | private function addProductVariantToOrder(ProductVariantInterface $productVariant, $quantity = 1) |
||
| 553 | |||
| 554 | /** |
||
| 555 | * @param CustomerInterface $customer |
||
| 556 | * @param string $number |
||
| 557 | * @param ChannelInterface|null $channel |
||
| 558 | * @param string|null $currencyCode |
||
| 559 | * @param string|null $localeCode |
||
| 560 | * |
||
| 561 | * @return OrderInterface |
||
| 562 | */ |
||
| 563 | private function createOrder( |
||
| 580 | |||
| 581 | /** |
||
| 582 | * @param CustomerInterface $customer |
||
| 583 | * @param ChannelInterface|null $channel |
||
| 584 | * @param string|null $currencyCode |
||
| 585 | * @param string|null $localeCode |
||
| 586 | * |
||
| 587 | * @return OrderInterface |
||
| 588 | */ |
||
| 589 | private function createCart( |
||
| 610 | |||
| 611 | /** |
||
| 612 | * @param int $count |
||
| 613 | * |
||
| 614 | * @return CustomerInterface[] |
||
| 615 | */ |
||
| 616 | private function generateCustomers($count) |
||
| 633 | |||
| 634 | /** |
||
| 635 | * @param string $price |
||
| 636 | * |
||
| 637 | * @return int |
||
| 638 | */ |
||
| 639 | private function getPriceFromString($price) |
||
| 643 | |||
| 644 | /** |
||
| 645 | * @param OrderInterface $order |
||
| 646 | * @param ShippingMethodInterface $shippingMethod |
||
| 647 | * @param AddressInterface $address |
||
| 648 | * @param PaymentMethodInterface $paymentMethod |
||
| 649 | */ |
||
| 650 | private function checkoutUsing( |
||
| 663 | |||
| 664 | /** |
||
| 665 | * @param OrderInterface $order |
||
| 666 | * @param ShippingMethodInterface $shippingMethod |
||
| 667 | * @param PaymentMethodInterface $paymentMethod |
||
| 668 | */ |
||
| 669 | private function proceedSelectingShippingAndPaymentMethod(OrderInterface $order, ShippingMethodInterface $shippingMethod, PaymentMethodInterface $paymentMethod) |
||
| 682 | } |
||
| 683 |
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: