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 |
||
| 46 | final class OrderContext implements Context |
||
| 47 | { |
||
| 48 | /** |
||
| 49 | * @var SharedStorageInterface |
||
| 50 | */ |
||
| 51 | private $sharedStorage; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var OrderRepositoryInterface |
||
| 55 | */ |
||
| 56 | private $orderRepository; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var FactoryInterface |
||
| 60 | */ |
||
| 61 | private $orderFactory; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var OrderProcessorInterface |
||
| 65 | */ |
||
| 66 | private $orderProcessor; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var FactoryInterface |
||
| 70 | */ |
||
| 71 | private $orderItemFactory; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var OrderItemQuantityModifierInterface |
||
| 75 | */ |
||
| 76 | private $itemQuantityModifier; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var RepositoryInterface |
||
| 80 | */ |
||
| 81 | private $currencyRepository; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var CurrencyStorageInterface |
||
| 85 | */ |
||
| 86 | private $currencyStorage; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var FactoryInterface |
||
| 90 | */ |
||
| 91 | private $customerFactory; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var RepositoryInterface |
||
| 95 | */ |
||
| 96 | private $customerRepository; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var ObjectManager |
||
| 100 | */ |
||
| 101 | private $objectManager; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var StateMachineFactoryInterface |
||
| 105 | */ |
||
| 106 | private $stateMachineFactory; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var ProductVariantResolverInterface |
||
| 110 | */ |
||
| 111 | private $variantResolver; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param SharedStorageInterface $sharedStorage |
||
| 115 | * @param OrderRepositoryInterface $orderRepository |
||
| 116 | * @param FactoryInterface $orderFactory |
||
| 117 | * @param OrderProcessorInterface $orderProcessor |
||
| 118 | * @param FactoryInterface $orderItemFactory |
||
| 119 | * @param OrderItemQuantityModifierInterface $itemQuantityModifier |
||
| 120 | * @param RepositoryInterface $currencyRepository |
||
| 121 | * @param CurrencyStorageInterface $currencyStorage |
||
| 122 | * @param FactoryInterface $customerFactory |
||
| 123 | * @param RepositoryInterface $customerRepository |
||
| 124 | * @param ObjectManager $objectManager |
||
| 125 | * @param StateMachineFactoryInterface $stateMachineFactory |
||
| 126 | * @param ProductVariantResolverInterface $variantResolver |
||
| 127 | */ |
||
| 128 | public function __construct( |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @Given /^there is (?:a|another) (customer "[^"]+") that placed an order$/ |
||
| 160 | * @Given /^there is (?:a|another) (customer "[^"]+") that placed (an order "[^"]+")$/ |
||
| 161 | * @Given a customer :customer placed an order :orderNumber |
||
| 162 | * @Given the customer :customer has already placed an order :orderNumber |
||
| 163 | */ |
||
| 164 | public function thereIsCustomerThatPlacedOrder(CustomerInterface $customer, $orderNumber = null) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @Given a customer :customer added something to cart |
||
| 175 | */ |
||
| 176 | public function customerStartedCheckout(CustomerInterface $customer) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @Given /^(I) placed (an order "[^"]+")$/ |
||
| 187 | */ |
||
| 188 | public function iPlacedAnOrder(UserInterface $user, $orderNumber) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @Given /^the customer ("[^"]+" addressed it to "[^"]+", "[^"]+" "[^"]+" in the "[^"]+"(?:|, "[^"]+"))$/ |
||
| 200 | * @Given /^I (addressed it to "[^"]+", "[^"]+", "[^"]+" "[^"]+" in the "[^"]+"(?:|, "[^"]+"))$/ |
||
| 201 | */ |
||
| 202 | public function theCustomerAddressedItTo(AddressInterface $address) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @Given /^the customer set the billing (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)")$/ |
||
| 213 | * @Given /^for the billing address (of "[^"]+" in the "[^"]+", "[^"]+" "[^"]+", "[^"]+")$/ |
||
| 214 | * @Given /^for the billing address (of "[^"]+" in the "[^"]+", "[^"]+" "([^"]+)", "[^"]+", "[^"]+")$/ |
||
| 215 | */ |
||
| 216 | public function forTheBillingAddressOf(AddressInterface $address) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @Given /^the customer ("[^"]+" addressed it to "[^"]+", "[^"]+" "[^"]+" in the "[^"]+") with identical billing address$/ |
||
| 230 | * @Given /^I (addressed it to "[^"]+", "[^"]+", "[^"]+" "[^"]+" in the "[^"]+") with identical billing address$/ |
||
| 231 | */ |
||
| 232 | public function theCustomerAddressedItToWithIdenticalBillingAddress(AddressInterface $address) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @Given /^the customer chose ("[^"]+" shipping method) (to "[^"]+") with ("[^"]+" payment)$/ |
||
| 240 | * @Given /^I chose ("[^"]+" shipping method) (to "[^"]+") with ("[^"]+" payment)$/ |
||
| 241 | */ |
||
| 242 | public function theCustomerChoseShippingToWithPayment( |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @Given /^the customer chose ("[^"]+" shipping method) with ("[^"]+" payment)$/ |
||
| 257 | * @Given /^I chose ("[^"]+" shipping method) with ("[^"]+" payment)$/ |
||
| 258 | */ |
||
| 259 | public function theCustomerChoseShippingWithPayment( |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @Given the customer bought a single :product |
||
| 273 | * @Given I bought a single :product |
||
| 274 | */ |
||
| 275 | public function theCustomerBoughtSingleProduct(ProductInterface $product) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @Given /^the customer bought ((?:a|an) "[^"]+") and ((?:a|an) "[^"]+")$/ |
||
| 284 | * @Given /^I bought ((?:a|an) "[^"]+") and ((?:a|an) "[^"]+")$/ |
||
| 285 | */ |
||
| 286 | public function theCustomerBoughtProductAndProduct(ProductInterface $product, ProductInterface $secondProduct) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @Given /^the customer bought (\d+) ("[^"]+" products)$/ |
||
| 294 | */ |
||
| 295 | public function theCustomerBoughtSeveralProducts($quantity, ProductInterface $product) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @Given /^the customer bought ([^"]+) units of ("[^"]+" variant of product "[^"]+")$/ |
||
| 305 | */ |
||
| 306 | public function theCustomerBoughtSeveralVariantsOfProduct($quantity, ProductVariantInterface $variant) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @Given /^the customer bought a single ("[^"]+" variant of product "[^"]+")$/ |
||
| 315 | */ |
||
| 316 | public function theCustomerBoughtSingleProductVariant(ProductVariantInterface $productVariant) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @Given the customer bought a single :product using :coupon coupon |
||
| 325 | * @Given I bought a single :product using :coupon coupon |
||
| 326 | */ |
||
| 327 | public function theCustomerBoughtSingleUsing(ProductInterface $product, PromotionCouponInterface $coupon) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @Given I used :coupon coupon |
||
| 337 | */ |
||
| 338 | public function iUsedCoupon(PromotionCouponInterface $coupon) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @Given /^(I) have already placed (\d+) orders choosing ("[^"]+" shipping method) (to "[^"]+") with ("[^"]+" payment)$/ |
||
| 348 | */ |
||
| 349 | public function iHaveAlreadyPlacedOrderNthTimes( |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @Given /^(this customer) has(?:| also) placed (an order "[^"]+") at "([^"]+)"$/ |
||
| 369 | */ |
||
| 370 | public function thisCustomerHasPlacedAnOrderAtDate(CustomerInterface $customer, $number, $checkoutCompletedAt) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @Given /^(this customer) has(?:| also) placed (an order "[^"]+") on a (channel "[^"]+")$/ |
||
| 381 | */ |
||
| 382 | public function thisCustomerHasPlacedAnOrderOnAChannel(CustomerInterface $customer, $number, $channel) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @Given :numberOfCustomers customers have added products to the cart for total of :total |
||
| 392 | */ |
||
| 393 | public function customersHaveAddedProductsToTheCartForTotalOf($numberOfCustomers, $total) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @Given :numberOfCustomers customers have placed :numberOfOrders orders for total of :total |
||
| 420 | * @Given then :numberOfCustomers more customers have placed :numberOfOrders orders for total of :total |
||
| 421 | */ |
||
| 422 | public function customersHavePlacedOrdersForTotalOf($numberOfCustomers, $numberOfOrders, $total) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @Given :numberOfCustomers customers have placed :numberOfOrders orders for total of :total mostly :product product |
||
| 451 | * @Given then :numberOfCustomers more customers have placed :numberOfOrders orders for total of :total mostly :product product |
||
| 452 | */ |
||
| 453 | public function customersHavePlacedOrdersForTotalOfMostlyProduct($numberOfCustomers, $numberOfOrders, $total, ProductInterface $product) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * @Given /^(this order) is already paid$/ |
||
| 481 | * @Given the order :order is already paid |
||
| 482 | */ |
||
| 483 | public function thisOrderIsAlreadyPaid(OrderInterface $order) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * @Given /^the customer cancelled (this order)$/ |
||
| 492 | * @Given /^(this order) was cancelled$/ |
||
| 493 | * @Given the order :order was cancelled |
||
| 494 | * @Given /^I cancelled (this order)$/ |
||
| 495 | */ |
||
| 496 | public function theCustomerCancelledThisOrder(OrderInterface $order) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * @Given /^I cancelled my last order$/ |
||
| 505 | */ |
||
| 506 | public function theCustomerCancelledMyLastOrder() |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @Given /^(this order) has already been shipped$/ |
||
| 516 | */ |
||
| 517 | public function thisOrderHasAlreadyBeenShipped(OrderInterface $order) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * @param OrderInterface $order |
||
| 526 | * @param string $transition |
||
| 527 | */ |
||
| 528 | private function applyShipmentTransitionOnOrder(OrderInterface $order, $transition) |
||
| 534 | |||
| 535 | /** |
||
| 536 | * @param OrderInterface $order |
||
| 537 | * @param string $transition |
||
| 538 | */ |
||
| 539 | private function applyPaymentTransitionOnOrder(OrderInterface $order, $transition) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * @param OrderInterface $order |
||
| 548 | * @param string $transition |
||
| 549 | */ |
||
| 550 | private function applyTransitionOnOrderCheckout(OrderInterface $order, $transition) |
||
| 554 | |||
| 555 | /** |
||
| 556 | * @param ProductVariantInterface $productVariant |
||
| 557 | * @param int $quantity |
||
| 558 | * |
||
| 559 | * @return OrderInterface |
||
| 560 | */ |
||
| 561 | private function addProductVariantToOrder(ProductVariantInterface $productVariant, $quantity = 1) |
||
| 579 | |||
| 580 | /** |
||
| 581 | * @param CustomerInterface $customer |
||
| 582 | * @param string $number |
||
| 583 | * @param ChannelInterface|null $channel |
||
| 584 | * @param string|null $currencyCode |
||
| 585 | * @param string|null $localeCode |
||
| 586 | * |
||
| 587 | * @return OrderInterface |
||
| 588 | */ |
||
| 589 | private function createOrder( |
||
| 606 | |||
| 607 | /** |
||
| 608 | * @param CustomerInterface $customer |
||
| 609 | * @param ChannelInterface|null $channel |
||
| 610 | * @param string|null $currencyCode |
||
| 611 | * @param string|null $localeCode |
||
| 612 | * |
||
| 613 | * @return OrderInterface |
||
| 614 | */ |
||
| 615 | private function createCart( |
||
| 635 | |||
| 636 | /** |
||
| 637 | * @param int $count |
||
| 638 | * |
||
| 639 | * @return CustomerInterface[] |
||
| 640 | */ |
||
| 641 | private function generateCustomers($count) |
||
| 658 | |||
| 659 | /** |
||
| 660 | * @param string $price |
||
| 661 | * |
||
| 662 | * @return int |
||
| 663 | */ |
||
| 664 | private function getPriceFromString($price) |
||
| 668 | |||
| 669 | /** |
||
| 670 | * @param OrderInterface $order |
||
| 671 | * @param ShippingMethodInterface $shippingMethod |
||
| 672 | * @param AddressInterface $address |
||
| 673 | * @param PaymentMethodInterface $paymentMethod |
||
| 674 | */ |
||
| 675 | private function checkoutUsing( |
||
| 688 | |||
| 689 | /** |
||
| 690 | * @param OrderInterface $order |
||
| 691 | * @param ShippingMethodInterface $shippingMethod |
||
| 692 | * @param PaymentMethodInterface $paymentMethod |
||
| 693 | */ |
||
| 694 | private function proceedSelectingShippingAndPaymentMethod(OrderInterface $order, ShippingMethodInterface $shippingMethod, PaymentMethodInterface $paymentMethod) |
||
| 707 | } |
||
| 708 |
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: