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 |
||
| 36 | final class CheckoutContext implements Context |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @var SharedStorageInterface |
||
| 40 | */ |
||
| 41 | private $sharedStorage; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var SecurityStepInterface |
||
| 45 | */ |
||
| 46 | private $checkoutSecurityStep; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var AddressingStepInterface |
||
| 50 | */ |
||
| 51 | private $checkoutAddressingStep; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var AddressingPageInterface |
||
| 55 | */ |
||
| 56 | private $addressingPage; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var ShippingStepInterface |
||
| 60 | */ |
||
| 61 | private $checkoutShippingStep; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var PaymentStepInterface |
||
| 65 | */ |
||
| 66 | private $checkoutPaymentStep; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var PaymentPageInterface |
||
| 70 | */ |
||
| 71 | private $paymentPage; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var FinalizeStepInterface |
||
| 75 | */ |
||
| 76 | private $checkoutFinalizeStep; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var ThankYouPageInterface |
||
| 80 | */ |
||
| 81 | private $checkoutThankYouPage; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var OrderPaymentsPageInterface |
||
| 85 | */ |
||
| 86 | private $orderPaymentsPage; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var ShippingPageInterface |
||
| 90 | */ |
||
| 91 | private $shippingPage; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var OrderRepositoryInterface |
||
| 95 | */ |
||
| 96 | private $orderRepository; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param SharedStorageInterface $sharedStorage |
||
| 100 | * @param SecurityStepInterface $checkoutSecurityStep |
||
| 101 | * @param AddressingStepInterface $checkoutAddressingStep |
||
| 102 | * @param AddressingPageInterface $addressingPage |
||
| 103 | * @param ShippingStepInterface $checkoutShippingStep |
||
| 104 | * @param ShippingPageInterface $shippingPage |
||
| 105 | * @param PaymentStepInterface $checkoutPaymentStep |
||
| 106 | * @param PaymentPageInterface $paymentPage |
||
| 107 | * @param FinalizeStepInterface $checkoutFinalizeStep |
||
| 108 | * @param ThankYouPageInterface $checkoutThankYouPage |
||
| 109 | * @param OrderPaymentsPageInterface $orderPaymentsPage |
||
| 110 | * @param OrderRepositoryInterface $orderRepository |
||
| 111 | */ |
||
| 112 | public function __construct( |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @Given /^I proceed without selecting shipping address$/ |
||
| 142 | */ |
||
| 143 | public function iProceedWithoutSelectingShippingAddress() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @Given I am at the checkout addressing step |
||
| 151 | */ |
||
| 152 | public function iAmAtTheCheckoutAddressingStep() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @When /^I specify the shipping (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ |
||
| 159 | * @When /^I (do not specify any shipping address) information$/ |
||
| 160 | */ |
||
| 161 | public function iSpecifyTheShippingAddressAs(AddressInterface $address) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @When /^I specify the billing (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ |
||
| 168 | * @When /^I (do not specify any billing address) information$/ |
||
| 169 | */ |
||
| 170 | public function iSpecifyTheBillingAddressAs(AddressInterface $address) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @When /^I specified the shipping (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ |
||
| 177 | */ |
||
| 178 | public function iSpecifiedTheShippingAddress(AddressInterface $address) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @When I choose the different billing address |
||
| 187 | */ |
||
| 188 | public function iChooseTheDifferentBillingAddress() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @When I specify the email as :email |
||
| 195 | * @When I do not specify the email |
||
| 196 | */ |
||
| 197 | public function iSpecifyTheEmail($email = null) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @When I select :shippingMethod shipping method |
||
| 204 | */ |
||
| 205 | public function iSelectShippingMethod($shippingMethod) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @Then I should not be able to select :shippingMethod shipping method |
||
| 212 | */ |
||
| 213 | public function iShouldNotBeAbleToSelectShippingMethod($shippingMethod) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @When I complete the addressing step |
||
| 223 | * @When I try to complete the addressing step |
||
| 224 | */ |
||
| 225 | public function iCompleteTheAddressingStep() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @When I complete the shipping step |
||
| 232 | */ |
||
| 233 | public function iCompleteTheShippingStep() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @When /^I proceed selecting "([^"]*)" as shipping country$/ |
||
| 240 | */ |
||
| 241 | public function iProceedSelectingShippingCountry($shippingCountry) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @When /^I proceed selecting "([^"]*)" as shipping country with "([^"]*)" method$/ |
||
| 258 | */ |
||
| 259 | public function iProceedSelectingShippingCountryAndShippingMethod($shippingCountry, $shippingMethodName) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @When /^I proceed selecting "([^"]+)" shipping method$/ |
||
| 269 | * @Given /^I chose "([^"]*)" shipping method$/ |
||
| 270 | */ |
||
| 271 | public function iProceedSelectingShippingMethod($shippingMethodName) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @When /^I choose "([^"]*)" payment method$/ |
||
| 278 | */ |
||
| 279 | public function iChoosePaymentMethod($paymentMethodName) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @When /^I proceed selecting "([^"]*)" as shipping country with "([^"]*)" payment method$/ |
||
| 288 | */ |
||
| 289 | public function iProceedSelectingShippingCountryAndPaymentMethod($shippingCountry, $paymentMethodName) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @When I proceed selecting :paymentMethodName payment method |
||
| 298 | */ |
||
| 299 | public function iProceedSelectingOfflinePaymentMethod($paymentMethodName) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @When /^I change shipping method to "([^"]*)"$/ |
||
| 306 | */ |
||
| 307 | public function iChangeShippingMethod($shippingMethodName) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @Given /^I proceed logging as "([^"]*)" with "([^"]*)" password$/ |
||
| 316 | */ |
||
| 317 | public function iProceedLoggingAs($login, $password) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @When /^I proceed as guest "([^"]*)" with "([^"]*)" as shipping country$/ |
||
| 327 | */ |
||
| 328 | public function iProceedLoggingAsGuestWithAsShippingCountry($email, $shippingCountry) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @When I confirm my order |
||
| 338 | */ |
||
| 339 | public function iConfirmMyOrder() |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @When I specify the password as :password |
||
| 346 | */ |
||
| 347 | public function iSpecifyThePasswordAs($password) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @When I sign in |
||
| 354 | */ |
||
| 355 | public function iSignIn() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @Then I should see the thank you page |
||
| 362 | */ |
||
| 363 | public function iShouldSeeTheThankYouPage() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @Then I should be redirected back to the thank you page |
||
| 374 | */ |
||
| 375 | public function iShouldBeRedirectedBackToTheThankYouPage() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @Then I should be redirected back to the order payment page |
||
| 384 | */ |
||
| 385 | public function iShouldBeRedirectedBackToTheOrderPaymentPage() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @Then I should be on the checkout shipping step |
||
| 394 | */ |
||
| 395 | public function iShouldBeOnTheCheckoutShippingStep() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @Then I should see two cancelled payments and new one ready to be paid |
||
| 405 | */ |
||
| 406 | public function iShouldSeeTwoCancelledPaymentsAndNewOneReadyToBePaid() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @Then /^I should(?:| also) be notified that the "([^"]+)" and the "([^"]+)" in (shipping|billing) details are required$/ |
||
| 414 | */ |
||
| 415 | public function iShouldBeNotifiedThatTheAndTheInShippingDetailsAreRequired($firstElement, $secondElement, $type) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @Then I should be informed that my order cannot be shipped to this address |
||
| 423 | */ |
||
| 424 | public function iShouldBeInformedThatMyOrderCannotBeShippedToThisAddress() |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @Then I should be able to log in |
||
| 434 | */ |
||
| 435 | public function iShouldBeAbleToLogIn() |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @Then the login form should no longer be accessible |
||
| 445 | */ |
||
| 446 | public function theLoginFormShouldNoLongerBeAccessible() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @Then I should be notified about bad credentials |
||
| 456 | */ |
||
| 457 | public function iShouldBeNotifiedAboutBadCredentials() |
||
| 464 | |||
| 465 | /** |
||
| 466 | * @Given I am at the checkout payment step |
||
| 467 | */ |
||
| 468 | public function iAmAtTheCheckoutPaymentStep() |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @When I complete the payment step |
||
| 475 | */ |
||
| 476 | public function iCompleteThePaymentStep() |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @When I select :paymentMethodName payment method |
||
| 483 | */ |
||
| 484 | public function iSelectPaymentMethod($paymentMethodName) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @Then I should not be able to select :paymentMethodName payment method |
||
| 491 | */ |
||
| 492 | public function iShouldNotBeAbleToSelectPaymentMethod($paymentMethodName) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @param string $type |
||
| 502 | * @param string $element |
||
| 503 | * @param string $expectedMessage |
||
| 504 | * |
||
| 505 | * @throws \InvalidArgumentException |
||
| 506 | */ |
||
| 507 | private function assertElementValidationMessage($type, $element, $expectedMessage) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @return OrderInterface |
||
| 518 | * |
||
| 519 | * @throws \RuntimeException |
||
| 520 | */ |
||
| 521 | private function getLastOrder() |
||
| 533 | } |
||
| 534 |