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 |
||
38 | final class CheckoutContext implements Context |
||
39 | { |
||
40 | /** |
||
41 | * @var SharedStorageInterface |
||
42 | */ |
||
43 | private $sharedStorage; |
||
44 | |||
45 | /** |
||
46 | * @var SecurityStepInterface |
||
47 | */ |
||
48 | private $checkoutSecurityStep; |
||
49 | |||
50 | /** |
||
51 | * @var AddressingStepInterface |
||
52 | */ |
||
53 | private $checkoutAddressingStep; |
||
54 | |||
55 | /** |
||
56 | * @var AddressingPageInterface |
||
57 | */ |
||
58 | private $addressingPage; |
||
59 | |||
60 | /** |
||
61 | * @var ShippingStepInterface |
||
62 | */ |
||
63 | private $checkoutShippingStep; |
||
64 | |||
65 | /** |
||
66 | * @var PaymentStepInterface |
||
67 | */ |
||
68 | private $checkoutPaymentStep; |
||
69 | |||
70 | /** |
||
71 | * @var PaymentPageInterface |
||
72 | */ |
||
73 | private $paymentPage; |
||
74 | |||
75 | /** |
||
76 | * @var FinalizeStepInterface |
||
77 | */ |
||
78 | private $checkoutFinalizeStep; |
||
79 | |||
80 | /** |
||
81 | * @var ThankYouPageInterface |
||
82 | */ |
||
83 | private $checkoutThankYouPage; |
||
84 | |||
85 | /** |
||
86 | * @var OrderPaymentsPageInterface |
||
87 | */ |
||
88 | private $orderPaymentsPage; |
||
89 | |||
90 | /** |
||
91 | * @var ShippingPageInterface |
||
92 | */ |
||
93 | private $shippingPage; |
||
94 | |||
95 | /** |
||
96 | * @var SummaryPageInterface |
||
97 | */ |
||
98 | private $summaryPage; |
||
99 | |||
100 | /** |
||
101 | * @var OrderRepositoryInterface |
||
102 | */ |
||
103 | private $orderRepository; |
||
104 | |||
105 | /** |
||
106 | * @param SharedStorageInterface $sharedStorage |
||
107 | * @param SecurityStepInterface $checkoutSecurityStep |
||
108 | * @param AddressingStepInterface $checkoutAddressingStep |
||
109 | * @param AddressingPageInterface $addressingPage |
||
110 | * @param ShippingStepInterface $checkoutShippingStep |
||
111 | * @param ShippingPageInterface $shippingPage |
||
112 | * @param PaymentStepInterface $checkoutPaymentStep |
||
113 | * @param PaymentPageInterface $paymentPage |
||
114 | * @param SummaryPageInterface $summaryPage |
||
115 | * @param FinalizeStepInterface $checkoutFinalizeStep |
||
116 | * @param ThankYouPageInterface $checkoutThankYouPage |
||
117 | * @param OrderPaymentsPageInterface $orderPaymentsPage |
||
118 | * @param OrderRepositoryInterface $orderRepository |
||
119 | */ |
||
120 | public function __construct( |
||
149 | |||
150 | /** |
||
151 | * @Given /^I proceed without selecting shipping address$/ |
||
152 | */ |
||
153 | public function iProceedWithoutSelectingShippingAddress() |
||
158 | |||
159 | /** |
||
160 | * @Given I am at the checkout addressing step |
||
161 | */ |
||
162 | public function iAmAtTheCheckoutAddressingStep() |
||
166 | |||
167 | /** |
||
168 | * @When /^I specify the shipping (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ |
||
169 | * @When /^I (do not specify any shipping address) information$/ |
||
170 | */ |
||
171 | public function iSpecifyTheShippingAddressAs(AddressInterface $address) |
||
182 | |||
183 | /** |
||
184 | * @When /^I specify the billing (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ |
||
185 | * @When /^I (do not specify any billing address) information$/ |
||
186 | */ |
||
187 | public function iSpecifyTheBillingAddressAs(AddressInterface $address) |
||
199 | |||
200 | /** |
||
201 | * @When /^I specified the shipping (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ |
||
202 | */ |
||
203 | public function iSpecifiedTheShippingAddress(AddressInterface $address) |
||
213 | |||
214 | /** |
||
215 | * @When I choose the different billing address |
||
216 | */ |
||
217 | public function iChooseTheDifferentBillingAddress() |
||
221 | |||
222 | /** |
||
223 | * @When I specify the email as :email |
||
224 | * @When I do not specify the email |
||
225 | */ |
||
226 | public function iSpecifyTheEmail($email = null) |
||
230 | |||
231 | /** |
||
232 | * @When I select :shippingMethod shipping method |
||
233 | */ |
||
234 | public function iSelectShippingMethod($shippingMethod) |
||
238 | |||
239 | /** |
||
240 | * @Then I should not be able to select :shippingMethod shipping method |
||
241 | */ |
||
242 | public function iShouldNotBeAbleToSelectShippingMethod($shippingMethod) |
||
249 | |||
250 | /** |
||
251 | * @When I complete the addressing step |
||
252 | * @When I try to complete the addressing step |
||
253 | */ |
||
254 | public function iCompleteTheAddressingStep() |
||
258 | |||
259 | /** |
||
260 | * @When I complete the shipping step |
||
261 | */ |
||
262 | public function iCompleteTheShippingStep() |
||
266 | |||
267 | /** |
||
268 | * @When /^I proceed selecting "([^"]*)" as shipping country$/ |
||
269 | */ |
||
270 | public function iProceedSelectingShippingCountry($shippingCountry) |
||
284 | |||
285 | /** |
||
286 | * @When /^I proceed selecting "([^"]*)" as shipping country with "([^"]*)" method$/ |
||
287 | */ |
||
288 | public function iProceedSelectingShippingCountryAndShippingMethod($shippingCountry, $shippingMethodName) |
||
295 | |||
296 | /** |
||
297 | * @When /^I proceed selecting "([^"]+)" shipping method$/ |
||
298 | * @Given /^I chose "([^"]*)" shipping method$/ |
||
299 | */ |
||
300 | public function iProceedSelectingShippingMethod($shippingMethodName) |
||
304 | |||
305 | /** |
||
306 | * @When /^I choose "([^"]*)" payment method$/ |
||
307 | */ |
||
308 | public function iChoosePaymentMethod($paymentMethodName) |
||
314 | |||
315 | /** |
||
316 | * @When /^I proceed selecting "([^"]*)" as shipping country with "([^"]*)" payment method$/ |
||
317 | */ |
||
318 | public function iProceedSelectingShippingCountryAndPaymentMethod($shippingCountry, $paymentMethodName) |
||
324 | |||
325 | /** |
||
326 | * @When I proceed selecting :paymentMethodName payment method |
||
327 | */ |
||
328 | public function iProceedSelectingOfflinePaymentMethod($paymentMethodName) |
||
332 | |||
333 | /** |
||
334 | * @When /^I change shipping method to "([^"]*)"$/ |
||
335 | */ |
||
336 | public function iChangeShippingMethod($shippingMethodName) |
||
342 | |||
343 | /** |
||
344 | * @Given /^I proceed logging as "([^"]*)" with "([^"]*)" password$/ |
||
345 | */ |
||
346 | public function iProceedLoggingAs($login, $password) |
||
353 | |||
354 | /** |
||
355 | * @When /^I proceed as guest "([^"]*)" with "([^"]*)" as shipping country$/ |
||
356 | */ |
||
357 | public function iProceedLoggingAsGuestWithAsShippingCountry($email, $shippingCountry) |
||
364 | |||
365 | /** |
||
366 | * @When I confirm my order |
||
367 | */ |
||
368 | public function iConfirmMyOrder() |
||
372 | |||
373 | /** |
||
374 | * @When I specify the password as :password |
||
375 | */ |
||
376 | public function iSpecifyThePasswordAs($password) |
||
380 | |||
381 | /** |
||
382 | * @When I sign in |
||
383 | */ |
||
384 | public function iSignIn() |
||
388 | |||
389 | /** |
||
390 | * @Then I should see the thank you page |
||
391 | */ |
||
392 | public function iShouldSeeTheThankYouPage() |
||
400 | |||
401 | /** |
||
402 | * @Then I should be redirected back to the thank you page |
||
403 | */ |
||
404 | public function iShouldBeRedirectedBackToTheThankYouPage() |
||
410 | |||
411 | /** |
||
412 | * @Then I should be redirected back to the order payment page |
||
413 | */ |
||
414 | public function iShouldBeRedirectedBackToTheOrderPaymentPage() |
||
420 | |||
421 | /** |
||
422 | * @Then I should be on the checkout shipping step |
||
423 | */ |
||
424 | public function iShouldBeOnTheCheckoutShippingStep() |
||
431 | |||
432 | /** |
||
433 | * @Then I should be on the checkout summary step |
||
434 | */ |
||
435 | public function iShouldBeOnTheCheckoutSummaryStep() |
||
442 | |||
443 | /** |
||
444 | * @Then I should see two cancelled payments and new one ready to be paid |
||
445 | */ |
||
446 | public function iShouldSeeTwoCancelledPaymentsAndNewOneReadyToBePaid() |
||
451 | |||
452 | /** |
||
453 | * @Then /^I should(?:| also) be notified that the "([^"]+)" and the "([^"]+)" in (shipping|billing) details are required$/ |
||
454 | */ |
||
455 | public function iShouldBeNotifiedThatTheAndTheInShippingDetailsAreRequired($firstElement, $secondElement, $type) |
||
460 | |||
461 | /** |
||
462 | * @Then I should be informed that my order cannot be shipped to this address |
||
463 | */ |
||
464 | public function iShouldBeInformedThatMyOrderCannotBeShippedToThisAddress() |
||
471 | |||
472 | /** |
||
473 | * @Then I should be able to log in |
||
474 | */ |
||
475 | public function iShouldBeAbleToLogIn() |
||
482 | |||
483 | /** |
||
484 | * @Then the login form should no longer be accessible |
||
485 | */ |
||
486 | public function theLoginFormShouldNoLongerBeAccessible() |
||
493 | |||
494 | /** |
||
495 | * @Then I should be notified about bad credentials |
||
496 | */ |
||
497 | public function iShouldBeNotifiedAboutBadCredentials() |
||
504 | |||
505 | /** |
||
506 | * @Then my order's shipping address should be to :fullName |
||
507 | */ |
||
508 | public function iShouldSeeThisShippingAddressAsShippingAddress($fullName) |
||
516 | |||
517 | /** |
||
518 | * @Then my order's billing address should be to :fullName |
||
519 | */ |
||
520 | public function iShouldSeeThisBillingAddressAsBillingAddress($fullName) |
||
528 | |||
529 | /** |
||
530 | * @Then address to :fullName should be used for both shipping and billing of my order` |
||
531 | */ |
||
532 | public function iShouldSeeThisShippingAddressAsShippingAndBillingAddress($fullName) |
||
537 | |||
538 | /** |
||
539 | * @Given I am at the checkout payment step |
||
540 | */ |
||
541 | public function iAmAtTheCheckoutPaymentStep() |
||
545 | |||
546 | /** |
||
547 | * @When I complete the payment step |
||
548 | */ |
||
549 | public function iCompleteThePaymentStep() |
||
553 | |||
554 | /** |
||
555 | * @When I select :paymentMethodName payment method |
||
556 | */ |
||
557 | public function iSelectPaymentMethod($paymentMethodName) |
||
561 | |||
562 | /** |
||
563 | * @Then I should not be able to select :paymentMethodName payment method |
||
564 | */ |
||
565 | public function iShouldNotBeAbleToSelectPaymentMethod($paymentMethodName) |
||
572 | |||
573 | /** |
||
574 | * @When I proceed order with :shippingMethod shipping method and :paymentMethod payment |
||
575 | */ |
||
576 | public function iProceedOrderWithShippingMethodAndPayment($shippingMethod, $paymentMethod) |
||
583 | |||
584 | /** |
||
585 | * @Given I should have :quantity :productName products in the cart |
||
586 | */ |
||
587 | public function iShouldHaveProductsInTheCart($quantity, $productName) |
||
594 | |||
595 | /** |
||
596 | * @param string $type |
||
597 | * @param string $element |
||
598 | * @param string $expectedMessage |
||
599 | * |
||
600 | * @throws \InvalidArgumentException |
||
601 | */ |
||
602 | private function assertElementValidationMessage($type, $element, $expectedMessage) |
||
610 | |||
611 | /** |
||
612 | * @return OrderInterface |
||
613 | * |
||
614 | * @throws \RuntimeException |
||
615 | */ |
||
616 | private function getLastOrder() |
||
628 | } |
||
629 |
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.