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 |
||
| 40 | final class CheckoutContext implements Context |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * @var SharedStorageInterface |
||
| 44 | */ |
||
| 45 | private $sharedStorage; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var SecurityStepInterface |
||
| 49 | */ |
||
| 50 | private $checkoutSecurityStep; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var AddressingStepInterface |
||
| 54 | */ |
||
| 55 | private $checkoutAddressingStep; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var AddressingPageInterface |
||
| 59 | */ |
||
| 60 | private $addressingPage; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var ShippingStepInterface |
||
| 64 | */ |
||
| 65 | private $checkoutShippingStep; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var PaymentStepInterface |
||
| 69 | */ |
||
| 70 | private $checkoutPaymentStep; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var PaymentPageInterface |
||
| 74 | */ |
||
| 75 | private $paymentPage; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var FinalizeStepInterface |
||
| 79 | */ |
||
| 80 | private $checkoutFinalizeStep; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var ThankYouPageInterface |
||
| 84 | */ |
||
| 85 | private $checkoutThankYouPage; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var OrderPaymentsPageInterface |
||
| 89 | */ |
||
| 90 | private $orderPaymentsPage; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var ShippingPageInterface |
||
| 94 | */ |
||
| 95 | private $shippingPage; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var SummaryPageInterface |
||
| 99 | */ |
||
| 100 | private $summaryPage; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var OrderRepositoryInterface |
||
| 104 | */ |
||
| 105 | private $orderRepository; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var SecurityServiceInterface |
||
| 109 | */ |
||
| 110 | private $securityService; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param SharedStorageInterface $sharedStorage |
||
| 114 | * @param SecurityStepInterface $checkoutSecurityStep |
||
| 115 | * @param AddressingStepInterface $checkoutAddressingStep |
||
| 116 | * @param AddressingPageInterface $addressingPage |
||
| 117 | * @param ShippingStepInterface $checkoutShippingStep |
||
| 118 | * @param ShippingPageInterface $shippingPage |
||
| 119 | * @param PaymentStepInterface $checkoutPaymentStep |
||
| 120 | * @param PaymentPageInterface $paymentPage |
||
| 121 | * @param SummaryPageInterface $summaryPage |
||
| 122 | * @param FinalizeStepInterface $checkoutFinalizeStep |
||
| 123 | * @param ThankYouPageInterface $checkoutThankYouPage |
||
| 124 | * @param OrderPaymentsPageInterface $orderPaymentsPage |
||
| 125 | * @param OrderRepositoryInterface $orderRepository |
||
| 126 | * @param SecurityServiceInterface $securityService |
||
| 127 | */ |
||
| 128 | public function __construct( |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @Given /^I proceed without selecting shipping address$/ |
||
| 162 | */ |
||
| 163 | public function iProceedWithoutSelectingShippingAddress() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @Given I am at the checkout addressing step |
||
| 171 | */ |
||
| 172 | public function iAmAtTheCheckoutAddressingStep() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @Given /^(this user) bought this product$/ |
||
| 179 | */ |
||
| 180 | public function thisUserBought(UserInterface $user) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @When /^I specify the shipping (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ |
||
| 190 | * @When /^I (do not specify any shipping address) information$/ |
||
| 191 | */ |
||
| 192 | public function iSpecifyTheShippingAddressAs(AddressInterface $address) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @When /^I specify the billing (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ |
||
| 206 | * @When /^I (do not specify any billing address) information$/ |
||
| 207 | */ |
||
| 208 | public function iSpecifyTheBillingAddressAs(AddressInterface $address) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @When /^I specified the shipping (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ |
||
| 223 | */ |
||
| 224 | public function iSpecifiedTheShippingAddress(AddressInterface $address) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @When I choose the different billing address |
||
| 237 | */ |
||
| 238 | public function iChooseTheDifferentBillingAddress() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @When I specify the email as :email |
||
| 245 | * @When I do not specify the email |
||
| 246 | */ |
||
| 247 | public function iSpecifyTheEmail($email = null) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @When I select :shippingMethod shipping method |
||
| 254 | */ |
||
| 255 | public function iSelectShippingMethod($shippingMethod) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @Then I should not be able to select :shippingMethod shipping method |
||
| 262 | */ |
||
| 263 | public function iShouldNotBeAbleToSelectShippingMethod($shippingMethod) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @When I complete the addressing step |
||
| 273 | * @When I try to complete the addressing step |
||
| 274 | */ |
||
| 275 | public function iCompleteTheAddressingStep() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @When I complete the shipping step |
||
| 282 | */ |
||
| 283 | public function iCompleteTheShippingStep() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @When /^I proceed selecting "([^"]*)" as shipping country$/ |
||
| 290 | */ |
||
| 291 | public function iProceedSelectingShippingCountry($shippingCountry) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @When /^I proceed selecting "([^"]*)" as shipping country with "([^"]*)" method$/ |
||
| 308 | */ |
||
| 309 | public function iProceedSelectingShippingCountryAndShippingMethod($shippingCountry, $shippingMethodName) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @When /^I proceed selecting "([^"]+)" shipping method$/ |
||
| 319 | * @Given /^I chose "([^"]*)" shipping method$/ |
||
| 320 | */ |
||
| 321 | public function iProceedSelectingShippingMethod($shippingMethodName) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @When /^I choose "([^"]*)" payment method$/ |
||
| 328 | */ |
||
| 329 | public function iChoosePaymentMethod($paymentMethodName) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @When /^I proceed selecting "([^"]*)" as shipping country with "([^"]*)" payment method$/ |
||
| 338 | */ |
||
| 339 | public function iProceedSelectingShippingCountryAndPaymentMethod($shippingCountry, $paymentMethodName) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @When I proceed selecting :paymentMethodName payment method |
||
| 348 | */ |
||
| 349 | public function iProceedSelectingOfflinePaymentMethod($paymentMethodName = 'Offline') |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @When /^I change shipping method to "([^"]*)"$/ |
||
| 356 | */ |
||
| 357 | public function iChangeShippingMethod($shippingMethodName) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @Given /^I proceed logging as "([^"]*)" with "([^"]*)" password$/ |
||
| 366 | */ |
||
| 367 | public function iProceedLoggingAs($login, $password) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @When /^I proceed as guest "([^"]*)" with "([^"]*)" as shipping country$/ |
||
| 377 | */ |
||
| 378 | public function iProceedLoggingAsGuestWithAsShippingCountry($email, $shippingCountry) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @When I confirm my order |
||
| 388 | */ |
||
| 389 | public function iConfirmMyOrder() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @When I specify the password as :password |
||
| 396 | */ |
||
| 397 | public function iSpecifyThePasswordAs($password) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @When I sign in |
||
| 404 | */ |
||
| 405 | public function iSignIn() |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @Then I should see the thank you page |
||
| 412 | */ |
||
| 413 | public function iShouldSeeTheThankYouPage() |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @Then I should be redirected back to the thank you page |
||
| 424 | */ |
||
| 425 | public function iShouldBeRedirectedBackToTheThankYouPage() |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @Then I should be redirected back to the order payment page |
||
| 434 | */ |
||
| 435 | public function iShouldBeRedirectedBackToTheOrderPaymentPage() |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @Then I should be on the checkout shipping step |
||
| 444 | */ |
||
| 445 | public function iShouldBeOnTheCheckoutShippingStep() |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @Then I should be on the checkout summary step |
||
| 455 | */ |
||
| 456 | public function iShouldBeOnTheCheckoutSummaryStep() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @Then I should see two cancelled payments and new one ready to be paid |
||
| 466 | */ |
||
| 467 | public function iShouldSeeTwoCancelledPaymentsAndNewOneReadyToBePaid() |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @Then /^I should(?:| also) be notified that the "([^"]+)" and the "([^"]+)" in (shipping|billing) details are required$/ |
||
| 475 | */ |
||
| 476 | public function iShouldBeNotifiedThatTheAndTheInShippingDetailsAreRequired($firstElement, $secondElement, $type) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * @Then I should be informed that my order cannot be shipped to this address |
||
| 484 | */ |
||
| 485 | public function iShouldBeInformedThatMyOrderCannotBeShippedToThisAddress() |
||
| 492 | |||
| 493 | /** |
||
| 494 | * @Then I should be able to log in |
||
| 495 | */ |
||
| 496 | public function iShouldBeAbleToLogIn() |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @Then the login form should no longer be accessible |
||
| 506 | */ |
||
| 507 | public function theLoginFormShouldNoLongerBeAccessible() |
||
| 514 | |||
| 515 | /** |
||
| 516 | * @Then I should be notified about bad credentials |
||
| 517 | */ |
||
| 518 | public function iShouldBeNotifiedAboutBadCredentials() |
||
| 525 | |||
| 526 | /** |
||
| 527 | * @Then my order's shipping address should be to :fullName |
||
| 528 | */ |
||
| 529 | public function iShouldSeeThisShippingAddressAsShippingAddress($fullName) |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @Then my order's billing address should be to :fullName |
||
| 540 | */ |
||
| 541 | public function iShouldSeeThisBillingAddressAsBillingAddress($fullName) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * @Then address to :fullName should be used for both shipping and billing of my order` |
||
| 552 | */ |
||
| 553 | public function iShouldSeeThisShippingAddressAsShippingAndBillingAddress($fullName) |
||
| 558 | |||
| 559 | /** |
||
| 560 | * @Given I am at the checkout payment step |
||
| 561 | */ |
||
| 562 | public function iAmAtTheCheckoutPaymentStep() |
||
| 566 | |||
| 567 | /** |
||
| 568 | * @When I complete the payment step |
||
| 569 | */ |
||
| 570 | public function iCompleteThePaymentStep() |
||
| 574 | |||
| 575 | /** |
||
| 576 | * @When I select :paymentMethodName payment method |
||
| 577 | */ |
||
| 578 | public function iSelectPaymentMethod($paymentMethodName) |
||
| 582 | |||
| 583 | /** |
||
| 584 | * @Then I should not be able to select :paymentMethodName payment method |
||
| 585 | */ |
||
| 586 | public function iShouldNotBeAbleToSelectPaymentMethod($paymentMethodName) |
||
| 593 | |||
| 594 | /** |
||
| 595 | * @When I proceed order with :shippingMethod shipping method and :paymentMethod payment |
||
| 596 | */ |
||
| 597 | public function iProceedOrderWithShippingMethodAndPayment($shippingMethod, $paymentMethod) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * @Given I should have :quantity :productName products in the cart |
||
| 607 | */ |
||
| 608 | public function iShouldHaveProductsInTheCart($quantity, $productName) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * @Then /^the ("[^"]+" product) should have unit price discounted by ("\$\d+")$/ |
||
| 618 | */ |
||
| 619 | public function theShouldHaveUnitPriceDiscountedFor(ProductInterface $product, $amount) |
||
| 626 | |||
| 627 | /** |
||
| 628 | * @Then /^my order total should be ("\$\d+")$/ |
||
| 629 | */ |
||
| 630 | public function myOrderTotalShouldBe($total) |
||
| 637 | |||
| 638 | /** |
||
| 639 | * @param string $type |
||
| 640 | * @param string $element |
||
| 641 | * @param string $expectedMessage |
||
| 642 | * |
||
| 643 | * @throws \InvalidArgumentException |
||
| 644 | */ |
||
| 645 | private function assertElementValidationMessage($type, $element, $expectedMessage) |
||
| 653 | |||
| 654 | /** |
||
| 655 | * @return OrderInterface |
||
| 656 | * |
||
| 657 | * @throws \RuntimeException |
||
| 658 | */ |
||
| 659 | private function getLastOrder() |
||
| 671 | } |
||
| 672 |
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.