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( |
||
| 44 | SharedStorageInterface $sharedStorage, |
||
| 45 | AddressPageInterface $addressPage, |
||
| 46 | FactoryInterface $addressFactory, |
||
| 47 | AddressComparatorInterface $addressComparator, |
||
| 48 | SelectShippingPageInterface $selectShippingPage |
||
| 49 | ) { |
||
| 50 | $this->sharedStorage = $sharedStorage; |
||
| 51 | $this->addressPage = $addressPage; |
||
| 52 | $this->addressFactory = $addressFactory; |
||
| 53 | $this->addressComparator = $addressComparator; |
||
| 54 | $this->selectShippingPage = $selectShippingPage; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @Given I am at the checkout addressing step |
||
| 59 | * @When I go back to addressing step of the checkout |
||
| 60 | */ |
||
| 61 | public function iAmAtTheCheckoutAddressingStep() |
||
| 62 | { |
||
| 63 | $this->addressPage->open(); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @Given /^I have completed addressing step with email "([^"]+)" and ("[^"]+" based billing address)$/ |
||
| 68 | * @When /^I complete addressing step with email "([^"]+)" and ("[^"]+" based billing address)$/ |
||
| 69 | */ |
||
| 70 | public function iCompleteAddressingStepWithEmail(string $email, AddressInterface $address): void |
||
| 71 | { |
||
| 72 | $this->addressPage->open(); |
||
| 73 | $this->iSpecifyTheEmail($email); |
||
| 74 | $this->iSpecifyTheBillingAddressAs($address); |
||
| 75 | $this->iCompleteTheAddressingStep(); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @When /^I complete addressing step with ("[^"]+" based billing address)$/ |
||
| 80 | */ |
||
| 81 | public function iCompleteAddressingStepWithBasedBillingAddress(AddressInterface $address): void |
||
| 82 | { |
||
| 83 | $this->addressPage->open(); |
||
| 84 | $this->iSpecifyTheBillingAddressAs($address); |
||
| 85 | $this->iCompleteTheAddressingStep(); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @When I specify the province name manually as :provinceName for shipping address |
||
| 90 | */ |
||
| 91 | public function iSpecifyTheProvinceNameManuallyAsForShippingAddress($provinceName) |
||
| 92 | { |
||
| 93 | $this->addressPage->specifyShippingAddressProvince($provinceName); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @When I specify the province name manually as :provinceName for billing address |
||
| 98 | */ |
||
| 99 | public function iSpecifyTheProvinceNameManuallyAsForBillingAddress($provinceName) |
||
| 100 | { |
||
| 101 | $this->addressPage->specifyBillingAddressProvince($provinceName); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @When I try to open checkout addressing page |
||
| 106 | */ |
||
| 107 | public function iTryToOpenCheckoutAddressingPage() |
||
| 108 | { |
||
| 109 | $this->addressPage->tryToOpen(); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @When /^I choose ("[^"]+" street) for shipping address$/ |
||
| 114 | */ |
||
| 115 | public function iChooseForShippingAddress(AddressInterface $address) |
||
| 116 | { |
||
| 117 | $this->addressPage->chooseDifferentShippingAddress(); |
||
| 118 | $this->addressPage->selectShippingAddressFromAddressBook($address); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @When /^I choose ("[^"]+" street) for billing address$/ |
||
| 123 | */ |
||
| 124 | public function iChooseForBillingAddress(AddressInterface $address) |
||
| 125 | { |
||
| 126 | $this->addressPage->selectBillingAddressFromAddressBook($address); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @When /^I specify the shipping (address as "[^"]+", "[^"]+", "[^"]+", "[^"]+" for "[^"]+")$/ |
||
| 131 | * @When /^I specify the shipping (address for "[^"]+" from "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+")$/ |
||
| 132 | * @When /^I (do not specify any shipping address) information$/ |
||
| 133 | * @When /^I change the shipping (address to "[^"]+", "[^"]+", "[^"]+", "[^"]+" for "[^"]+")$/ |
||
| 134 | */ |
||
| 135 | public function iSpecifyTheShippingAddressAs(AddressInterface $address) |
||
| 136 | { |
||
| 137 | $this->addressPage->chooseDifferentShippingAddress(); |
||
| 138 | |||
| 139 | $key = sprintf( |
||
| 140 | 'shipping_address_%s_%s', |
||
| 141 | strtolower((string) $address->getFirstName()), |
||
| 142 | strtolower((string) $address->getLastName()) |
||
| 143 | ); |
||
| 144 | $this->sharedStorage->set($key, $address); |
||
| 145 | |||
| 146 | $this->addressPage->specifyShippingAddress($address); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @When I specify shipping country province as :province |
||
| 151 | */ |
||
| 152 | public function iSpecifyShippingCountryProvinceAs($province) |
||
| 153 | { |
||
| 154 | $this->addressPage->selectShippingAddressProvince($province); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @When I specify billing country province as :province |
||
| 159 | */ |
||
| 160 | public function iSpecifyBillingCountryProvinceAs($province) |
||
| 161 | { |
||
| 162 | $this->addressPage->selectBillingAddressProvince($province); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @When /^I specify the billing (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/ |
||
| 167 | * @When /^I specify the billing (address for "([^"]+)" from "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)")$/ |
||
| 168 | * @When /^I (do not specify any billing address) information$/ |
||
| 169 | */ |
||
| 170 | public function iSpecifyTheBillingAddressAs(AddressInterface $address) |
||
| 171 | { |
||
| 172 | $key = sprintf( |
||
| 173 | 'billing_address_%s_%s', |
||
| 174 | strtolower((string) $address->getFirstName()), |
||
| 175 | strtolower((string) $address->getLastName()) |
||
| 176 | ); |
||
| 177 | $this->sharedStorage->set($key, $address); |
||
| 178 | |||
| 179 | $this->addressPage->specifyBillingAddress($address); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @When /^I specified the billing (address as "[^"]+", "[^"]+", "[^"]+", "[^"]+" for "[^"]+")$/ |
||
| 184 | */ |
||
| 185 | public function iSpecifiedTheBillingAddress(AddressInterface $address = null) |
||
| 186 | { |
||
| 187 | if (null === $address) { |
||
| 188 | $address = $this->createDefaultAddress(); |
||
| 189 | } |
||
| 190 | |||
| 191 | $this->addressPage->open(); |
||
| 192 | $this->iSpecifyTheBillingAddressAs($address); |
||
| 193 | |||
| 194 | $key = sprintf('shipping_address_%s_%s', strtolower((string) $address->getFirstName()), strtolower((string) $address->getLastName())); |
||
| 195 | $this->sharedStorage->set($key, $address); |
||
| 196 | |||
| 197 | $this->iCompleteTheAddressingStep(); |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @When I specify the email as :email |
||
| 202 | * @When I do not specify the email |
||
| 203 | */ |
||
| 204 | public function iSpecifyTheEmail($email = null) |
||
| 205 | { |
||
| 206 | $this->addressPage->specifyEmail($email); |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @When I specify the first and last name as :fullName for billing address |
||
| 211 | */ |
||
| 212 | public function iSpecifyTheStreetAsForBillingAddress(string $fullName): void |
||
| 213 | { |
||
| 214 | $this->addressPage->specifyBillingAddressFullName($fullName); |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @When I complete the addressing step |
||
| 219 | * @When I try to complete the addressing step |
||
| 220 | */ |
||
| 221 | public function iCompleteTheAddressingStep() |
||
| 222 | { |
||
| 223 | $this->addressPage->nextStep(); |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @When I go back to store |
||
| 228 | */ |
||
| 229 | public function iGoBackToStore() |
||
| 230 | { |
||
| 231 | $this->addressPage->backToStore(); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @When /^I proceed selecting ("[^"]+" as billing country)$/ |
||
| 236 | */ |
||
| 237 | public function iProceedSelectingBillingCountry( |
||
| 238 | CountryInterface $shippingCountry = null, |
||
| 239 | string $localeCode = 'en_US', |
||
| 240 | ?string $email = null |
||
| 241 | ) { |
||
| 242 | $this->addressPage->open(['_locale' => $localeCode]); |
||
| 243 | $shippingAddress = $this->createDefaultAddress(); |
||
| 244 | if (null !== $shippingCountry) { |
||
| 245 | $shippingAddress->setCountryCode($shippingCountry->getCode()); |
||
| 246 | } |
||
| 247 | if (null !== $email) { |
||
| 248 | $this->addressPage->specifyEmail($email); |
||
| 249 | } |
||
| 250 | $this->addressPage->specifyBillingAddress($shippingAddress); |
||
| 251 | $this->addressPage->nextStep(); |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @When /^I proceed as guest "([^"]*)" with ("[^"]+" as billing country)$/ |
||
| 256 | */ |
||
| 257 | public function iProceedLoggingAsGuestWithAsBillingCountry( |
||
| 258 | string $email, |
||
| 259 | CountryInterface $shippingCountry = null |
||
| 260 | ): void { |
||
| 261 | $this->addressPage->open(); |
||
| 262 | $this->addressPage->specifyEmail($email); |
||
| 263 | $shippingAddress = $this->createDefaultAddress(); |
||
| 264 | if (null !== $shippingCountry) { |
||
| 265 | $shippingAddress->setCountryCode($shippingCountry->getCode()); |
||
| 266 | } |
||
| 267 | |||
| 268 | $this->addressPage->specifyBillingAddress($shippingAddress); |
||
| 269 | $this->addressPage->nextStep(); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @When I specify the password as :password |
||
| 274 | */ |
||
| 275 | public function iSpecifyThePasswordAs($password) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @When I sign in |
||
| 282 | */ |
||
| 283 | public function iSignIn() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @Then I should have :countryName selected as country |
||
| 290 | */ |
||
| 291 | public function iShouldHaveSelectedAsCountry($countryName) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @Then I should have no country selected |
||
| 298 | */ |
||
| 299 | public function iShouldHaveNoCountrySelected() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @Then I should be able to log in |
||
| 306 | */ |
||
| 307 | public function iShouldBeAbleToLogIn() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @Then the login form should no longer be accessible |
||
| 314 | */ |
||
| 315 | public function theLoginFormShouldNoLongerBeAccessible() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @Then I should be notified about bad credentials |
||
| 322 | */ |
||
| 323 | public function iShouldBeNotifiedAboutBadCredentials() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @Then I should be redirected to the addressing step |
||
| 330 | * @Then I should be on the checkout addressing step |
||
| 331 | */ |
||
| 332 | public function iShouldBeRedirectedToTheAddressingStep() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @Then I should be able to go to the shipping step again |
||
| 339 | */ |
||
| 340 | public function iShouldBeAbleToGoToTheShippingStepAgain() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @Then I should not be able to specify province name manually for shipping address |
||
| 349 | */ |
||
| 350 | public function iShouldNotBeAbleToSpecifyProvinceNameManuallyForShippingAddress() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @Then I should not be able to specify province name manually for billing address |
||
| 357 | */ |
||
| 358 | public function iShouldNotBeAbleToSpecifyProvinceNameManuallyForBillingAddress() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @Then /^(address "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+") should be filled as shipping address$/ |
||
| 365 | */ |
||
| 366 | public function addressShouldBeFilledAsShippingAddress(AddressInterface $address) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @Then /^(address "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+", "[^"]+") should be filled as billing address$/ |
||
| 373 | */ |
||
| 374 | public function addressShouldBeFilledAsBillingAddress(AddressInterface $address) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @Then /^I should(?:| also) be notified that the "([^"]+)" and the "([^"]+)" in (shipping|billing) details are required$/ |
||
| 381 | */ |
||
| 382 | public function iShouldBeNotifiedThatTheAndTheInShippingDetailsAreRequired($firstElement, $secondElement, $type) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @return AddressInterface |
||
| 390 | */ |
||
| 391 | private function createDefaultAddress() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param string $type |
||
| 408 | * @param string $element |
||
| 409 | * @param string $expectedMessage |
||
| 410 | * |
||
| 411 | * @throws \InvalidArgumentException |
||
| 412 | */ |
||
| 413 | private function assertElementValidationMessage($type, $element, $expectedMessage) |
||
| 418 | } |
||
| 419 |