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( |
||
379 | |||
380 | /** |
||
381 | * @Given :numberOfCustomers customers have added products to the cart for total of :total |
||
382 | */ |
||
383 | public function customersHaveAddedProductsToTheCartForTotalOf($numberOfCustomers, $total) |
||
407 | |||
408 | /** |
||
409 | * @Given :numberOfCustomers customers have placed :numberOfOrders orders for total of :total |
||
410 | * @Given then :numberOfCustomers more customers have placed :numberOfOrders orders for total of :total |
||
411 | */ |
||
412 | public function customersHavePlacedOrdersForTotalOf($numberOfCustomers, $numberOfOrders, $total) |
||
438 | |||
439 | /** |
||
440 | * @Given :numberOfCustomers customers have placed :numberOfOrders orders for total of :total mostly :product product |
||
441 | * @Given then :numberOfCustomers more customers have placed :numberOfOrders orders for total of :total mostly :product product |
||
442 | */ |
||
443 | public function customersHavePlacedOrdersForTotalOfMostlyProduct($numberOfCustomers, $numberOfOrders, $total, ProductInterface $product) |
||
468 | |||
469 | /** |
||
470 | * @Given /^(this order) is already paid$/ |
||
471 | * @Given the order :order is already paid |
||
472 | */ |
||
473 | public function thisOrderIsAlreadyPaid(OrderInterface $order) |
||
479 | |||
480 | /** |
||
481 | * @Given /^the customer cancelled (this order)$/ |
||
482 | * @Given /^(this order) was cancelled$/ |
||
483 | * @Given the order :order was cancelled |
||
484 | * @Given /^I cancelled (this order)$/ |
||
485 | */ |
||
486 | public function theCustomerCancelledThisOrder(OrderInterface $order) |
||
492 | |||
493 | /** |
||
494 | * @Given /^I cancelled my last order$/ |
||
495 | */ |
||
496 | public function theCustomerCancelledMyLastOrder() |
||
503 | |||
504 | /** |
||
505 | * @Given /^(this order) has already been shipped$/ |
||
506 | */ |
||
507 | public function thisOrderHasAlreadyBeenShipped(OrderInterface $order) |
||
513 | |||
514 | /** |
||
515 | * @param OrderInterface $order |
||
516 | * @param string $transition |
||
517 | */ |
||
518 | private function applyShipmentTransitionOnOrder(OrderInterface $order, $transition) |
||
524 | |||
525 | /** |
||
526 | * @param OrderInterface $order |
||
527 | * @param string $transition |
||
528 | */ |
||
529 | private function applyPaymentTransitionOnOrder(OrderInterface $order, $transition) |
||
535 | |||
536 | /** |
||
537 | * @param OrderInterface $order |
||
538 | * @param string $transition |
||
539 | */ |
||
540 | private function applyTransitionOnOrderCheckout(OrderInterface $order, $transition) |
||
544 | |||
545 | /** |
||
546 | * @param ProductVariantInterface $productVariant |
||
547 | * @param int $quantity |
||
548 | * |
||
549 | * @return OrderInterface |
||
550 | */ |
||
551 | private function addProductVariantToOrder(ProductVariantInterface $productVariant, $quantity = 1) |
||
566 | |||
567 | /** |
||
568 | * @param CustomerInterface $customer |
||
569 | * @param string $number |
||
570 | * @param ChannelInterface|null $channel |
||
571 | * @param string|null $currencyCode |
||
572 | * @param string|null $localeCode |
||
573 | * |
||
574 | * @return OrderInterface |
||
575 | */ |
||
576 | private function createOrder( |
||
593 | |||
594 | /** |
||
595 | * @param CustomerInterface $customer |
||
596 | * @param ChannelInterface|null $channel |
||
597 | * @param string|null $currencyCode |
||
598 | * @param string|null $localeCode |
||
599 | * |
||
600 | * @return OrderInterface |
||
601 | */ |
||
602 | private function createCart( |
||
623 | |||
624 | /** |
||
625 | * @param int $count |
||
626 | * |
||
627 | * @return CustomerInterface[] |
||
628 | */ |
||
629 | private function generateCustomers($count) |
||
646 | |||
647 | /** |
||
648 | * @param string $price |
||
649 | * |
||
650 | * @return int |
||
651 | */ |
||
652 | private function getPriceFromString($price) |
||
656 | |||
657 | /** |
||
658 | * @param OrderInterface $order |
||
659 | * @param ShippingMethodInterface $shippingMethod |
||
660 | * @param AddressInterface $address |
||
661 | * @param PaymentMethodInterface $paymentMethod |
||
662 | */ |
||
663 | private function checkoutUsing( |
||
676 | |||
677 | /** |
||
678 | * @param OrderInterface $order |
||
679 | * @param ShippingMethodInterface $shippingMethod |
||
680 | * @param PaymentMethodInterface $paymentMethod |
||
681 | */ |
||
682 | private function proceedSelectingShippingAndPaymentMethod(OrderInterface $order, ShippingMethodInterface $shippingMethod, PaymentMethodInterface $paymentMethod) |
||
695 | } |
||
696 |
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: