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  | 
            ||
| 27 | final class ManagingCustomersContext implements Context  | 
            ||
| 28 | { | 
            ||
| 29 | /**  | 
            ||
| 30 | * @var SharedStorageInterface  | 
            ||
| 31 | */  | 
            ||
| 32 | private $sharedStorage;  | 
            ||
| 33 | |||
| 34 | /**  | 
            ||
| 35 | * @var IndexPageInterface  | 
            ||
| 36 | */  | 
            ||
| 37 | private $indexPage;  | 
            ||
| 38 | |||
| 39 | /**  | 
            ||
| 40 | * @var CreatePageInterface  | 
            ||
| 41 | */  | 
            ||
| 42 | private $createPage;  | 
            ||
| 43 | |||
| 44 | /**  | 
            ||
| 45 | * @var UpdatePageInterface  | 
            ||
| 46 | */  | 
            ||
| 47 | private $updatePage;  | 
            ||
| 48 | |||
| 49 | /**  | 
            ||
| 50 | * @var ShowPageInterface  | 
            ||
| 51 | */  | 
            ||
| 52 | private $showPage;  | 
            ||
| 53 | |||
| 54 | /**  | 
            ||
| 55 | * @param SharedStorageInterface $sharedStorage  | 
            ||
| 56 | * @param CreatePageInterface $createPage  | 
            ||
| 57 | * @param IndexPageInterface $indexPage  | 
            ||
| 58 | * @param UpdatePageInterface $updatePage  | 
            ||
| 59 | * @param ShowPageInterface $showPage  | 
            ||
| 60 | */  | 
            ||
| 61 | public function __construct(  | 
            ||
| 74 | |||
| 75 | /**  | 
            ||
| 76 | * @Given I want to create a new customer  | 
            ||
| 77 | * @Given I want to create a new customer account  | 
            ||
| 78 | */  | 
            ||
| 79 | public function iWantToCreateANewCustomer()  | 
            ||
| 83 | |||
| 84 | /**  | 
            ||
| 85 | * @When /^I specify (?:their|his) first name as "([^"]*)"$/  | 
            ||
| 86 | */  | 
            ||
| 87 | public function iSpecifyItsFirstNameAs($name)  | 
            ||
| 91 | |||
| 92 | /**  | 
            ||
| 93 | * @When /^I specify (?:their|his) last name as "([^"]*)"$/  | 
            ||
| 94 | */  | 
            ||
| 95 | public function iSpecifyItsLastNameAs($name)  | 
            ||
| 99 | |||
| 100 | /**  | 
            ||
| 101 | * @When I specify their email as :name  | 
            ||
| 102 | */  | 
            ||
| 103 | public function iSpecifyItsEmailAs($email)  | 
            ||
| 107 | |||
| 108 | /**  | 
            ||
| 109 | * @When I add them  | 
            ||
| 110 | * @When I try to add it  | 
            ||
| 111 | */  | 
            ||
| 112 | public function iAddIt()  | 
            ||
| 116 | |||
| 117 | /**  | 
            ||
| 118 | * @Then the customer :customer should appear in the store  | 
            ||
| 119 | * @Then the customer :customer should still have this email  | 
            ||
| 120 | */  | 
            ||
| 121 | public function theCustomerShould(CustomerInterface $customer)  | 
            ||
| 130 | |||
| 131 | /**  | 
            ||
| 132 | * @When I select :gender as its gender  | 
            ||
| 133 | */  | 
            ||
| 134 | public function iSelectGender($gender)  | 
            ||
| 138 | |||
| 139 | /**  | 
            ||
| 140 | * @When I select :group as their group  | 
            ||
| 141 | */  | 
            ||
| 142 | public function iSelectGroup($group)  | 
            ||
| 146 | |||
| 147 | /**  | 
            ||
| 148 | * @When I specify its birthday as :birthday  | 
            ||
| 149 | */  | 
            ||
| 150 | public function iSpecifyItsBirthdayAs($birthday)  | 
            ||
| 154 | |||
| 155 | /**  | 
            ||
| 156 | * @Given /^I want to edit (this customer)$/  | 
            ||
| 157 | * @Given I want to edit the customer :customer  | 
            ||
| 158 | */  | 
            ||
| 159 | public function iWantToEditThisCustomer(CustomerInterface $customer)  | 
            ||
| 163 | |||
| 164 | /**  | 
            ||
| 165 | * @Given I want to change my password  | 
            ||
| 166 | */  | 
            ||
| 167 | public function iWantToChangeMyPassword()  | 
            ||
| 173 | |||
| 174 | /**  | 
            ||
| 175 | * @When I save my changes  | 
            ||
| 176 | * @When I try to save my changes  | 
            ||
| 177 | */  | 
            ||
| 178 | public function iSaveMyChanges()  | 
            ||
| 182 | |||
| 183 | /**  | 
            ||
| 184 | * @Then /^(this customer) with name "([^"]*)" should appear in the store$/  | 
            ||
| 185 | */  | 
            ||
| 186 | public function theCustomerWithNameShouldAppearInTheRegistry(CustomerInterface $customer, $name)  | 
            ||
| 196 | |||
| 197 | /**  | 
            ||
| 198 | * @When I want to see all customers in store  | 
            ||
| 199 | */  | 
            ||
| 200 | public function iWantToSeeAllCustomersInStore()  | 
            ||
| 204 | |||
| 205 | /**  | 
            ||
| 206 | * @Then /^I should see (\d+) customers in the list$/  | 
            ||
| 207 | */  | 
            ||
| 208 | public function iShouldSeeCustomersInTheList($amountOfCustomers)  | 
            ||
| 216 | |||
| 217 | /**  | 
            ||
| 218 | * @Then I should see the customer :email in the list  | 
            ||
| 219 | */  | 
            ||
| 220 | public function iShouldSeeTheCustomerInTheList($email)  | 
            ||
| 227 | |||
| 228 | /**  | 
            ||
| 229 | * @Then /^I should be notified that ([^"]+) is required$/  | 
            ||
| 230 | */  | 
            ||
| 231 | public function iShouldBeNotifiedThatFirstNameIsRequired($elementName)  | 
            ||
| 235 | |||
| 236 | /**  | 
            ||
| 237 | * @Then /^I should be notified that ([^"]+) should be ([^"]+)$/  | 
            ||
| 238 | */  | 
            ||
| 239 | public function iShouldBeNotifiedThatTheElementShouldBe($elementName, $validationMessage)  | 
            ||
| 246 | |||
| 247 | /**  | 
            ||
| 248 | * @Then the customer with email :email should not appear in the store  | 
            ||
| 249 | */  | 
            ||
| 250 | public function theCustomerShouldNotAppearInTheStore($email)  | 
            ||
| 259 | |||
| 260 | /**  | 
            ||
| 261 | * @When I remove its first name  | 
            ||
| 262 | */  | 
            ||
| 263 | public function iRemoveItsFirstName()  | 
            ||
| 267 | |||
| 268 | /**  | 
            ||
| 269 | * @Then /^(this customer) should have an empty first name$/  | 
            ||
| 270 | * @Then the customer :customer should still have an empty first name  | 
            ||
| 271 | */  | 
            ||
| 272 | public function theCustomerShouldStillHaveAnEmptyFirstName(CustomerInterface $customer)  | 
            ||
| 282 | |||
| 283 | /**  | 
            ||
| 284 | * @When I remove its last name  | 
            ||
| 285 | */  | 
            ||
| 286 | public function iRemoveItsLastName()  | 
            ||
| 290 | |||
| 291 | /**  | 
            ||
| 292 | * @Then /^(this customer) should have an empty last name$/  | 
            ||
| 293 | * @Then the customer :customer should still have an empty last name  | 
            ||
| 294 | */  | 
            ||
| 295 | public function theCustomerShouldStillHaveAnEmptyLastName(CustomerInterface $customer)  | 
            ||
| 305 | |||
| 306 | /**  | 
            ||
| 307 | * @When I remove its email  | 
            ||
| 308 | */  | 
            ||
| 309 | public function iRemoveItsEmail()  | 
            ||
| 313 | |||
| 314 | /**  | 
            ||
| 315 | * @Then I should be notified that email is not valid  | 
            ||
| 316 | */  | 
            ||
| 317 | public function iShouldBeNotifiedThatEmailIsNotValid()  | 
            ||
| 321 | |||
| 322 | /**  | 
            ||
| 323 | * @Then I should be notified that email must be unique  | 
            ||
| 324 | */  | 
            ||
| 325 | public function iShouldBeNotifiedThatEmailMustBeUnique()  | 
            ||
| 329 | |||
| 330 | /**  | 
            ||
| 331 | * @Then there should still be only one customer with email :email  | 
            ||
| 332 | */  | 
            ||
| 333 | public function thereShouldStillBeOnlyOneCustomerWithEmail($email)  | 
            ||
| 342 | |||
| 343 | /**  | 
            ||
| 344 | * @Given I want to enable :customer  | 
            ||
| 345 | * @Given I want to disable :customer  | 
            ||
| 346 | */  | 
            ||
| 347 | public function iWantToChangeStatusOf(CustomerInterface $customer)  | 
            ||
| 351 | |||
| 352 | /**  | 
            ||
| 353 | * @When I enable their account  | 
            ||
| 354 | */  | 
            ||
| 355 | public function iEnableIt()  | 
            ||
| 359 | |||
| 360 | /**  | 
            ||
| 361 | * @When I disable their account  | 
            ||
| 362 | */  | 
            ||
| 363 | public function iDisableIt()  | 
            ||
| 367 | |||
| 368 | /**  | 
            ||
| 369 | * @Then /^(this customer) should be enabled$/  | 
            ||
| 370 | */  | 
            ||
| 371 | public function thisCustomerShouldBeEnabled(CustomerInterface $customer)  | 
            ||
| 381 | |||
| 382 | /**  | 
            ||
| 383 | * @Then /^(this customer) should be disabled$/  | 
            ||
| 384 | */  | 
            ||
| 385 | public function thisCustomerShouldBeDisabled(CustomerInterface $customer)  | 
            ||
| 395 | |||
| 396 | /**  | 
            ||
| 397 | * @When I specify its password as :password  | 
            ||
| 398 | */  | 
            ||
| 399 | public function iSpecifyItsPasswordAs($password)  | 
            ||
| 403 | |||
| 404 | /**  | 
            ||
| 405 | * @When I change my password to :password  | 
            ||
| 406 | */  | 
            ||
| 407 | public function iSpecifyMyPasswordAs($password)  | 
            ||
| 411 | |||
| 412 | /**  | 
            ||
| 413 | * @When I choose create account option  | 
            ||
| 414 | */  | 
            ||
| 415 | public function iChooseCreateAccountOption()  | 
            ||
| 419 | |||
| 420 | /**  | 
            ||
| 421 | * @Then the customer :customer should have an account created  | 
            ||
| 422 | * @Then /^(this customer) should have an account created$/  | 
            ||
| 423 | */  | 
            ||
| 424 | public function theyShouldHaveAnAccountCreated(CustomerInterface $customer)  | 
            ||
| 431 | |||
| 432 | /**  | 
            ||
| 433 | * @When I view details of the customer :customer  | 
            ||
| 434 | */  | 
            ||
| 435 | public function iViewDetailsOfTheCustomer(CustomerInterface $customer)  | 
            ||
| 439 | |||
| 440 | /**  | 
            ||
| 441 | * @Then his name should be :name  | 
            ||
| 442 | */  | 
            ||
| 443 | public function hisNameShouldBe($name)  | 
            ||
| 451 | |||
| 452 | /**  | 
            ||
| 453 | * @Given he should be registered since :registrationDate  | 
            ||
| 454 | */  | 
            ||
| 455 | public function hisRegistrationDateShouldBe($registrationDate)  | 
            ||
| 463 | |||
| 464 | /**  | 
            ||
| 465 | * @Given his email should be :email  | 
            ||
| 466 | */  | 
            ||
| 467 | public function hisEmailShouldBe($email)  | 
            ||
| 475 | |||
| 476 | /**  | 
            ||
| 477 | * @Then his default address should be :defaultAddress  | 
            ||
| 478 | */  | 
            ||
| 479 | public function hisShippingAddressShouldBe($defaultAddress)  | 
            ||
| 487 | |||
| 488 | /**  | 
            ||
| 489 | * @Then I should see information about no existing account for this customer  | 
            ||
| 490 | */  | 
            ||
| 491 | public function iShouldSeeInformationAboutNoExistingAccountForThisCustomer()  | 
            ||
| 498 | |||
| 499 | /**  | 
            ||
| 500 | * @Then I should see that this customer is subscribed to the newsletter  | 
            ||
| 501 | */  | 
            ||
| 502 | public function iShouldSeeThatThisCustomerIsSubscribedToTheNewsletter()  | 
            ||
| 509 | |||
| 510 | /**  | 
            ||
| 511 | * @When I make them subscribed to the newsletter  | 
            ||
| 512 | */  | 
            ||
| 513 | public function iMakeThemSubscribedToTheNewsletter()  | 
            ||
| 517 | |||
| 518 | /**  | 
            ||
| 519 | * @Then this customer should be subscribed to the newsletter  | 
            ||
| 520 | */  | 
            ||
| 521 | public function thisCustomerShouldBeSubscribedToTheNewsletter()  | 
            ||
| 528 | |||
| 529 | /**  | 
            ||
| 530 | * @Then the province in the default address should be :provinceName  | 
            ||
| 531 | */  | 
            ||
| 532 | public function theProvinceInTheDefaultAddressShouldBe($provinceName)  | 
            ||
| 539 | |||
| 540 | /**  | 
            ||
| 541 | * @Then /^(this customer) should have "([^"]+)" as their group$/  | 
            ||
| 542 | */  | 
            ||
| 543 | public function thisCustomerShouldHaveAsTheirGroup(CustomerInterface $customer, $groupName)  | 
            ||
| 553 | }  | 
            ||
| 554 | 
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: