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 |
||
| 35 | final class CheckoutContext implements Context |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @var SharedStorageInterface |
||
| 39 | */ |
||
| 40 | private $sharedStorage; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var AddressPageInterface |
||
| 44 | */ |
||
| 45 | private $addressPage; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var SelectPaymentPageInterface |
||
| 49 | */ |
||
| 50 | private $selectPaymentPage; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var SelectShippingPageInterface |
||
| 54 | */ |
||
| 55 | private $selectShippingPage; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var CompletePageInterface |
||
| 59 | */ |
||
| 60 | private $completePage; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var CurrentPageResolverInterface |
||
| 64 | */ |
||
| 65 | private $currentPageResolver; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var AddressingContext |
||
| 69 | */ |
||
| 70 | private $addressingContext; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param SharedStorageInterface $sharedStorage |
||
| 74 | * @param AddressPageInterface $addressPage |
||
| 75 | * @param SelectPaymentPageInterface $selectPaymentPage |
||
| 76 | * @param SelectShippingPageInterface $selectShippingPage |
||
| 77 | * @param CompletePageInterface $completePage |
||
| 78 | * @param CurrentPageResolverInterface $currentPageResolver |
||
| 79 | * @param AddressingContext $addressingContext |
||
| 80 | */ |
||
| 81 | public function __construct( |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @Given I was at the checkout summary step |
||
| 101 | */ |
||
| 102 | public function iWasAtTheCheckoutSummaryStep() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @Given I have proceeded selecting :shippingMethodName shipping method |
||
| 110 | */ |
||
| 111 | public function iHaveProceededSelectingShippingMethod($shippingMethodName) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @When I try to open checkout shipping page |
||
| 119 | */ |
||
| 120 | public function iTryToOpenCheckoutShippingPage() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @When I try to open checkout payment page |
||
| 127 | */ |
||
| 128 | public function iTryToOpenCheckoutPaymentPage() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @When I try to open checkout complete page |
||
| 135 | */ |
||
| 136 | public function iTryToOpenCheckoutCompletePage() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @Given I have selected :shippingMethod shipping method |
||
| 143 | * @When I select :shippingMethod shipping method |
||
| 144 | */ |
||
| 145 | public function iSelectShippingMethod($shippingMethod) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @Then I should not be able to select :shippingMethodName shipping method |
||
| 152 | */ |
||
| 153 | public function iShouldNotBeAbleToSelectShippingMethod($shippingMethodName) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @Then I should have :shippingMethodName shipping method available as the first choice |
||
| 163 | */ |
||
| 164 | public function iShouldHaveShippingMethodAvailableAsFirstChoice($shippingMethodName) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @Then I should have :shippingMethodName shipping method available as the last choice |
||
| 174 | */ |
||
| 175 | public function iShouldHaveShippingMethodAvailableAsLastChoice($shippingMethodName) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @When /^I(?:| try to) complete the shipping step$/ |
||
| 185 | */ |
||
| 186 | public function iCompleteTheShippingStep() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @When I decide to change my address |
||
| 193 | */ |
||
| 194 | public function iDecideToChangeMyAddress() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @When I decide to change order shipping method |
||
| 201 | */ |
||
| 202 | public function iDecideToChangeMyShippingMethod() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @When I go to the addressing step |
||
| 209 | */ |
||
| 210 | public function iGoToTheAddressingStep() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @When I go to the shipping step |
||
| 235 | */ |
||
| 236 | public function iGoToTheShippingStep() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @When I decide to change the payment method |
||
| 255 | */ |
||
| 256 | public function iGoToThePaymentStep() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @When /^I proceed selecting ("[^"]+" as shipping country) with "([^"]+)" method$/ |
||
| 263 | */ |
||
| 264 | public function iProceedSelectingShippingCountryAndShippingMethod(CountryInterface $shippingCountry = null, $shippingMethodName = null) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @When /^I proceed selecting "([^"]+)" shipping method$/ |
||
| 274 | * @Given /^I chose "([^"]*)" shipping method$/ |
||
| 275 | */ |
||
| 276 | public function iProceedSelectingShippingMethod($shippingMethodName) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @When /^I choose "([^"]*)" payment method$/ |
||
| 283 | */ |
||
| 284 | public function iChoosePaymentMethod($paymentMethodName) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @When I go back to shipping step of the checkout |
||
| 292 | */ |
||
| 293 | public function iGoBackToShippingStepOfTheCheckout() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @Given I have proceeded selecting :paymentMethodName payment method |
||
| 300 | * @When /^I (?:proceed|proceeded) selecting "([^"]+)" payment method$/ |
||
| 301 | */ |
||
| 302 | public function iProceedSelectingPaymentMethod($paymentMethodName = 'Offline') |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @When /^I change shipping method to "([^"]*)"$/ |
||
| 310 | */ |
||
| 311 | public function iChangeShippingMethod($shippingMethodName) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @When /^I provide additional note like "([^"]+)"$/ |
||
| 320 | */ |
||
| 321 | public function iProvideAdditionalNotesLike($notes) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @When I return to the checkout summary step |
||
| 329 | */ |
||
| 330 | public function iReturnToTheCheckoutSummaryStep() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @When I want to complete checkout |
||
| 337 | */ |
||
| 338 | public function iWantToCompleteCheckout() |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @When I want to pay for order |
||
| 345 | */ |
||
| 346 | public function iWantToPayForOrder() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @When I confirm my order |
||
| 353 | */ |
||
| 354 | public function iConfirmMyOrder() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @Then I should be on the checkout shipping step |
||
| 361 | */ |
||
| 362 | public function iShouldBeOnTheCheckoutShippingStep() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @Then I should be on the checkout complete step |
||
| 372 | */ |
||
| 373 | public function iShouldBeOnTheCheckoutCompleteStep() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @Then I should be on the checkout payment step |
||
| 380 | */ |
||
| 381 | public function iShouldBeOnTheCheckoutPaymentStep() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @Then I should be on the checkout summary step |
||
| 391 | */ |
||
| 392 | public function iShouldBeOnTheCheckoutSummaryStep() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @Then I should be informed that my order cannot be shipped to this address |
||
| 402 | */ |
||
| 403 | public function iShouldBeInformedThatMyOrderCannotBeShippedToThisAddress() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @Then my order's shipping address should be to :fullName |
||
| 413 | */ |
||
| 414 | public function iShouldSeeThisShippingAddressAsShippingAddress($fullName) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @Then my order's billing address should be to :fullName |
||
| 425 | */ |
||
| 426 | public function iShouldSeeThisBillingAddressAsBillingAddress($fullName) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @Then address to :fullName should be used for both shipping and billing of my order |
||
| 437 | */ |
||
| 438 | public function iShouldSeeThisShippingAddressAsShippingAndBillingAddress($fullName) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @When I go back to payment step of the checkout |
||
| 446 | */ |
||
| 447 | public function iAmAtTheCheckoutPaymentStep() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @When I complete the payment step |
||
| 454 | */ |
||
| 455 | public function iCompleteThePaymentStep() |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @When I select :name payment method |
||
| 462 | */ |
||
| 463 | public function iSelectPaymentMethod($name) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @When /^I do not modify anything$/ |
||
| 470 | */ |
||
| 471 | public function iDoNotModifyAnything() |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @Then I should not be able to select :paymentMethodName payment method |
||
| 478 | */ |
||
| 479 | public function iShouldNotBeAbleToSelectPaymentMethod($paymentMethodName) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * @Then I should be able to select :paymentMethodName payment method |
||
| 489 | */ |
||
| 490 | public function iShouldBeAbleToSelectPaymentMethod($paymentMethodName) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @Given I have proceeded order with :shippingMethod shipping method and :paymentMethod payment |
||
| 500 | * @When I proceed with :shippingMethod shipping method and :paymentMethod payment |
||
| 501 | */ |
||
| 502 | public function iProceedOrderWithShippingMethodAndPayment($shippingMethod, $paymentMethod) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * @When I proceed with :shippingMethod shipping method |
||
| 512 | */ |
||
| 513 | public function iProceedOrderWithShippingMethod($shippingMethod) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * @Given I should have :quantity :productName products in the cart |
||
| 521 | */ |
||
| 522 | public function iShouldHaveProductsInTheCart($quantity, $productName) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @Then my order shipping should be :price |
||
| 532 | */ |
||
| 533 | public function myOrderShippingShouldBe($price) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @Then /^the ("[^"]+" product) should have unit price discounted by ("\$\d+")$/ |
||
| 543 | */ |
||
| 544 | public function theShouldHaveUnitPriceDiscountedFor(ProductInterface $product, $amount) |
||
| 551 | |||
| 552 | /** |
||
| 553 | * @Then /^my order total should be ("(?:\£|\$)\d+(?:\.\d+)?")$/ |
||
| 554 | */ |
||
| 555 | public function myOrderTotalShouldBe($total) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @Then my order promotion total should be :promotionTotal |
||
| 565 | */ |
||
| 566 | public function myOrderPromotionTotalShouldBe($promotionTotal) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @Then :promotionName should be applied to my order |
||
| 576 | */ |
||
| 577 | public function shouldBeAppliedToMyOrder($promotionName) |
||
| 584 | |||
| 585 | /** |
||
| 586 | * @Then :promotionName should be applied to my order shipping |
||
| 587 | */ |
||
| 588 | public function shouldBeAppliedToMyOrderShipping($promotionName) |
||
| 592 | |||
| 593 | /** |
||
| 594 | * @Given my tax total should be :taxTotal |
||
| 595 | */ |
||
| 596 | public function myTaxTotalShouldBe($taxTotal) |
||
| 603 | |||
| 604 | /** |
||
| 605 | * @Then my order's shipping method should be :shippingMethod |
||
| 606 | */ |
||
| 607 | public function myOrderSShippingMethodShouldBe(ShippingMethodInterface $shippingMethod) |
||
| 614 | |||
| 615 | /** |
||
| 616 | * @Then my order's payment method should be :paymentMethod |
||
| 617 | */ |
||
| 618 | public function myOrderSPaymentMethodShouldBe(PaymentMethodInterface $paymentMethod) |
||
| 625 | |||
| 626 | /** |
||
| 627 | * @Then I should be able to go to the complete step again |
||
| 628 | */ |
||
| 629 | public function iShouldBeAbleToGoToTheCompleteStepAgain() |
||
| 635 | |||
| 636 | /** |
||
| 637 | * @Then I should be redirected to the shipping step |
||
| 638 | */ |
||
| 639 | public function iShouldBeRedirectedToTheShippingStep() |
||
| 646 | |||
| 647 | /** |
||
| 648 | * @Given I should be able to go to the payment step again |
||
| 649 | */ |
||
| 650 | public function iShouldBeAbleToGoToThePaymentStepAgain() |
||
| 659 | |||
| 660 | /** |
||
| 661 | * @Then I should be redirected to the payment step |
||
| 662 | */ |
||
| 663 | public function iShouldBeRedirectedToThePaymentStep() |
||
| 670 | |||
| 671 | /** |
||
| 672 | * @Given I should be able to go to the summary page again |
||
| 673 | */ |
||
| 674 | public function iShouldBeAbleToGoToTheSummaryPageAgain() |
||
| 683 | |||
| 684 | /** |
||
| 685 | * @Given I should see shipping method :shippingMethodName with fee :fee |
||
| 686 | */ |
||
| 687 | public function iShouldSeeShippingFee($shippingMethodName, $fee) |
||
| 694 | |||
| 695 | /** |
||
| 696 | * @Then the subtotal of :item item should be :price |
||
| 697 | */ |
||
| 698 | public function theSubtotalOfItemShouldBe($item, $price) |
||
| 709 | |||
| 710 | /** |
||
| 711 | * @Then the :product product should have unit price :price |
||
| 712 | */ |
||
| 713 | public function theProductShouldHaveUnitPrice(ProductInterface $product, $price) |
||
| 720 | |||
| 721 | /** |
||
| 722 | * @Given /^I should be notified that (this product) does not have sufficient stock$/ |
||
| 723 | */ |
||
| 724 | public function iShouldBeNotifiedThatThisProductDoesNotHaveSufficientStock(ProductInterface $product) |
||
| 731 | |||
| 732 | /** |
||
| 733 | * @Then my order's locale should be :localeName |
||
| 734 | */ |
||
| 735 | public function myOrderSLocaleShouldBe($localeName) |
||
| 742 | |||
| 743 | /** |
||
| 744 | * @Then /^I should not be notified that (this product) does not have sufficient stock$/ |
||
| 745 | */ |
||
| 746 | public function iShouldNotBeNotifiedThatThisProductDoesNotHaveSufficientStock(ProductInterface $product) |
||
| 753 | |||
| 754 | /** |
||
| 755 | * @Then I should see :provinceName in the shipping address |
||
| 756 | */ |
||
| 757 | public function iShouldSeeInTheShippingAddress($provinceName) |
||
| 764 | |||
| 765 | /** |
||
| 766 | * @Then I should see :provinceName in the billing address |
||
| 767 | */ |
||
| 768 | public function iShouldSeeInTheBillingAddress($provinceName) |
||
| 775 | |||
| 776 | /** |
||
| 777 | * @Then there should be information about no available shipping methods |
||
| 778 | */ |
||
| 779 | public function thereShouldBeInformationAboutNoShippingMethodsAvailableForMyShippingAddress() |
||
| 786 | |||
| 787 | /** |
||
| 788 | * @Then I should have :paymentMethodName payment method available as the first choice |
||
| 789 | */ |
||
| 790 | public function iShouldHavePaymentMethodAvailableAsFirstChoice($paymentMethodName) |
||
| 797 | |||
| 798 | /** |
||
| 799 | * @Then I should have :paymentMethodName payment method available as the last choice |
||
| 800 | */ |
||
| 801 | public function iShouldHavePaymentMethodAvailableAsLastChoice($paymentMethodName) |
||
| 808 | |||
| 809 | /** |
||
| 810 | * @Then I should see :shippingMethodName shipping method |
||
| 811 | */ |
||
| 812 | public function iShouldSeeShippingMethod($shippingMethodName) |
||
| 819 | |||
| 820 | /** |
||
| 821 | * @Then I should not see :shippingMethodName shipping method |
||
| 822 | */ |
||
| 823 | public function iShouldNotSeeShippingMethod($shippingMethodName) |
||
| 830 | |||
| 831 | /** |
||
| 832 | * @Then I should be checking out as :email |
||
| 833 | */ |
||
| 834 | public function iShouldBeCheckingOutAs($email) |
||
| 841 | |||
| 842 | /** |
||
| 843 | * @Then I should not see any information about payment method |
||
| 844 | */ |
||
| 845 | public function iShouldNotSeeAnyInformationAboutPaymentMethod() |
||
| 852 | |||
| 853 | /** |
||
| 854 | * @Then /^(this promotion) should give "([^"]+)" discount$/ |
||
| 855 | */ |
||
| 856 | public function thisPromotionShouldGiveDiscount(PromotionInterface $promotion, $discount) |
||
| 863 | |||
| 864 | /** |
||
| 865 | * @return SymfonyPageInterface |
||
| 866 | */ |
||
| 867 | private function resolveCurrentStepPage() |
||
| 878 | } |
||
| 879 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.