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 | */ |
||
119 | public function iSpecifyItsEmailAs($email) |
||
123 | |||
124 | /** |
||
125 | * @When I add them |
||
126 | * @When I try to add it |
||
127 | */ |
||
128 | public function iAddIt() |
||
132 | |||
133 | /** |
||
134 | * @Then the customer :customer should appear in the store |
||
135 | * @Then the customer :customer should still have this email |
||
136 | */ |
||
137 | public function theCustomerShould(CustomerInterface $customer) |
||
146 | |||
147 | /** |
||
148 | * @When I select :gender as its gender |
||
149 | */ |
||
150 | public function iSelectGender($gender) |
||
154 | |||
155 | /** |
||
156 | * @When I select :group as their group |
||
157 | */ |
||
158 | public function iSelectGroup($group) |
||
162 | |||
163 | /** |
||
164 | * @When I specify its birthday as :birthday |
||
165 | */ |
||
166 | public function iSpecifyItsBirthdayAs($birthday) |
||
170 | |||
171 | /** |
||
172 | * @Given /^I want to edit (this customer)$/ |
||
173 | * @Given I want to edit the customer :customer |
||
174 | */ |
||
175 | public function iWantToEditThisCustomer(CustomerInterface $customer) |
||
179 | |||
180 | /** |
||
181 | * @Given I want to change my password |
||
182 | */ |
||
183 | public function iWantToChangeMyPassword() |
||
189 | |||
190 | /** |
||
191 | * @When I save my changes |
||
192 | * @When I try to save my changes |
||
193 | */ |
||
194 | public function iSaveMyChanges() |
||
198 | |||
199 | /** |
||
200 | * @Then /^(this customer) with name "([^"]*)" should appear in the store$/ |
||
201 | */ |
||
202 | public function theCustomerWithNameShouldAppearInTheRegistry(CustomerInterface $customer, $name) |
||
212 | |||
213 | /** |
||
214 | * @When I want to see all customers in store |
||
215 | */ |
||
216 | public function iWantToSeeAllCustomersInStore() |
||
220 | |||
221 | /** |
||
222 | * @Then /^I should see (\d+) customers in the list$/ |
||
223 | */ |
||
224 | public function iShouldSeeCustomersInTheList($amountOfCustomers) |
||
232 | |||
233 | /** |
||
234 | * @Then I should see the customer :email in the list |
||
235 | */ |
||
236 | public function iShouldSeeTheCustomerInTheList($email) |
||
243 | |||
244 | /** |
||
245 | * @Then /^I should be notified that ([^"]+) is required$/ |
||
246 | */ |
||
247 | public function iShouldBeNotifiedThatFirstNameIsRequired($elementName) |
||
251 | |||
252 | /** |
||
253 | * @Then /^I should be notified that ([^"]+) should be ([^"]+)$/ |
||
254 | */ |
||
255 | public function iShouldBeNotifiedThatTheElementShouldBe($elementName, $validationMessage) |
||
262 | |||
263 | /** |
||
264 | * @Then the customer with email :email should not appear in the store |
||
265 | */ |
||
266 | public function theCustomerShouldNotAppearInTheStore($email) |
||
275 | |||
276 | /** |
||
277 | * @When I remove its first name |
||
278 | */ |
||
279 | public function iRemoveItsFirstName() |
||
283 | |||
284 | /** |
||
285 | * @Then /^(this customer) should have an empty first name$/ |
||
286 | * @Then the customer :customer should still have an empty first name |
||
287 | */ |
||
288 | public function theCustomerShouldStillHaveAnEmptyFirstName(CustomerInterface $customer) |
||
298 | |||
299 | /** |
||
300 | * @When I remove its last name |
||
301 | */ |
||
302 | public function iRemoveItsLastName() |
||
306 | |||
307 | /** |
||
308 | * @Then /^(this customer) should have an empty last name$/ |
||
309 | * @Then the customer :customer should still have an empty last name |
||
310 | */ |
||
311 | public function theCustomerShouldStillHaveAnEmptyLastName(CustomerInterface $customer) |
||
321 | |||
322 | /** |
||
323 | * @When I remove its email |
||
324 | */ |
||
325 | public function iRemoveItsEmail() |
||
329 | |||
330 | /** |
||
331 | * @Then I should be notified that email is not valid |
||
332 | */ |
||
333 | public function iShouldBeNotifiedThatEmailIsNotValid() |
||
337 | |||
338 | /** |
||
339 | * @Then I should be notified that email must be unique |
||
340 | */ |
||
341 | public function iShouldBeNotifiedThatEmailMustBeUnique() |
||
345 | |||
346 | /** |
||
347 | * @Then there should still be only one customer with email :email |
||
348 | */ |
||
349 | public function thereShouldStillBeOnlyOneCustomerWithEmail($email) |
||
358 | |||
359 | /** |
||
360 | * @Given I want to enable :customer |
||
361 | * @Given I want to disable :customer |
||
362 | */ |
||
363 | public function iWantToChangeStatusOf(CustomerInterface $customer) |
||
367 | |||
368 | /** |
||
369 | * @When I enable their account |
||
370 | */ |
||
371 | public function iEnableIt() |
||
375 | |||
376 | /** |
||
377 | * @When I disable their account |
||
378 | */ |
||
379 | public function iDisableIt() |
||
383 | |||
384 | /** |
||
385 | * @Then /^(this customer) should be enabled$/ |
||
386 | */ |
||
387 | public function thisCustomerShouldBeEnabled(CustomerInterface $customer) |
||
397 | |||
398 | /** |
||
399 | * @Then /^(this customer) should be disabled$/ |
||
400 | */ |
||
401 | public function thisCustomerShouldBeDisabled(CustomerInterface $customer) |
||
411 | |||
412 | /** |
||
413 | * @When I specify its password as :password |
||
414 | */ |
||
415 | public function iSpecifyItsPasswordAs($password) |
||
419 | |||
420 | /** |
||
421 | * @When I change my password to :password |
||
422 | */ |
||
423 | public function iSpecifyMyPasswordAs($password) |
||
427 | |||
428 | /** |
||
429 | * @When I choose create account option |
||
430 | */ |
||
431 | public function iChooseCreateAccountOption() |
||
435 | |||
436 | /** |
||
437 | * @When I browse orders of a customer :customer |
||
438 | */ |
||
439 | public function iBrowseOrdersOfACustomer(CustomerInterface $customer) |
||
443 | |||
444 | /** |
||
445 | * @Then the customer :customer should have an account created |
||
446 | * @Then /^(this customer) should have an account created$/ |
||
447 | */ |
||
448 | public function theyShouldHaveAnAccountCreated(CustomerInterface $customer) |
||
455 | |||
456 | /** |
||
457 | * @When I view details of the customer :customer |
||
458 | */ |
||
459 | public function iViewDetailsOfTheCustomer(CustomerInterface $customer) |
||
463 | |||
464 | /** |
||
465 | * @Then his name should be :name |
||
466 | */ |
||
467 | public function hisNameShouldBe($name) |
||
475 | |||
476 | /** |
||
477 | * @Given he should be registered since :registrationDate |
||
478 | */ |
||
479 | public function hisRegistrationDateShouldBe($registrationDate) |
||
487 | |||
488 | /** |
||
489 | * @Given his email should be :email |
||
490 | */ |
||
491 | public function hisEmailShouldBe($email) |
||
499 | |||
500 | /** |
||
501 | * @Then his default address should be :defaultAddress |
||
502 | */ |
||
503 | public function hisShippingAddressShouldBe($defaultAddress) |
||
511 | |||
512 | /** |
||
513 | * @Then I should see information about no existing account for this customer |
||
514 | */ |
||
515 | public function iShouldSeeInformationAboutNoExistingAccountForThisCustomer() |
||
522 | |||
523 | /** |
||
524 | * @Then I should see that this customer is subscribed to the newsletter |
||
525 | */ |
||
526 | public function iShouldSeeThatThisCustomerIsSubscribedToTheNewsletter() |
||
533 | |||
534 | /** |
||
535 | * @Then I should not see information about email verification |
||
536 | */ |
||
537 | public function iShouldSeeInformationAboutEmailVerification() |
||
544 | |||
545 | /** |
||
546 | * @When I make them subscribed to the newsletter |
||
547 | */ |
||
548 | public function iMakeThemSubscribedToTheNewsletter() |
||
552 | |||
553 | /** |
||
554 | * @When I change the password of user :customer to :newPassword |
||
555 | */ |
||
556 | public function iChangeThePasswordOfUserTo(CustomerInterface $customer, $newPassword) |
||
562 | |||
563 | /** |
||
564 | * @Given I reset their password |
||
565 | */ |
||
566 | public function iResetTheirPassword() |
||
570 | |||
571 | /** |
||
572 | * @Then this customer should be subscribed to the newsletter |
||
573 | */ |
||
574 | public function thisCustomerShouldBeSubscribedToTheNewsletter() |
||
581 | |||
582 | /** |
||
583 | * @Then the province in the default address should be :provinceName |
||
584 | */ |
||
585 | public function theProvinceInTheDefaultAddressShouldBe($provinceName) |
||
592 | |||
593 | /** |
||
594 | * @Then this customer should have :groupName as their group |
||
595 | */ |
||
596 | public function thisCustomerShouldHaveAsTheirGroup($groupName) |
||
607 | |||
608 | /** |
||
609 | * @Then I should see that this customer has verified the email |
||
610 | */ |
||
611 | public function iShouldSeeThatThisCustomerHasVerifiedTheEmail() |
||
618 | |||
619 | /** |
||
620 | * @Then I should see a single order in the list |
||
621 | */ |
||
622 | public function iShouldSeeASingleOrderInTheList() |
||
630 | |||
631 | /** |
||
632 | * @Then I should see the order with number :orderNumber in the list |
||
633 | */ |
||
634 | public function iShouldSeeASingleOrderFromCustomer($orderNumber) |
||
641 | |||
642 | /** |
||
643 | * @Then I should not see the order with number :orderNumber in the list |
||
644 | */ |
||
645 | public function iShouldNotSeeASingleOrderFromCustomer($orderNumber) |
||
652 | } |
||
653 |
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: