Complex classes like CheckoutAddressingContext 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 CheckoutAddressingContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | final class CheckoutAddressingContext implements Context |
||
| 27 | { |
||
| 28 | /** @var SharedStorageInterface */ |
||
| 29 | private $sharedStorage; |
||
| 30 | |||
| 31 | /** @var AddressPageInterface */ |
||
| 32 | private $addressPage; |
||
| 33 | |||
| 34 | /** @var FactoryInterface */ |
||
| 35 | private $addressFactory; |
||
| 36 | |||
| 37 | /** @var AddressComparatorInterface */ |
||
| 38 | private $addressComparator; |
||
| 39 | |||
| 40 | /** @var SelectShippingPageInterface */ |
||
| 41 | private $selectShippingPage; |
||
| 42 | |||
| 43 | public function __construct( |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @Given I am at the checkout addressing step |
||
| 59 | * @When I go to the checkout addressing step |
||
| 60 | * @When I go back to addressing step of the checkout |
||
| 61 | */ |
||
| 62 | public function iAmAtTheCheckoutAddressingStep(): void |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @Given /^I have completed addressing step with email "([^"]+)" and ("[^"]+" based billing address)$/ |
||
| 69 | * @When /^I complete addressing step with email "([^"]+)" and ("[^"]+" based billing address)$/ |
||
| 70 | */ |
||
| 71 | public function iCompleteAddressingStepWithEmail(string $email, AddressInterface $address): void |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @When /^I complete addressing step with ("[^"]+" based billing address)$/ |
||
| 81 | */ |
||
| 82 | public function iCompleteAddressingStepWithBasedBillingAddress(AddressInterface $address): void |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @When I specify the province name manually as :provinceName for shipping address |
||
| 91 | */ |
||
| 92 | public function iSpecifyTheProvinceNameManuallyAsForShippingAddress($provinceName) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @When I specify the province name manually as :provinceName for billing address |
||
| 99 | */ |
||
| 100 | public function iSpecifyTheProvinceNameManuallyAsForBillingAddress($provinceName) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @When I try to open checkout addressing page |
||
| 107 | */ |
||
| 108 | public function iTryToOpenCheckoutAddressingPage() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @When /^I choose ("[^"]+" street) for shipping address$/ |
||
| 115 | */ |
||
| 116 | public function iChooseForShippingAddress(AddressInterface $address) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @When /^I choose ("[^"]+" street) for billing address$/ |
||
| 124 | */ |
||
| 125 | public function iChooseForBillingAddress(AddressInterface $address) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @When /^I specify the shipping (address as "[^"]+", "[^"]+", "[^"]+", "[^"]+" for "[^"]+")$/ |
||
| 132 | * @When /^I specify the shipping (address for "[^"]+" from "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+")$/ |
||
| 133 | * @When /^I (do not specify any shipping address) information$/ |
||
| 134 | * @When /^I change the shipping (address to "[^"]+", "[^"]+", "[^"]+", "[^"]+" for "[^"]+")$/ |
||
| 135 | */ |
||
| 136 | public function iSpecifyTheShippingAddressAs(AddressInterface $address) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @When I specify shipping country province as :province |
||
| 152 | */ |
||
| 153 | public function iSpecifyShippingCountryProvinceAs($province) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @When I specify billing country province as :province |
||
| 160 | */ |
||
| 161 | public function iSpecifyBillingCountryProvinceAs($province) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @When /^I specify the billing (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ |
||
| 168 | * @When /^I specify the billing (address for "([^"]+)" from "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)")$/ |
||
| 169 | * @When /^I (do not specify any billing address) information$/ |
||
| 170 | */ |
||
| 171 | public function iSpecifyTheBillingAddressAs(AddressInterface $address) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @When /^I specified the billing (address as "[^"]+", "[^"]+", "[^"]+", "[^"]+" for "[^"]+")$/ |
||
| 185 | */ |
||
| 186 | public function iSpecifiedTheBillingAddress(AddressInterface $address = null) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @When I specify the email as :email |
||
| 203 | * @When I do not specify the email |
||
| 204 | */ |
||
| 205 | public function iSpecifyTheEmail($email = null) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @When I specify the first and last name as :fullName for billing address |
||
| 212 | */ |
||
| 213 | public function iSpecifyTheStreetAsForBillingAddress(string $fullName): void |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @When I complete the addressing step |
||
| 220 | * @When I try to complete the addressing step |
||
| 221 | */ |
||
| 222 | public function iCompleteTheAddressingStep() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @When I go back to store |
||
| 229 | */ |
||
| 230 | public function iGoBackToStore() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @When /^I proceed selecting ("[^"]+" as billing country)$/ |
||
| 237 | */ |
||
| 238 | public function iProceedSelectingBillingCountry( |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @When /^I proceed as guest "([^"]*)" with ("[^"]+" as billing country)$/ |
||
| 257 | */ |
||
| 258 | public function iProceedLoggingAsGuestWithAsBillingCountry( |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @When I specify the password as :password |
||
| 275 | */ |
||
| 276 | public function iSpecifyThePasswordAs($password) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @When I sign in |
||
| 283 | */ |
||
| 284 | public function iSignIn() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @Then I should have :countryName selected as country |
||
| 291 | */ |
||
| 292 | public function iShouldHaveSelectedAsCountry($countryName) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @Then I should have no country selected |
||
| 299 | */ |
||
| 300 | public function iShouldHaveNoCountrySelected() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @Then I should be able to log in |
||
| 307 | */ |
||
| 308 | public function iShouldBeAbleToLogIn() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @Then the login form should no longer be accessible |
||
| 315 | */ |
||
| 316 | public function theLoginFormShouldNoLongerBeAccessible() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @Then I should be notified about bad credentials |
||
| 323 | */ |
||
| 324 | public function iShouldBeNotifiedAboutBadCredentials() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @Then I should be redirected to the addressing step |
||
| 331 | * @Then I should be on the checkout addressing step |
||
| 332 | */ |
||
| 333 | public function iShouldBeRedirectedToTheAddressingStep() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @Then I should be able to go to the shipping step again |
||
| 340 | */ |
||
| 341 | public function iShouldBeAbleToGoToTheShippingStepAgain() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @Then I should not be able to specify province name manually for shipping address |
||
| 350 | */ |
||
| 351 | public function iShouldNotBeAbleToSpecifyProvinceNameManuallyForShippingAddress() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @Then I should not be able to specify province name manually for billing address |
||
| 358 | */ |
||
| 359 | public function iShouldNotBeAbleToSpecifyProvinceNameManuallyForBillingAddress() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @Then /^(address "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+") should be filled as shipping address$/ |
||
| 366 | */ |
||
| 367 | public function addressShouldBeFilledAsShippingAddress(AddressInterface $address) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @Then /^(address "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+") should be filled as billing address$/ |
||
| 374 | */ |
||
| 375 | public function addressShouldBeFilledAsBillingAddress(AddressInterface $address) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @Then /^I should(?:| also) be notified that the "([^"]+)" and the "([^"]+)" in (shipping|billing) details are required$/ |
||
| 382 | */ |
||
| 383 | public function iShouldBeNotifiedThatTheAndTheInShippingDetailsAreRequired($firstElement, $secondElement, $type) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @Then I should have only :firstCountry country available to choose from |
||
| 391 | * @Then I should have both :firstCountry and :secondCountry countries available to choose from |
||
| 392 | */ |
||
| 393 | public function shouldHaveCountriesToChooseFrom(string ...$countries): void |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @return AddressInterface |
||
| 408 | */ |
||
| 409 | private function createDefaultAddress() |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @param string $type |
||
| 426 | * @param string $element |
||
| 427 | * @param string $expectedMessage |
||
| 428 | * |
||
| 429 | * @throws \InvalidArgumentException |
||
| 430 | */ |
||
| 431 | private function assertElementValidationMessage($type, $element, $expectedMessage) |
||
| 436 | } |
||
| 437 |