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 SharedStorageInterface |
||
32 | */ |
||
33 | private $sharedStorage; |
||
34 | |||
35 | /** |
||
36 | * @var IndexPageInterface |
||
37 | */ |
||
38 | private $indexPage; |
||
39 | |||
40 | /** |
||
41 | * @var CreatePageInterface |
||
42 | */ |
||
43 | private $createPage; |
||
44 | |||
45 | /** |
||
46 | * @var UpdatePageInterface |
||
47 | */ |
||
48 | private $updatePage; |
||
49 | |||
50 | /** |
||
51 | * @var ShowPageInterface |
||
52 | */ |
||
53 | private $showPage; |
||
54 | |||
55 | /** |
||
56 | * @var CurrentPageResolverInterface |
||
57 | */ |
||
58 | private $currentPageResolver; |
||
59 | |||
60 | /** |
||
61 | * @param SharedStorageInterface $sharedStorage |
||
62 | * @param CreatePageInterface $createPage |
||
63 | * @param IndexPageInterface $indexPage |
||
64 | * @param UpdatePageInterface $updatePage |
||
65 | * @param ShowPageInterface $showPage |
||
66 | * @param CurrentPageResolverInterface $currentPageResolver |
||
67 | */ |
||
68 | public function __construct( |
||
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() |
||
92 | |||
93 | /** |
||
94 | * @When /^I specify (?:their|his) first name as "([^"]*)"$/ |
||
95 | */ |
||
96 | public function iSpecifyItsFirstNameAs($name) |
||
100 | |||
101 | /** |
||
102 | * @When /^I specify (?:their|his) last name as "([^"]*)"$/ |
||
103 | */ |
||
104 | public function iSpecifyItsLastNameAs($name) |
||
108 | |||
109 | /** |
||
110 | * @When I specify their email as :name |
||
111 | */ |
||
112 | public function iSpecifyItsEmailAs($email) |
||
116 | |||
117 | /** |
||
118 | * @When I add them |
||
119 | * @When I try to add it |
||
120 | */ |
||
121 | public function iAddIt() |
||
125 | |||
126 | /** |
||
127 | * @Then the customer :customer should appear in the store |
||
128 | * @Then the customer :customer should still have this email |
||
129 | */ |
||
130 | public function theCustomerShould(CustomerInterface $customer) |
||
139 | |||
140 | /** |
||
141 | * @When I select :gender as its gender |
||
142 | */ |
||
143 | public function iSelectGender($gender) |
||
147 | |||
148 | /** |
||
149 | * @When I select :group as their group |
||
150 | */ |
||
151 | public function iSelectGroup($group) |
||
155 | |||
156 | /** |
||
157 | * @When I specify its birthday as :birthday |
||
158 | */ |
||
159 | public function iSpecifyItsBirthdayAs($birthday) |
||
163 | |||
164 | /** |
||
165 | * @Given /^I want to edit (this customer)$/ |
||
166 | * @Given I want to edit the customer :customer |
||
167 | */ |
||
168 | public function iWantToEditThisCustomer(CustomerInterface $customer) |
||
172 | |||
173 | /** |
||
174 | * @Given I want to change my password |
||
175 | */ |
||
176 | public function iWantToChangeMyPassword() |
||
182 | |||
183 | /** |
||
184 | * @When I save my changes |
||
185 | * @When I try to save my changes |
||
186 | */ |
||
187 | public function iSaveMyChanges() |
||
191 | |||
192 | /** |
||
193 | * @Then /^(this customer) with name "([^"]*)" should appear in the store$/ |
||
194 | */ |
||
195 | public function theCustomerWithNameShouldAppearInTheRegistry(CustomerInterface $customer, $name) |
||
205 | |||
206 | /** |
||
207 | * @When I want to see all customers in store |
||
208 | */ |
||
209 | public function iWantToSeeAllCustomersInStore() |
||
213 | |||
214 | /** |
||
215 | * @Then /^I should see (\d+) customers in the list$/ |
||
216 | */ |
||
217 | public function iShouldSeeCustomersInTheList($amountOfCustomers) |
||
225 | |||
226 | /** |
||
227 | * @Then I should see the customer :email in the list |
||
228 | */ |
||
229 | public function iShouldSeeTheCustomerInTheList($email) |
||
236 | |||
237 | /** |
||
238 | * @Then /^I should be notified that ([^"]+) is required$/ |
||
239 | */ |
||
240 | public function iShouldBeNotifiedThatFirstNameIsRequired($elementName) |
||
244 | |||
245 | /** |
||
246 | * @Then /^I should be notified that ([^"]+) should be ([^"]+)$/ |
||
247 | */ |
||
248 | public function iShouldBeNotifiedThatTheElementShouldBe($elementName, $validationMessage) |
||
255 | |||
256 | /** |
||
257 | * @Then the customer with email :email should not appear in the store |
||
258 | */ |
||
259 | public function theCustomerShouldNotAppearInTheStore($email) |
||
268 | |||
269 | /** |
||
270 | * @When I remove its first name |
||
271 | */ |
||
272 | public function iRemoveItsFirstName() |
||
276 | |||
277 | /** |
||
278 | * @Then /^(this customer) should have an empty first name$/ |
||
279 | * @Then the customer :customer should still have an empty first name |
||
280 | */ |
||
281 | public function theCustomerShouldStillHaveAnEmptyFirstName(CustomerInterface $customer) |
||
291 | |||
292 | /** |
||
293 | * @When I remove its last name |
||
294 | */ |
||
295 | public function iRemoveItsLastName() |
||
299 | |||
300 | /** |
||
301 | * @Then /^(this customer) should have an empty last name$/ |
||
302 | * @Then the customer :customer should still have an empty last name |
||
303 | */ |
||
304 | public function theCustomerShouldStillHaveAnEmptyLastName(CustomerInterface $customer) |
||
314 | |||
315 | /** |
||
316 | * @When I remove its email |
||
317 | */ |
||
318 | public function iRemoveItsEmail() |
||
322 | |||
323 | /** |
||
324 | * @Then I should be notified that email is not valid |
||
325 | */ |
||
326 | public function iShouldBeNotifiedThatEmailIsNotValid() |
||
330 | |||
331 | /** |
||
332 | * @Then I should be notified that email must be unique |
||
333 | */ |
||
334 | public function iShouldBeNotifiedThatEmailMustBeUnique() |
||
338 | |||
339 | /** |
||
340 | * @Then there should still be only one customer with email :email |
||
341 | */ |
||
342 | public function thereShouldStillBeOnlyOneCustomerWithEmail($email) |
||
351 | |||
352 | /** |
||
353 | * @Given I want to enable :customer |
||
354 | * @Given I want to disable :customer |
||
355 | */ |
||
356 | public function iWantToChangeStatusOf(CustomerInterface $customer) |
||
360 | |||
361 | /** |
||
362 | * @When I enable their account |
||
363 | */ |
||
364 | public function iEnableIt() |
||
368 | |||
369 | /** |
||
370 | * @When I disable their account |
||
371 | */ |
||
372 | public function iDisableIt() |
||
376 | |||
377 | /** |
||
378 | * @Then /^(this customer) should be enabled$/ |
||
379 | */ |
||
380 | public function thisCustomerShouldBeEnabled(CustomerInterface $customer) |
||
390 | |||
391 | /** |
||
392 | * @Then /^(this customer) should be disabled$/ |
||
393 | */ |
||
394 | public function thisCustomerShouldBeDisabled(CustomerInterface $customer) |
||
404 | |||
405 | /** |
||
406 | * @When I specify its password as :password |
||
407 | */ |
||
408 | public function iSpecifyItsPasswordAs($password) |
||
412 | |||
413 | /** |
||
414 | * @When I change my password to :password |
||
415 | */ |
||
416 | public function iSpecifyMyPasswordAs($password) |
||
420 | |||
421 | /** |
||
422 | * @When I choose create account option |
||
423 | */ |
||
424 | public function iChooseCreateAccountOption() |
||
428 | |||
429 | /** |
||
430 | * @Then the customer :customer should have an account created |
||
431 | * @Then /^(this customer) should have an account created$/ |
||
432 | */ |
||
433 | public function theyShouldHaveAnAccountCreated(CustomerInterface $customer) |
||
440 | |||
441 | /** |
||
442 | * @When I view details of the customer :customer |
||
443 | */ |
||
444 | public function iViewDetailsOfTheCustomer(CustomerInterface $customer) |
||
448 | |||
449 | /** |
||
450 | * @Then his name should be :name |
||
451 | */ |
||
452 | public function hisNameShouldBe($name) |
||
460 | |||
461 | /** |
||
462 | * @Given he should be registered since :registrationDate |
||
463 | */ |
||
464 | public function hisRegistrationDateShouldBe($registrationDate) |
||
472 | |||
473 | /** |
||
474 | * @Given his email should be :email |
||
475 | */ |
||
476 | public function hisEmailShouldBe($email) |
||
484 | |||
485 | /** |
||
486 | * @Then his default address should be :defaultAddress |
||
487 | */ |
||
488 | public function hisShippingAddressShouldBe($defaultAddress) |
||
496 | |||
497 | /** |
||
498 | * @Then I should see information about no existing account for this customer |
||
499 | */ |
||
500 | public function iShouldSeeInformationAboutNoExistingAccountForThisCustomer() |
||
507 | |||
508 | /** |
||
509 | * @Then I should see that this customer is subscribed to the newsletter |
||
510 | */ |
||
511 | public function iShouldSeeThatThisCustomerIsSubscribedToTheNewsletter() |
||
518 | |||
519 | /** |
||
520 | * @Then I should not see information about email verification |
||
521 | */ |
||
522 | public function iShouldSeeInformationAboutEmailVerification() |
||
529 | |||
530 | /** |
||
531 | * @When I make them subscribed to the newsletter |
||
532 | */ |
||
533 | public function iMakeThemSubscribedToTheNewsletter() |
||
537 | |||
538 | /** |
||
539 | * @When I change the password of user :customer to :newPassword |
||
540 | */ |
||
541 | public function iChangeThePasswordOfUserTo(CustomerInterface $customer, $newPassword) |
||
547 | |||
548 | /** |
||
549 | * @Then this customer should be subscribed to the newsletter |
||
550 | */ |
||
551 | public function thisCustomerShouldBeSubscribedToTheNewsletter() |
||
558 | |||
559 | /** |
||
560 | * @Then the province in the default address should be :provinceName |
||
561 | */ |
||
562 | public function theProvinceInTheDefaultAddressShouldBe($provinceName) |
||
569 | |||
570 | /** |
||
571 | * @Then this customer should have :groupName as their group |
||
572 | */ |
||
573 | public function thisCustomerShouldHaveAsTheirGroup($groupName) |
||
584 | |||
585 | /** |
||
586 | * @Then I should see that this customer has verified the email |
||
587 | */ |
||
588 | public function iShouldSeeThatThisCustomerHasVerifiedTheEmail() |
||
595 | } |
||
596 |
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: