Complex classes like ManagingCustomersContext 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 ManagingCustomersContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | final class ManagingCustomersContext implements Context |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var IndexPageInterface |
||
| 32 | */ |
||
| 33 | private $indexPage; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var CreatePageInterface |
||
| 37 | */ |
||
| 38 | private $createPage; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var UpdatePageInterface |
||
| 42 | */ |
||
| 43 | private $updatePage; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var ShowPageInterface |
||
| 47 | */ |
||
| 48 | private $showPage; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var IndexPageInterface |
||
| 52 | */ |
||
| 53 | private $ordersIndexPage; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var CurrentPageResolverInterface |
||
| 57 | */ |
||
| 58 | private $currentPageResolver; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param CreatePageInterface $createPage |
||
| 62 | * @param IndexPageInterface $indexPage |
||
| 63 | * @param UpdatePageInterface $updatePage |
||
| 64 | * @param ShowPageInterface $showPage |
||
| 65 | * @param IndexPageInterface $ordersIndexPage |
||
| 66 | * @param CurrentPageResolverInterface $currentPageResolver |
||
| 67 | */ |
||
| 68 | public function __construct( |
||
| 69 | CreatePageInterface $createPage, |
||
| 70 | IndexPageInterface $indexPage, |
||
| 71 | UpdatePageInterface $updatePage, |
||
| 72 | ShowPageInterface $showPage, |
||
| 73 | IndexPageInterface $ordersIndexPage, |
||
| 74 | CurrentPageResolverInterface $currentPageResolver |
||
| 75 | ) { |
||
| 76 | $this->createPage = $createPage; |
||
| 77 | $this->indexPage = $indexPage; |
||
| 78 | $this->updatePage = $updatePage; |
||
| 79 | $this->showPage = $showPage; |
||
| 80 | $this->ordersIndexPage = $ordersIndexPage; |
||
| 81 | $this->currentPageResolver = $currentPageResolver; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @Given I want to create a new customer |
||
| 86 | * @Given I want to create a new customer account |
||
| 87 | */ |
||
| 88 | public function iWantToCreateANewCustomer() |
||
| 89 | { |
||
| 90 | $this->createPage->open(); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @When /^I specify (?:their|his) first name as "([^"]*)"$/ |
||
| 95 | */ |
||
| 96 | public function iSpecifyItsFirstNameAs($name) |
||
| 97 | { |
||
| 98 | $this->createPage->specifyFirstName($name); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @When /^I specify (?:their|his) last name as "([^"]*)"$/ |
||
| 103 | */ |
||
| 104 | public function iSpecifyItsLastNameAs($name) |
||
| 105 | { |
||
| 106 | $this->createPage->specifyLastName($name); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @When I specify their email as :name |
||
| 111 | * @When I do not specify their email |
||
| 112 | */ |
||
| 113 | public function iSpecifyItsEmailAs($email = null) |
||
| 114 | { |
||
| 115 | $this->createPage->specifyEmail($email); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @When I change their email to :email |
||
| 120 | * @When I remove its email |
||
| 121 | */ |
||
| 122 | public function iChangeTheirEmailTo(string $email = null): void |
||
| 123 | { |
||
| 124 | $this->updatePage->changeEmail($email); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @When I add them |
||
| 129 | * @When I try to add them |
||
| 130 | */ |
||
| 131 | public function iAddIt() |
||
| 132 | { |
||
| 133 | $this->createPage->create(); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @Then the customer :customer should appear in the store |
||
| 138 | * @Then the customer :customer should still have this email |
||
| 139 | */ |
||
| 140 | public function theCustomerShould(CustomerInterface $customer) |
||
| 141 | { |
||
| 142 | $this->indexPage->open(); |
||
| 143 | |||
| 144 | Assert::true($this->indexPage->isSingleResourceOnPage(['email' => $customer->getEmail()])); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @When I select :gender as its gender |
||
| 149 | */ |
||
| 150 | public function iSelectGender($gender) |
||
| 151 | { |
||
| 152 | $this->createPage->chooseGender($gender); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @When I select :group as their group |
||
| 157 | */ |
||
| 158 | public function iSelectGroup($group) |
||
| 159 | { |
||
| 160 | $this->createPage->chooseGroup($group); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @When I specify its birthday as :birthday |
||
| 165 | */ |
||
| 166 | public function iSpecifyItsBirthdayAs($birthday) |
||
| 167 | { |
||
| 168 | $this->createPage->specifyBirthday($birthday); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @When /^I want to edit (this customer)$/ |
||
| 173 | */ |
||
| 174 | public function iWantToEditThisCustomer(CustomerInterface $customer) |
||
| 175 | { |
||
| 176 | $this->updatePage->open(['id' => $customer->getId()]); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @When I save my changes |
||
| 181 | * @When I try to save my changes |
||
| 182 | */ |
||
| 183 | public function iSaveMyChanges() |
||
| 184 | { |
||
| 185 | $this->updatePage->saveChanges(); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @Then /^(this customer) with name "([^"]*)" should appear in the store$/ |
||
| 190 | */ |
||
| 191 | public function theCustomerWithNameShouldAppearInTheRegistry(CustomerInterface $customer, $name) |
||
| 192 | { |
||
| 193 | $this->updatePage->open(['id' => $customer->getId()]); |
||
| 194 | |||
| 195 | Assert::same($this->updatePage->getFullName(), $name); |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @When I want to see all customers in store |
||
| 200 | */ |
||
| 201 | public function iWantToSeeAllCustomersInStore() |
||
| 202 | { |
||
| 203 | $this->indexPage->open(); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @Then /^I should see (\d+) customers in the list$/ |
||
| 208 | */ |
||
| 209 | public function iShouldSeeCustomersInTheList($amountOfCustomers) |
||
| 210 | { |
||
| 211 | Assert::same($this->indexPage->countItems(), (int) $amountOfCustomers); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @Then I should see the customer :email in the list |
||
| 216 | */ |
||
| 217 | public function iShouldSeeTheCustomerInTheList($email) |
||
| 218 | { |
||
| 219 | Assert::true($this->indexPage->isSingleResourceOnPage(['email' => $email])); |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @Then /^I should be notified that ([^"]+) is required$/ |
||
| 224 | */ |
||
| 225 | public function iShouldBeNotifiedThatFirstNameIsRequired($elementName) |
||
| 226 | { |
||
| 227 | Assert::same( |
||
| 228 | $this->createPage->getValidationMessage($elementName), |
||
| 229 | sprintf('Please enter your %s.', $elementName) |
||
| 230 | ); |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @Then /^I should be notified that ([^"]+) should be ([^"]+)$/ |
||
| 235 | */ |
||
| 236 | public function iShouldBeNotifiedThatTheElementShouldBe($elementName, $validationMessage) |
||
| 237 | { |
||
| 238 | Assert::same( |
||
| 239 | $this->updatePage->getValidationMessage($elementName), |
||
| 240 | sprintf('%s must be %s.', ucfirst($elementName), $validationMessage) |
||
| 241 | ); |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @Then the customer with email :email should not appear in the store |
||
| 246 | */ |
||
| 247 | public function theCustomerShouldNotAppearInTheStore($email) |
||
| 248 | { |
||
| 249 | $this->indexPage->open(); |
||
| 250 | |||
| 251 | Assert::false($this->indexPage->isSingleResourceOnPage(['email' => $email])); |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @When I remove its first name |
||
| 256 | */ |
||
| 257 | public function iRemoveItsFirstName() |
||
| 258 | { |
||
| 259 | $this->updatePage->changeFirstName(''); |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @Then /^(this customer) should have an empty first name$/ |
||
| 264 | * @Then the customer :customer should still have an empty first name |
||
| 265 | */ |
||
| 266 | public function theCustomerShouldStillHaveAnEmptyFirstName(CustomerInterface $customer) |
||
| 267 | { |
||
| 268 | $this->updatePage->open(['id' => $customer->getId()]); |
||
| 269 | |||
| 270 | Assert::eq($this->updatePage->getFirstName(), ''); |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @When I remove its last name |
||
| 275 | */ |
||
| 276 | public function iRemoveItsLastName() |
||
| 277 | { |
||
| 278 | $this->updatePage->changeLastName(''); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @Then /^(this customer) should have an empty last name$/ |
||
| 283 | * @Then the customer :customer should still have an empty last name |
||
| 284 | */ |
||
| 285 | public function theCustomerShouldStillHaveAnEmptyLastName(CustomerInterface $customer) |
||
| 286 | { |
||
| 287 | $this->updatePage->open(['id' => $customer->getId()]); |
||
| 288 | |||
| 289 | Assert::eq($this->updatePage->getLastName(), ''); |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @Then I should be notified that email is not valid |
||
| 294 | */ |
||
| 295 | public function iShouldBeNotifiedThatEmailIsNotValid() |
||
| 296 | { |
||
| 297 | Assert::same($this->createPage->getValidationMessage('email'), 'This email is invalid.'); |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @Then I should be notified that email must be unique |
||
| 302 | */ |
||
| 303 | public function iShouldBeNotifiedThatEmailMustBeUnique() |
||
| 304 | { |
||
| 305 | Assert::same($this->createPage->getValidationMessage('email'), 'This email is already used.'); |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @Then there should still be only one customer with email :email |
||
| 310 | */ |
||
| 311 | public function thereShouldStillBeOnlyOneCustomerWithEmail($email) |
||
| 312 | { |
||
| 313 | $this->indexPage->open(); |
||
| 314 | |||
| 315 | Assert::true($this->indexPage->isSingleResourceOnPage(['email' => $email])); |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @Given I want to enable :customer |
||
| 320 | * @Given I want to disable :customer |
||
| 321 | */ |
||
| 322 | public function iWantToChangeStatusOf(CustomerInterface $customer) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @When I enable their account |
||
| 329 | */ |
||
| 330 | public function iEnableIt() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @When I disable their account |
||
| 337 | */ |
||
| 338 | public function iDisableIt() |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @Then /^(this customer) should be enabled$/ |
||
| 345 | */ |
||
| 346 | public function thisCustomerShouldBeEnabled(CustomerInterface $customer) |
||
| 347 | { |
||
| 348 | $this->indexPage->open(); |
||
| 349 | |||
| 350 | Assert::eq($this->indexPage->getCustomerAccountStatus($customer), 'Enabled'); |
||
|
|
|||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @Then /^(this customer) should be disabled$/ |
||
| 355 | */ |
||
| 356 | public function thisCustomerShouldBeDisabled(CustomerInterface $customer) |
||
| 357 | { |
||
| 358 | $this->indexPage->open(); |
||
| 359 | |||
| 360 | Assert::eq($this->indexPage->getCustomerAccountStatus($customer), 'Disabled'); |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @When I specify their password as :password |
||
| 365 | */ |
||
| 366 | public function iSpecifyItsPasswordAs($password) |
||
| 367 | { |
||
| 368 | $this->createPage->specifyPassword($password); |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @When I choose create account option |
||
| 373 | */ |
||
| 374 | public function iChooseCreateAccountOption() |
||
| 375 | { |
||
| 376 | $this->createPage->selectCreateAccount(); |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @When I browse orders of a customer :customer |
||
| 381 | */ |
||
| 382 | public function iBrowseOrdersOfACustomer(CustomerInterface $customer) |
||
| 383 | { |
||
| 384 | $this->ordersIndexPage->open(['id' => $customer->getId()]); |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @Then the customer :customer should have an account created |
||
| 389 | * @Then /^(this customer) should have an account created$/ |
||
| 390 | */ |
||
| 391 | public function theyShouldHaveAnAccountCreated(CustomerInterface $customer) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @When I view details of the customer :customer |
||
| 401 | * @When /^I view (their) details$/ |
||
| 402 | */ |
||
| 403 | public function iViewDetailsOfTheCustomer(CustomerInterface $customer) |
||
| 404 | { |
||
| 405 | $this->showPage->open(['id' => $customer->getId()]); |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @Then his name should be :name |
||
| 410 | */ |
||
| 411 | public function hisNameShouldBe($name) |
||
| 412 | { |
||
| 413 | Assert::same($this->showPage->getCustomerName(), $name); |
||
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @Then he should be registered since :registrationDate |
||
| 418 | */ |
||
| 419 | public function hisRegistrationDateShouldBe($registrationDate) |
||
| 420 | { |
||
| 421 | Assert::eq($this->showPage->getRegistrationDate(), new \DateTime($registrationDate)); |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @Then his email should be :email |
||
| 426 | */ |
||
| 427 | public function hisEmailShouldBe($email) |
||
| 428 | { |
||
| 429 | Assert::same($this->showPage->getCustomerEmail(), $email); |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @Then his phone number should be :phoneNumber |
||
| 434 | */ |
||
| 435 | public function hisPhoneNumberShouldBe($phoneNumber) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @Then his default address should be :defaultAddress |
||
| 442 | */ |
||
| 443 | public function hisShippingAddressShouldBe($defaultAddress) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @Then I should see information about no existing account for this customer |
||
| 450 | */ |
||
| 451 | public function iShouldSeeInformationAboutNoExistingAccountForThisCustomer() |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @Then I should see that this customer is subscribed to the newsletter |
||
| 458 | */ |
||
| 459 | public function iShouldSeeThatThisCustomerIsSubscribedToTheNewsletter() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @Then I should not see information about email verification |
||
| 466 | */ |
||
| 467 | public function iShouldSeeInformationAboutEmailVerification() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @When I make them subscribed to the newsletter |
||
| 474 | */ |
||
| 475 | public function iMakeThemSubscribedToTheNewsletter() |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @When I change the password of user :customer to :newPassword |
||
| 482 | */ |
||
| 483 | public function iChangeThePasswordOfUserTo(CustomerInterface $customer, $newPassword) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * @Then this customer should be subscribed to the newsletter |
||
| 492 | */ |
||
| 493 | public function thisCustomerShouldBeSubscribedToTheNewsletter() |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @Then the province in the default address should be :provinceName |
||
| 500 | */ |
||
| 501 | public function theProvinceInTheDefaultAddressShouldBe($provinceName) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @Then this customer should have :groupName as their group |
||
| 508 | */ |
||
| 509 | public function thisCustomerShouldHaveAsTheirGroup($groupName) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @Then I should see that this customer has verified the email |
||
| 519 | */ |
||
| 520 | public function iShouldSeeThatThisCustomerHasVerifiedTheEmail() |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @Then I should see a single order in the list |
||
| 527 | */ |
||
| 528 | public function iShouldSeeASingleOrderInTheList() |
||
| 532 | |||
| 533 | /** |
||
| 534 | * @Then I should see the order with number :orderNumber in the list |
||
| 535 | */ |
||
| 536 | public function iShouldSeeASingleOrderFromCustomer($orderNumber) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @Then I should not see the order with number :orderNumber in the list |
||
| 543 | */ |
||
| 544 | public function iShouldNotSeeASingleOrderFromCustomer($orderNumber) |
||
| 548 | |||
| 549 | /** |
||
| 550 | * @When I do not specify any information |
||
| 551 | */ |
||
| 552 | public function iDoNotSpecifyAnyInformation() |
||
| 556 | |||
| 557 | /** |
||
| 558 | * @Then I should not be able to specify their password |
||
| 559 | */ |
||
| 560 | public function iShouldNotBeAbleToSpecifyItPassword() |
||
| 564 | |||
| 565 | /** |
||
| 566 | * @Then I should still be on the customer creation page |
||
| 567 | */ |
||
| 568 | public function iShouldBeOnTheCustomerCreationPage() |
||
| 572 | |||
| 573 | /** |
||
| 574 | * @Then I should be able to select create account option |
||
| 575 | */ |
||
| 576 | public function iShouldBeAbleToSelectCreateAccountOption() |
||
| 580 | |||
| 581 | /** |
||
| 582 | * @Then I should be able to specify their password |
||
| 583 | */ |
||
| 584 | public function iShouldBeAbleToSpecifyItPassword() |
||
| 588 | |||
| 589 | /** |
||
| 590 | * @Then I should not be able to select create account option |
||
| 591 | */ |
||
| 592 | public function iShouldNotBeAbleToSelectCreateAccountOption() |
||
| 596 | |||
| 597 | /** |
||
| 598 | * @When I do not choose create account option |
||
| 599 | */ |
||
| 600 | public function iDoNotChooseCreateAccountOption() |
||
| 604 | |||
| 605 | /** |
||
| 606 | * @Then I should not see create account option |
||
| 607 | */ |
||
| 608 | public function iShouldNotSeeCreateAccountOption() |
||
| 612 | |||
| 613 | /** |
||
| 614 | * @Then /^I should be notified that the password must be at least (\d+) characters long$/ |
||
| 615 | */ |
||
| 616 | public function iShouldBeNotifiedThatThePasswordMustBeAtLeastCharactersLong($amountOfCharacters) |
||
| 623 | |||
| 624 | /** |
||
| 625 | * @Then I should see the customer has not placed any orders yet |
||
| 626 | */ |
||
| 627 | public function iShouldSeeTheCustomerHasNotYetPlacedAnyOrders() |
||
| 631 | |||
| 632 | /** |
||
| 633 | * @Then /^I should see that they have placed (\d+) orders? in the "([^"]+)" channel$/ |
||
| 634 | */ |
||
| 635 | public function iShouldSeeThatTheyHavePlacedOrdersInTheChannel($ordersCount, $channelName) |
||
| 639 | |||
| 640 | /** |
||
| 641 | * @Then /^I should see that the overall total value of all their orders in the "([^"]+)" channel is "([^"]+)"$/ |
||
| 642 | */ |
||
| 643 | public function iShouldSeeThatTheOverallTotalValueOfAllTheirOrdersInTheChannelIs($channelName, $ordersValue) |
||
| 647 | |||
| 648 | /** |
||
| 649 | * @Then /^I should see that the average total value of their order in the "([^"]+)" channel is "([^"]+)"$/ |
||
| 650 | */ |
||
| 651 | public function iShouldSeeThatTheAverageTotalValueOfTheirOrderInTheChannelIs($channelName, $ordersValue) |
||
| 655 | } |
||
| 656 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: