Complex classes like CheckoutContext 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 CheckoutContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
42 | final class CheckoutContext implements Context |
||
43 | { |
||
44 | /** |
||
45 | * @var SharedStorageInterface |
||
46 | */ |
||
47 | private $sharedStorage; |
||
48 | |||
49 | /** |
||
50 | * @var SecurityStepInterface |
||
51 | */ |
||
52 | private $checkoutSecurityStep; |
||
53 | |||
54 | /** |
||
55 | * @var AddressingStepInterface |
||
56 | */ |
||
57 | private $checkoutAddressingStep; |
||
58 | |||
59 | /** |
||
60 | * @var AddressingPageInterface |
||
61 | */ |
||
62 | private $addressingPage; |
||
63 | |||
64 | /** |
||
65 | * @var ShippingStepInterface |
||
66 | */ |
||
67 | private $checkoutShippingStep; |
||
68 | |||
69 | /** |
||
70 | * @var PaymentStepInterface |
||
71 | */ |
||
72 | private $checkoutPaymentStep; |
||
73 | |||
74 | /** |
||
75 | * @var PaymentPageInterface |
||
76 | */ |
||
77 | private $paymentPage; |
||
78 | |||
79 | /** |
||
80 | * @var FinalizeStepInterface |
||
81 | */ |
||
82 | private $checkoutFinalizeStep; |
||
83 | |||
84 | /** |
||
85 | * @var ThankYouPageInterface |
||
86 | */ |
||
87 | private $checkoutThankYouPage; |
||
88 | |||
89 | /** |
||
90 | * @var OrderPaymentsPageInterface |
||
91 | */ |
||
92 | private $orderPaymentsPage; |
||
93 | |||
94 | /** |
||
95 | * @var ShippingPageInterface |
||
96 | */ |
||
97 | private $shippingPage; |
||
98 | |||
99 | /** |
||
100 | * @var SummaryPageInterface |
||
101 | */ |
||
102 | private $summaryPage; |
||
103 | |||
104 | /** |
||
105 | * @var OrderRepositoryInterface |
||
106 | */ |
||
107 | private $orderRepository; |
||
108 | |||
109 | /** |
||
110 | * @var SecurityServiceInterface |
||
111 | */ |
||
112 | private $securityService; |
||
113 | |||
114 | /** |
||
115 | * @param SharedStorageInterface $sharedStorage |
||
116 | * @param SecurityStepInterface $checkoutSecurityStep |
||
117 | * @param AddressingStepInterface $checkoutAddressingStep |
||
118 | * @param AddressingPageInterface $addressingPage |
||
119 | * @param ShippingStepInterface $checkoutShippingStep |
||
120 | * @param ShippingPageInterface $shippingPage |
||
121 | * @param PaymentStepInterface $checkoutPaymentStep |
||
122 | * @param PaymentPageInterface $paymentPage |
||
123 | * @param SummaryPageInterface $summaryPage |
||
124 | * @param FinalizeStepInterface $checkoutFinalizeStep |
||
125 | * @param ThankYouPageInterface $checkoutThankYouPage |
||
126 | * @param OrderPaymentsPageInterface $orderPaymentsPage |
||
127 | * @param OrderRepositoryInterface $orderRepository |
||
128 | * @param SecurityServiceInterface $securityService |
||
129 | */ |
||
130 | public function __construct( |
||
131 | SharedStorageInterface $sharedStorage, |
||
132 | SecurityStepInterface $checkoutSecurityStep, |
||
133 | AddressingStepInterface $checkoutAddressingStep, |
||
134 | AddressingPageInterface $addressingPage, |
||
135 | ShippingStepInterface $checkoutShippingStep, |
||
136 | ShippingPageInterface $shippingPage, |
||
137 | PaymentStepInterface $checkoutPaymentStep, |
||
138 | PaymentPageInterface $paymentPage, |
||
139 | SummaryPageInterface $summaryPage, |
||
140 | FinalizeStepInterface $checkoutFinalizeStep, |
||
141 | ThankYouPageInterface $checkoutThankYouPage, |
||
142 | OrderPaymentsPageInterface $orderPaymentsPage, |
||
143 | OrderRepositoryInterface $orderRepository, |
||
144 | SecurityServiceInterface $securityService |
||
145 | ) { |
||
146 | $this->sharedStorage = $sharedStorage; |
||
147 | $this->checkoutSecurityStep = $checkoutSecurityStep; |
||
148 | $this->checkoutAddressingStep = $checkoutAddressingStep; |
||
149 | $this->addressingPage = $addressingPage; |
||
150 | $this->checkoutShippingStep = $checkoutShippingStep; |
||
151 | $this->shippingPage = $shippingPage; |
||
152 | $this->checkoutPaymentStep = $checkoutPaymentStep; |
||
153 | $this->paymentPage = $paymentPage; |
||
154 | $this->summaryPage = $summaryPage; |
||
155 | $this->checkoutFinalizeStep = $checkoutFinalizeStep; |
||
156 | $this->checkoutThankYouPage = $checkoutThankYouPage; |
||
157 | $this->orderPaymentsPage = $orderPaymentsPage; |
||
158 | $this->orderRepository = $orderRepository; |
||
159 | $this->securityService = $securityService; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @Given /^I proceed without selecting shipping address$/ |
||
164 | */ |
||
165 | public function iProceedWithoutSelectingShippingAddress() |
||
170 | |||
171 | /** |
||
172 | * @Given I am at the checkout addressing step |
||
173 | */ |
||
174 | public function iAmAtTheCheckoutAddressingStep() |
||
178 | |||
179 | /** |
||
180 | * @Given /^(this user) bought this product$/ |
||
181 | */ |
||
182 | public function thisUserBought(UserInterface $user) |
||
189 | |||
190 | /** |
||
191 | * @When /^I specify the shipping (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ |
||
192 | * @When /^I (do not specify any shipping address) information$/ |
||
193 | */ |
||
194 | public function iSpecifyTheShippingAddressAs(AddressInterface $address) |
||
205 | |||
206 | /** |
||
207 | * @When /^I specify the billing (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ |
||
208 | * @When /^I (do not specify any billing address) information$/ |
||
209 | */ |
||
210 | public function iSpecifyTheBillingAddressAs(AddressInterface $address) |
||
222 | |||
223 | /** |
||
224 | * @When /^I specified the shipping (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ |
||
225 | */ |
||
226 | public function iSpecifiedTheShippingAddress(AddressInterface $address) |
||
227 | { |
||
228 | $this->addressingPage->open(); |
||
229 | $this->iSpecifyTheShippingAddressAs($address); |
||
230 | |||
231 | $key = sprintf('billing_address_%s_%s', strtolower($address->getFirstName()), strtolower($address->getLastName())); |
||
232 | $this->sharedStorage->set($key, $address); |
||
233 | |||
234 | $this->iCompleteTheAddressingStep(); |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * @When I choose the different billing address |
||
239 | */ |
||
240 | public function iChooseTheDifferentBillingAddress() |
||
244 | |||
245 | /** |
||
246 | * @When I specify the email as :email |
||
247 | * @When I do not specify the email |
||
248 | */ |
||
249 | public function iSpecifyTheEmail($email = null) |
||
253 | |||
254 | /** |
||
255 | * @When I select :shippingMethod shipping method |
||
256 | */ |
||
257 | public function iSelectShippingMethod($shippingMethod) |
||
258 | { |
||
259 | $this->shippingPage->selectShippingMethod($shippingMethod); |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * @Then I should not be able to select :shippingMethod shipping method |
||
264 | */ |
||
265 | public function iShouldNotBeAbleToSelectShippingMethod($shippingMethod) |
||
266 | { |
||
267 | Assert::false( |
||
268 | $this->shippingPage->hasShippingMethod($shippingMethod), |
||
269 | sprintf('Shipping method "%s" should not be available but it does.', $shippingMethod) |
||
270 | ); |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * @When I complete the addressing step |
||
275 | * @When I try to complete the addressing step |
||
276 | */ |
||
277 | public function iCompleteTheAddressingStep() |
||
278 | { |
||
279 | $this->addressingPage->nextStep(); |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * @When I complete the shipping step |
||
284 | */ |
||
285 | public function iCompleteTheShippingStep() |
||
286 | { |
||
287 | $this->shippingPage->nextStep(); |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * @When /^I proceed selecting "([^"]*)" as shipping country$/ |
||
292 | */ |
||
293 | public function iProceedSelectingShippingCountry($shippingCountry) |
||
294 | { |
||
295 | $this->checkoutAddressingStep->open(); |
||
296 | $this->checkoutAddressingStep->fillAddressingDetails([ |
||
297 | 'firstName' => 'John', |
||
298 | 'lastName' => 'Doe', |
||
299 | 'country' => $shippingCountry ?: 'France', |
||
300 | 'street' => '0635 Myron Hollow Apt. 711', |
||
301 | 'city' => 'North Bridget', |
||
302 | 'postcode' => '93-554', |
||
303 | 'phoneNumber' => '321123456', |
||
304 | ]); |
||
305 | $this->checkoutAddressingStep->continueCheckout(); |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * @When /^I proceed selecting "([^"]*)" as shipping country with "([^"]*)" method$/ |
||
310 | */ |
||
311 | public function iProceedSelectingShippingCountryAndShippingMethod($shippingCountry, $shippingMethodName) |
||
312 | { |
||
313 | $this->iProceedSelectingShippingCountry($shippingCountry); |
||
314 | |||
315 | $this->checkoutShippingStep->selectShippingMethod($shippingMethodName ?: 'Free'); |
||
316 | $this->checkoutShippingStep->continueCheckout(); |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * @When /^I proceed selecting "([^"]+)" shipping method$/ |
||
321 | * @Given /^I chose "([^"]*)" shipping method$/ |
||
322 | */ |
||
323 | public function iProceedSelectingShippingMethod($shippingMethodName) |
||
324 | { |
||
325 | $this->iProceedSelectingShippingCountryAndShippingMethod(null, $shippingMethodName); |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * @When /^I choose "([^"]*)" payment method$/ |
||
330 | */ |
||
331 | public function iChoosePaymentMethod($paymentMethodName) |
||
337 | |||
338 | /** |
||
339 | * @When /^I proceed selecting "([^"]*)" as shipping country with "([^"]*)" payment method$/ |
||
340 | */ |
||
341 | public function iProceedSelectingShippingCountryAndPaymentMethod($shippingCountry, $paymentMethodName) |
||
347 | |||
348 | /** |
||
349 | * @When I proceed selecting :paymentMethodName payment method |
||
350 | */ |
||
351 | public function iProceedSelectingOfflinePaymentMethod($paymentMethodName = 'Offline') |
||
355 | |||
356 | /** |
||
357 | * @When /^I change shipping method to "([^"]*)"$/ |
||
358 | */ |
||
359 | public function iChangeShippingMethod($shippingMethodName) |
||
365 | |||
366 | /** |
||
367 | * @Given /^I proceed logging as "([^"]*)" with "([^"]*)" password$/ |
||
368 | */ |
||
369 | public function iProceedLoggingAs($login, $password) |
||
376 | |||
377 | /** |
||
378 | * @When /^I proceed as guest "([^"]*)" with "([^"]*)" as shipping country$/ |
||
379 | */ |
||
380 | public function iProceedLoggingAsGuestWithAsShippingCountry($email, $shippingCountry) |
||
387 | |||
388 | /** |
||
389 | * @When I confirm my order |
||
390 | */ |
||
391 | public function iConfirmMyOrder() |
||
395 | |||
396 | /** |
||
397 | * @When I specify the password as :password |
||
398 | */ |
||
399 | public function iSpecifyThePasswordAs($password) |
||
403 | |||
404 | /** |
||
405 | * @When I sign in |
||
406 | */ |
||
407 | public function iSignIn() |
||
411 | |||
412 | /** |
||
413 | * @Then I should see the thank you page |
||
414 | */ |
||
415 | public function iShouldSeeTheThankYouPage() |
||
423 | |||
424 | /** |
||
425 | * @Then I should be redirected back to the thank you page |
||
426 | */ |
||
427 | public function iShouldBeRedirectedBackToTheThankYouPage() |
||
433 | |||
434 | /** |
||
435 | * @Then I should be redirected back to the order payment page |
||
436 | */ |
||
437 | public function iShouldBeRedirectedBackToTheOrderPaymentPage() |
||
443 | |||
444 | /** |
||
445 | * @Then I should be on the checkout shipping step |
||
446 | */ |
||
447 | public function iShouldBeOnTheCheckoutShippingStep() |
||
448 | { |
||
449 | Assert::true( |
||
450 | $this->shippingPage->isOpen(), |
||
451 | 'Checkout shipping page should be opened, but it is not.' |
||
452 | ); |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * @Then I should be on the checkout summary step |
||
457 | */ |
||
458 | public function iShouldBeOnTheCheckoutSummaryStep() |
||
465 | |||
466 | /** |
||
467 | * @Then I should see two cancelled payments and new one ready to be paid |
||
468 | */ |
||
469 | public function iShouldSeeTwoCancelledPaymentsAndNewOneReadyToBePaid() |
||
474 | |||
475 | /** |
||
476 | * @Then /^I should(?:| also) be notified that the "([^"]+)" and the "([^"]+)" in (shipping|billing) details are required$/ |
||
477 | */ |
||
478 | public function iShouldBeNotifiedThatTheAndTheInShippingDetailsAreRequired($firstElement, $secondElement, $type) |
||
483 | |||
484 | /** |
||
485 | * @Then I should be informed that my order cannot be shipped to this address |
||
486 | */ |
||
487 | public function iShouldBeInformedThatMyOrderCannotBeShippedToThisAddress() |
||
488 | { |
||
489 | Assert::true( |
||
490 | $this->shippingPage->hasNoShippingMethodsMessage(), |
||
491 | 'Shipping page should have no shipping methods message but it does not.' |
||
492 | ); |
||
493 | } |
||
494 | |||
495 | /** |
||
496 | * @Then I should be able to log in |
||
497 | */ |
||
498 | public function iShouldBeAbleToLogIn() |
||
505 | |||
506 | /** |
||
507 | * @Then the login form should no longer be accessible |
||
508 | */ |
||
509 | public function theLoginFormShouldNoLongerBeAccessible() |
||
516 | |||
517 | /** |
||
518 | * @Then I should be notified about bad credentials |
||
519 | */ |
||
520 | public function iShouldBeNotifiedAboutBadCredentials() |
||
527 | |||
528 | /** |
||
529 | * @Then my order's shipping address should be to :fullName |
||
530 | */ |
||
531 | public function iShouldSeeThisShippingAddressAsShippingAddress($fullName) |
||
539 | |||
540 | /** |
||
541 | * @Then my order's billing address should be to :fullName |
||
542 | */ |
||
543 | public function iShouldSeeThisBillingAddressAsBillingAddress($fullName) |
||
551 | |||
552 | /** |
||
553 | * @Then address to :fullName should be used for both shipping and billing of my order` |
||
554 | */ |
||
555 | public function iShouldSeeThisShippingAddressAsShippingAndBillingAddress($fullName) |
||
560 | |||
561 | /** |
||
562 | * @Given I am at the checkout payment step |
||
563 | */ |
||
564 | public function iAmAtTheCheckoutPaymentStep() |
||
568 | |||
569 | /** |
||
570 | * @When I complete the payment step |
||
571 | */ |
||
572 | public function iCompleteThePaymentStep() |
||
576 | |||
577 | /** |
||
578 | * @When I select :paymentMethodName payment method |
||
579 | */ |
||
580 | public function iSelectPaymentMethod($paymentMethodName) |
||
584 | |||
585 | /** |
||
586 | * @Then I should not be able to select :paymentMethodName payment method |
||
587 | */ |
||
588 | public function iShouldNotBeAbleToSelectPaymentMethod($paymentMethodName) |
||
595 | |||
596 | /** |
||
597 | * @When I proceed order with :shippingMethod shipping method and :paymentMethod payment |
||
598 | */ |
||
599 | public function iProceedOrderWithShippingMethodAndPayment($shippingMethod, $paymentMethod) |
||
606 | |||
607 | /** |
||
608 | * @Given I should have :quantity :productName products in the cart |
||
609 | */ |
||
610 | public function iShouldHaveProductsInTheCart($quantity, $productName) |
||
617 | |||
618 | /** |
||
619 | * @Given I should see the shipping total with price :price |
||
620 | */ |
||
621 | public function iShouldSeeTheShippingTotalWithPrice($price) |
||
622 | { |
||
623 | Assert::true( |
||
624 | $this->summaryPage->hasShippingTotal($price), |
||
625 | sprintf('The shipping total should be %s, but it is not.',$price) |
||
626 | ); |
||
627 | } |
||
628 | |||
629 | /** |
||
630 | * @Then /^the ("[^"]+" product) should have unit price discounted by ("\$\d+")$/ |
||
631 | */ |
||
632 | public function theShouldHaveUnitPriceDiscountedFor(ProductInterface $product, $amount) |
||
639 | |||
640 | /** |
||
641 | * @Then /^my order total should be ("\$\d+")$/ |
||
642 | */ |
||
643 | public function myOrderTotalShouldBe($total) |
||
650 | |||
651 | /** |
||
652 | * @Given I should see promotion total :promotionTotal |
||
653 | */ |
||
654 | public function iShouldSeeDiscountTotal($promotionTotal) |
||
661 | |||
662 | /** |
||
663 | * @Given I should see promotion :promotionName |
||
664 | */ |
||
665 | public function iShouldSeePromotion($promotionName) |
||
672 | |||
673 | /** |
||
674 | * @Given my tax total should be :taxTotal |
||
675 | */ |
||
676 | public function myTaxTotalShouldBe($taxTotal) |
||
683 | |||
684 | /** |
||
685 | * @Then I should see :shippingMethod shipping method |
||
686 | */ |
||
687 | public function iShouldSeeThisShippingMethod(ShippingMethodInterface $shippingMethod) |
||
694 | |||
695 | /** |
||
696 | * @Then I should see :paymentMethod payment method |
||
697 | */ |
||
698 | public function iShouldSeeThisPaymentMethod(PaymentMethodInterface $paymentMethod) |
||
705 | |||
706 | /** |
||
707 | * @param string $type |
||
708 | * @param string $element |
||
709 | * @param string $expectedMessage |
||
710 | * |
||
711 | * @throws \InvalidArgumentException |
||
712 | */ |
||
713 | private function assertElementValidationMessage($type, $element, $expectedMessage) |
||
721 | |||
722 | /** |
||
723 | * @return OrderInterface |
||
724 | * |
||
725 | * @throws \RuntimeException |
||
726 | */ |
||
727 | private function getLastOrder() |
||
739 | } |
||
740 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.