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 | * @var IndexPageInterface |
||
56 | */ |
||
57 | private $ordersIndexPage; |
||
58 | |||
59 | /** |
||
60 | * @var CurrentPageResolverInterface |
||
61 | */ |
||
62 | private $currentPageResolver; |
||
63 | |||
64 | /** |
||
65 | * @param SharedStorageInterface $sharedStorage |
||
66 | * @param CreatePageInterface $createPage |
||
67 | * @param IndexPageInterface $indexPage |
||
68 | * @param UpdatePageInterface $updatePage |
||
69 | * @param ShowPageInterface $showPage |
||
70 | * @param IndexPageInterface $ordersIndexPage |
||
71 | * @param CurrentPageResolverInterface $currentPageResolver |
||
72 | */ |
||
73 | public function __construct( |
||
90 | |||
91 | /** |
||
92 | * @Given I want to create a new customer |
||
93 | * @Given I want to create a new customer account |
||
94 | */ |
||
95 | public function iWantToCreateANewCustomer() |
||
99 | |||
100 | /** |
||
101 | * @When /^I specify (?:their|his) first name as "([^"]*)"$/ |
||
102 | */ |
||
103 | public function iSpecifyItsFirstNameAs($name) |
||
107 | |||
108 | /** |
||
109 | * @When /^I specify (?:their|his) last name as "([^"]*)"$/ |
||
110 | */ |
||
111 | public function iSpecifyItsLastNameAs($name) |
||
115 | |||
116 | /** |
||
117 | * @When I specify their email as :name |
||
118 | * @When I do not specify their email |
||
119 | */ |
||
120 | public function iSpecifyItsEmailAs($email = null) |
||
121 | { |
||
122 | $this->createPage->specifyEmail($email); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @When I add them |
||
127 | * @When I try to add them |
||
128 | */ |
||
129 | public function iAddIt() |
||
130 | { |
||
131 | $this->createPage->create(); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @Then the customer :customer should appear in the store |
||
136 | * @Then the customer :customer should still have this email |
||
137 | */ |
||
138 | public function theCustomerShould(CustomerInterface $customer) |
||
139 | { |
||
140 | $this->indexPage->open(); |
||
141 | |||
142 | Assert::true( |
||
143 | $this->indexPage->isSingleResourceOnPage(['email' => $customer->getEmail()]), |
||
144 | sprintf('Customer with email %s should exist but it does not.', $customer->getEmail()) |
||
145 | ); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @When I select :gender as its gender |
||
150 | */ |
||
151 | public function iSelectGender($gender) |
||
152 | { |
||
153 | $this->createPage->chooseGender($gender); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @When I select :group as their group |
||
158 | */ |
||
159 | public function iSelectGroup($group) |
||
160 | { |
||
161 | $this->createPage->chooseGroup($group); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @When I specify its birthday as :birthday |
||
166 | */ |
||
167 | public function iSpecifyItsBirthdayAs($birthday) |
||
168 | { |
||
169 | $this->createPage->specifyBirthday($birthday); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @Given /^I want to edit (this customer)$/ |
||
174 | */ |
||
175 | public function iWantToEditThisCustomer(CustomerInterface $customer) |
||
179 | |||
180 | /** |
||
181 | * @When I save my changes |
||
182 | * @When I try to save my changes |
||
183 | */ |
||
184 | public function iSaveMyChanges() |
||
185 | { |
||
188 | |||
189 | /** |
||
190 | * @Then /^(this customer) with name "([^"]*)" should appear in the store$/ |
||
191 | */ |
||
192 | public function theCustomerWithNameShouldAppearInTheRegistry(CustomerInterface $customer, $name) |
||
202 | |||
203 | /** |
||
204 | * @When I want to see all customers in store |
||
205 | */ |
||
206 | public function iWantToSeeAllCustomersInStore() |
||
210 | |||
211 | /** |
||
212 | * @Then /^I should see (\d+) customers in the list$/ |
||
213 | */ |
||
214 | public function iShouldSeeCustomersInTheList($amountOfCustomers) |
||
222 | |||
223 | /** |
||
224 | * @Then I should see the customer :email in the list |
||
225 | */ |
||
226 | public function iShouldSeeTheCustomerInTheList($email) |
||
233 | |||
234 | /** |
||
235 | * @Then /^I should be notified that ([^"]+) is required$/ |
||
236 | */ |
||
237 | public function iShouldBeNotifiedThatFirstNameIsRequired($elementName) |
||
241 | |||
242 | /** |
||
243 | * @Then /^I should be notified that ([^"]+) should be ([^"]+)$/ |
||
244 | */ |
||
245 | public function iShouldBeNotifiedThatTheElementShouldBe($elementName, $validationMessage) |
||
252 | |||
253 | /** |
||
254 | * @Then the customer with email :email should not appear in the store |
||
255 | */ |
||
256 | public function theCustomerShouldNotAppearInTheStore($email) |
||
265 | |||
266 | /** |
||
267 | * @When I remove its first name |
||
268 | */ |
||
269 | public function iRemoveItsFirstName() |
||
273 | |||
274 | /** |
||
275 | * @Then /^(this customer) should have an empty first name$/ |
||
276 | * @Then the customer :customer should still have an empty first name |
||
277 | */ |
||
278 | public function theCustomerShouldStillHaveAnEmptyFirstName(CustomerInterface $customer) |
||
288 | |||
289 | /** |
||
290 | * @When I remove its last name |
||
291 | */ |
||
292 | public function iRemoveItsLastName() |
||
296 | |||
297 | /** |
||
298 | * @Then /^(this customer) should have an empty last name$/ |
||
299 | * @Then the customer :customer should still have an empty last name |
||
300 | */ |
||
301 | public function theCustomerShouldStillHaveAnEmptyLastName(CustomerInterface $customer) |
||
311 | |||
312 | /** |
||
313 | * @When I remove its email |
||
314 | */ |
||
315 | public function iRemoveItsEmail() |
||
319 | |||
320 | /** |
||
321 | * @Then I should be notified that email is not valid |
||
322 | */ |
||
323 | public function iShouldBeNotifiedThatEmailIsNotValid() |
||
327 | |||
328 | /** |
||
329 | * @Then I should be notified that email must be unique |
||
330 | */ |
||
331 | public function iShouldBeNotifiedThatEmailMustBeUnique() |
||
335 | |||
336 | /** |
||
337 | * @Then there should still be only one customer with email :email |
||
338 | */ |
||
339 | public function thereShouldStillBeOnlyOneCustomerWithEmail($email) |
||
348 | |||
349 | /** |
||
350 | * @Given I want to enable :customer |
||
351 | * @Given I want to disable :customer |
||
352 | */ |
||
353 | public function iWantToChangeStatusOf(CustomerInterface $customer) |
||
357 | |||
358 | /** |
||
359 | * @When I enable their account |
||
360 | */ |
||
361 | public function iEnableIt() |
||
365 | |||
366 | /** |
||
367 | * @When I disable their account |
||
368 | */ |
||
369 | public function iDisableIt() |
||
373 | |||
374 | /** |
||
375 | * @Then /^(this customer) should be enabled$/ |
||
376 | */ |
||
377 | public function thisCustomerShouldBeEnabled(CustomerInterface $customer) |
||
387 | |||
388 | /** |
||
389 | * @Then /^(this customer) should be disabled$/ |
||
390 | */ |
||
391 | public function thisCustomerShouldBeDisabled(CustomerInterface $customer) |
||
401 | |||
402 | /** |
||
403 | * @When I specify their password as :password |
||
404 | */ |
||
405 | public function iSpecifyItsPasswordAs($password) |
||
409 | |||
410 | /** |
||
411 | * @When I choose create account option |
||
412 | */ |
||
413 | public function iChooseCreateAccountOption() |
||
417 | |||
418 | /** |
||
419 | * @When I browse orders of a customer :customer |
||
420 | */ |
||
421 | public function iBrowseOrdersOfACustomer(CustomerInterface $customer) |
||
425 | |||
426 | /** |
||
427 | * @Then the customer :customer should have an account created |
||
428 | * @Then /^(this customer) should have an account created$/ |
||
429 | */ |
||
430 | public function theyShouldHaveAnAccountCreated(CustomerInterface $customer) |
||
437 | |||
438 | /** |
||
439 | * @When I view details of the customer :customer |
||
440 | * @When /^I view (their) details$/ |
||
441 | */ |
||
442 | public function iViewDetailsOfTheCustomer(CustomerInterface $customer) |
||
446 | |||
447 | /** |
||
448 | * @Then his name should be :name |
||
449 | */ |
||
450 | public function hisNameShouldBe($name) |
||
458 | |||
459 | /** |
||
460 | * @Given he should be registered since :registrationDate |
||
461 | */ |
||
462 | public function hisRegistrationDateShouldBe($registrationDate) |
||
470 | |||
471 | /** |
||
472 | * @Given his email should be :email |
||
473 | */ |
||
474 | public function hisEmailShouldBe($email) |
||
482 | |||
483 | /** |
||
484 | * @Then his default address should be :defaultAddress |
||
485 | */ |
||
486 | public function hisShippingAddressShouldBe($defaultAddress) |
||
494 | |||
495 | /** |
||
496 | * @Then I should see information about no existing account for this customer |
||
497 | */ |
||
498 | public function iShouldSeeInformationAboutNoExistingAccountForThisCustomer() |
||
505 | |||
506 | /** |
||
507 | * @Then I should see that this customer is subscribed to the newsletter |
||
508 | */ |
||
509 | public function iShouldSeeThatThisCustomerIsSubscribedToTheNewsletter() |
||
516 | |||
517 | /** |
||
518 | * @Then I should not see information about email verification |
||
519 | */ |
||
520 | public function iShouldSeeInformationAboutEmailVerification() |
||
527 | |||
528 | /** |
||
529 | * @When I make them subscribed to the newsletter |
||
530 | */ |
||
531 | public function iMakeThemSubscribedToTheNewsletter() |
||
535 | |||
536 | /** |
||
537 | * @When I change the password of user :customer to :newPassword |
||
538 | */ |
||
539 | public function iChangeThePasswordOfUserTo(CustomerInterface $customer, $newPassword) |
||
545 | |||
546 | /** |
||
547 | * @Then this customer should be subscribed to the newsletter |
||
548 | */ |
||
549 | public function thisCustomerShouldBeSubscribedToTheNewsletter() |
||
556 | |||
557 | /** |
||
558 | * @Then the province in the default address should be :provinceName |
||
559 | */ |
||
560 | public function theProvinceInTheDefaultAddressShouldBe($provinceName) |
||
567 | |||
568 | /** |
||
569 | * @Then this customer should have :groupName as their group |
||
570 | */ |
||
571 | public function thisCustomerShouldHaveAsTheirGroup($groupName) |
||
582 | |||
583 | /** |
||
584 | * @Then I should see that this customer has verified the email |
||
585 | */ |
||
586 | public function iShouldSeeThatThisCustomerHasVerifiedTheEmail() |
||
593 | |||
594 | /** |
||
595 | * @Then I should see a single order in the list |
||
596 | */ |
||
597 | public function iShouldSeeASingleOrderInTheList() |
||
605 | |||
606 | /** |
||
607 | * @Then I should see the order with number :orderNumber in the list |
||
608 | */ |
||
609 | public function iShouldSeeASingleOrderFromCustomer($orderNumber) |
||
616 | |||
617 | /** |
||
618 | * @Then I should not see the order with number :orderNumber in the list |
||
619 | */ |
||
620 | public function iShouldNotSeeASingleOrderFromCustomer($orderNumber) |
||
627 | |||
628 | /** |
||
629 | * @When I do not specify any information |
||
630 | */ |
||
631 | public function iDoNotSpecifyAnyInformation() |
||
635 | |||
636 | /** |
||
637 | * @Then I should not be able to specify their password |
||
638 | */ |
||
639 | public function iShouldNotBeAbleToSpecifyItPassword() |
||
646 | |||
647 | /** |
||
648 | * @Then I should still be on the customer creation page |
||
649 | */ |
||
650 | public function iShouldBeOnTheCustomerCreationPage() |
||
657 | |||
658 | /** |
||
659 | * @Then I should be able to select create account option |
||
660 | */ |
||
661 | public function iShouldBeAbleToSelectCreateAccountOption() |
||
668 | |||
669 | /** |
||
670 | * @Then I should be able to specify their password |
||
671 | */ |
||
672 | public function iShouldBeAbleToSpecifyItPassword() |
||
679 | |||
680 | /** |
||
681 | * @Then I should not be able to select create account option |
||
682 | */ |
||
683 | public function iShouldNotBeAbleToSelectCreateAccountOption() |
||
690 | |||
691 | /** |
||
692 | * @When I do not choose create account option |
||
693 | */ |
||
694 | public function iDoNotChooseCreateAccountOption() |
||
698 | |||
699 | /** |
||
700 | * @Then I should not see create account option |
||
701 | */ |
||
702 | public function iShouldNotSeeCreateAccountOption() |
||
709 | |||
710 | /** |
||
711 | * @Then /^I should be notified that the password must be at least (\d+) characters long$/ |
||
712 | */ |
||
713 | public function iShouldBeNotifiedThatThePasswordMustBeAtLeastCharactersLong($amountOfCharacters) |
||
717 | } |
||
718 |
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: