Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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( |
||
| 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 | View Code Duplication | 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 | View Code Duplication | public function iSpecifyTheBillingAddressAs(AddressInterface $address) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * @When /^I specified the shipping (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ |
||
| 225 | */ |
||
| 226 | View Code Duplication | public function iSpecifiedTheShippingAddress(AddressInterface $address) |
|
| 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) |
||
| 250 | { |
||
| 251 | $this->addressingPage->specifyEmail($email); |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @When I select :shippingMethod shipping method |
||
| 256 | */ |
||
| 257 | public function iSelectShippingMethod($shippingMethod) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @Then I should not be able to select :shippingMethod shipping method |
||
| 264 | */ |
||
| 265 | public function iShouldNotBeAbleToSelectShippingMethod($shippingMethod) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @When I complete the addressing step |
||
| 275 | * @When I try to complete the addressing step |
||
| 276 | */ |
||
| 277 | public function iCompleteTheAddressingStep() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @When I complete the shipping step |
||
| 284 | */ |
||
| 285 | public function iCompleteTheShippingStep() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @When /^I proceed selecting "([^"]*)" as shipping country$/ |
||
| 292 | */ |
||
| 293 | public function iProceedSelectingShippingCountry($shippingCountry) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @When /^I proceed selecting "([^"]*)" as shipping country with "([^"]*)" method$/ |
||
| 310 | */ |
||
| 311 | public function iProceedSelectingShippingCountryAndShippingMethod($shippingCountry, $shippingMethodName) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @When /^I proceed selecting "([^"]+)" shipping method$/ |
||
| 321 | * @Given /^I chose "([^"]*)" shipping method$/ |
||
| 322 | */ |
||
| 323 | public function iProceedSelectingShippingMethod($shippingMethodName) |
||
| 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 | * @When /^I provide additional note like "([^"]+)"$/ |
||
| 368 | */ |
||
| 369 | public function iProvideAdditionalNotesLike($notes) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @Given /^I proceed logging as "([^"]*)" with "([^"]*)" password$/ |
||
| 377 | */ |
||
| 378 | public function iProceedLoggingAs($login, $password) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @When /^I proceed as guest "([^"]*)" with "([^"]*)" as shipping country$/ |
||
| 388 | */ |
||
| 389 | public function iProceedLoggingAsGuestWithAsShippingCountry($email, $shippingCountry) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @When I confirm my order |
||
| 399 | */ |
||
| 400 | public function iConfirmMyOrder() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @When I specify the password as :password |
||
| 407 | */ |
||
| 408 | public function iSpecifyThePasswordAs($password) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @When I sign in |
||
| 415 | */ |
||
| 416 | public function iSignIn() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @Then I should see the thank you page |
||
| 423 | */ |
||
| 424 | public function iShouldSeeTheThankYouPage() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @Then I should be redirected back to the thank you page |
||
| 435 | */ |
||
| 436 | public function iShouldBeRedirectedBackToTheThankYouPage() |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @Then I should be redirected back to the order payment page |
||
| 445 | */ |
||
| 446 | public function iShouldBeRedirectedBackToTheOrderPaymentPage() |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @Then I should be on the checkout shipping step |
||
| 455 | */ |
||
| 456 | public function iShouldBeOnTheCheckoutShippingStep() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @Then I should be on the checkout summary step |
||
| 466 | */ |
||
| 467 | public function iShouldBeOnTheCheckoutSummaryStep() |
||
| 474 | |||
| 475 | /** |
||
| 476 | * @Then I should see two cancelled payments and new one ready to be paid |
||
| 477 | */ |
||
| 478 | public function iShouldSeeTwoCancelledPaymentsAndNewOneReadyToBePaid() |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @Then /^I should(?:| also) be notified that the "([^"]+)" and the "([^"]+)" in (shipping|billing) details are required$/ |
||
| 486 | */ |
||
| 487 | public function iShouldBeNotifiedThatTheAndTheInShippingDetailsAreRequired($firstElement, $secondElement, $type) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * @Then I should be informed that my order cannot be shipped to this address |
||
| 495 | */ |
||
| 496 | public function iShouldBeInformedThatMyOrderCannotBeShippedToThisAddress() |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @Then I should be able to log in |
||
| 506 | */ |
||
| 507 | public function iShouldBeAbleToLogIn() |
||
| 514 | |||
| 515 | /** |
||
| 516 | * @Then the login form should no longer be accessible |
||
| 517 | */ |
||
| 518 | public function theLoginFormShouldNoLongerBeAccessible() |
||
| 525 | |||
| 526 | /** |
||
| 527 | * @Then I should be notified about bad credentials |
||
| 528 | */ |
||
| 529 | public function iShouldBeNotifiedAboutBadCredentials() |
||
| 536 | |||
| 537 | /** |
||
| 538 | * @Then my order's shipping address should be to :fullName |
||
| 539 | */ |
||
| 540 | View Code Duplication | public function iShouldSeeThisShippingAddressAsShippingAddress($fullName) |
|
| 548 | |||
| 549 | /** |
||
| 550 | * @Then my order's billing address should be to :fullName |
||
| 551 | */ |
||
| 552 | View Code Duplication | public function iShouldSeeThisBillingAddressAsBillingAddress($fullName) |
|
| 560 | |||
| 561 | /** |
||
| 562 | * @Then address to :fullName should be used for both shipping and billing of my order` |
||
| 563 | */ |
||
| 564 | public function iShouldSeeThisShippingAddressAsShippingAndBillingAddress($fullName) |
||
| 569 | |||
| 570 | /** |
||
| 571 | * @Given I am at the checkout payment step |
||
| 572 | */ |
||
| 573 | public function iAmAtTheCheckoutPaymentStep() |
||
| 577 | |||
| 578 | /** |
||
| 579 | * @When I complete the payment step |
||
| 580 | */ |
||
| 581 | public function iCompleteThePaymentStep() |
||
| 585 | |||
| 586 | /** |
||
| 587 | * @When I select :paymentMethodName payment method |
||
| 588 | */ |
||
| 589 | public function iSelectPaymentMethod($paymentMethodName) |
||
| 593 | |||
| 594 | /** |
||
| 595 | * @Then I should not be able to select :paymentMethodName payment method |
||
| 596 | */ |
||
| 597 | public function iShouldNotBeAbleToSelectPaymentMethod($paymentMethodName) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * @When I proceed order with :shippingMethod shipping method and :paymentMethod payment |
||
| 607 | */ |
||
| 608 | public function iProceedOrderWithShippingMethodAndPayment($shippingMethod, $paymentMethod) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * @Given I should have :quantity :productName products in the cart |
||
| 618 | */ |
||
| 619 | public function iShouldHaveProductsInTheCart($quantity, $productName) |
||
| 626 | |||
| 627 | /** |
||
| 628 | * @Given I should see the shipping total with price :price |
||
| 629 | */ |
||
| 630 | public function iShouldSeeTheShippingTotalWithPrice($price) |
||
| 637 | |||
| 638 | /** |
||
| 639 | * @Then /^the ("[^"]+" product) should have unit price discounted by ("\$\d+")$/ |
||
| 640 | */ |
||
| 641 | public function theShouldHaveUnitPriceDiscountedFor(ProductInterface $product, $amount) |
||
| 648 | |||
| 649 | /** |
||
| 650 | * @Then /^my order total should be ("\$\d+")$/ |
||
| 651 | */ |
||
| 652 | public function myOrderTotalShouldBe($total) |
||
| 659 | |||
| 660 | /** |
||
| 661 | * @Given I should see promotion total :promotionTotal |
||
| 662 | */ |
||
| 663 | public function iShouldSeeDiscountTotal($promotionTotal) |
||
| 670 | |||
| 671 | /** |
||
| 672 | * @Given I should see promotion :promotionName |
||
| 673 | */ |
||
| 674 | public function iShouldSeePromotion($promotionName) |
||
| 681 | |||
| 682 | /** |
||
| 683 | * @Given my tax total should be :taxTotal |
||
| 684 | */ |
||
| 685 | public function myTaxTotalShouldBe($taxTotal) |
||
| 692 | |||
| 693 | /** |
||
| 694 | * @Then I should see :shippingMethod shipping method |
||
| 695 | */ |
||
| 696 | public function iShouldSeeThisShippingMethod(ShippingMethodInterface $shippingMethod) |
||
| 703 | |||
| 704 | /** |
||
| 705 | * @Then I should see :paymentMethod payment method |
||
| 706 | */ |
||
| 707 | public function iShouldSeeThisPaymentMethod(PaymentMethodInterface $paymentMethod) |
||
| 714 | |||
| 715 | /** |
||
| 716 | * @param string $type |
||
| 717 | * @param string $element |
||
| 718 | * @param string $expectedMessage |
||
| 719 | * |
||
| 720 | * @throws \InvalidArgumentException |
||
| 721 | */ |
||
| 722 | private function assertElementValidationMessage($type, $element, $expectedMessage) |
||
| 730 | |||
| 731 | /** |
||
| 732 | * @return OrderInterface |
||
| 733 | * |
||
| 734 | * @throws \RuntimeException |
||
| 735 | */ |
||
| 736 | View Code Duplication | private function getLastOrder() |
|
| 748 | } |
||
| 749 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.