Complex classes like AccountContext 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 AccountContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | final class AccountContext implements Context |
||
30 | { |
||
31 | /** |
||
32 | * @var DashboardPageInterface |
||
33 | */ |
||
34 | private $dashboardPage; |
||
35 | |||
36 | /** |
||
37 | * @var ProfileUpdatePageInterface |
||
38 | */ |
||
39 | private $profileUpdatePage; |
||
40 | |||
41 | /** |
||
42 | * @var ChangePasswordPageInterface |
||
43 | */ |
||
44 | private $changePasswordPage; |
||
45 | |||
46 | /** |
||
47 | * @var IndexPageInterface |
||
48 | */ |
||
49 | private $orderIndexPage; |
||
50 | |||
51 | /** |
||
52 | * @var ShowPageInterface |
||
53 | */ |
||
54 | private $orderShowPage; |
||
55 | |||
56 | /** |
||
57 | * @var LoginPageInterface |
||
58 | */ |
||
59 | private $loginPage; |
||
60 | |||
61 | /** |
||
62 | * @var NotificationCheckerInterface |
||
63 | */ |
||
64 | private $notificationChecker; |
||
65 | |||
66 | /** |
||
67 | * @param DashboardPageInterface $dashboardPage |
||
68 | * @param ProfileUpdatePageInterface $profileUpdatePage |
||
69 | * @param ChangePasswordPageInterface $changePasswordPage |
||
70 | * @param IndexPageInterface $orderIndexPage |
||
71 | * @param ShowPageInterface $orderShowPage |
||
72 | * @param LoginPageInterface $loginPage |
||
73 | * @param NotificationCheckerInterface $notificationChecker |
||
74 | */ |
||
75 | public function __construct( |
||
92 | |||
93 | /** |
||
94 | * @When I want to modify my profile |
||
95 | */ |
||
96 | public function iWantToModifyMyProfile() |
||
100 | |||
101 | /** |
||
102 | * @When I specify the first name as :firstName |
||
103 | * @When I remove the first name |
||
104 | */ |
||
105 | public function iSpecifyTheFirstName($firstName = null) |
||
109 | |||
110 | /** |
||
111 | * @When I specify the last name as :lastName |
||
112 | * @When I remove the last name |
||
113 | */ |
||
114 | public function iSpecifyTheLastName($lastName = null) |
||
118 | |||
119 | /** |
||
120 | * @When I specify the customer email as :email |
||
121 | * @When I remove the customer email |
||
122 | */ |
||
123 | public function iSpecifyCustomerTheEmail($email = null) |
||
127 | |||
128 | /** |
||
129 | * @When I save my changes |
||
130 | * @When I try to save my changes |
||
131 | */ |
||
132 | public function iSaveMyChanges() |
||
136 | |||
137 | /** |
||
138 | * @Then I should be notified that it has been successfully edited |
||
139 | */ |
||
140 | public function iShouldBeNotifiedThatItHasBeenSuccessfullyEdited() |
||
144 | |||
145 | /** |
||
146 | * @Then my name should be :name |
||
147 | * @Then my name should still be :name |
||
148 | */ |
||
149 | public function myNameShouldBe($name) |
||
155 | |||
156 | /** |
||
157 | * @Then my email should be :email |
||
158 | * @Then my email should still be :email |
||
159 | */ |
||
160 | public function myEmailShouldBe($email) |
||
166 | |||
167 | /** |
||
168 | * @Then /^I should be notified that the (email|password|city|street|first name|last name) is required$/ |
||
169 | */ |
||
170 | public function iShouldBeNotifiedThatElementIsRequired($element) |
||
177 | |||
178 | /** |
||
179 | * @Then /^I should be notified that the (email) is invalid$/ |
||
180 | */ |
||
181 | public function iShouldBeNotifiedThatElementIsInvalid($element) |
||
188 | |||
189 | /** |
||
190 | * @Then I should be notified that the email is already used |
||
191 | */ |
||
192 | public function iShouldBeNotifiedThatTheEmailIsAlreadyUsed() |
||
196 | |||
197 | /** |
||
198 | * @Given /^I want to change my password$/ |
||
199 | */ |
||
200 | public function iWantToChangeMyPassword() |
||
204 | |||
205 | /** |
||
206 | * @Given I change password from :oldPassword to :newPassword |
||
207 | */ |
||
208 | public function iChangePasswordTo($oldPassword, $newPassword) |
||
214 | |||
215 | /** |
||
216 | * @Then I should be notified that my password has been successfully changed |
||
217 | */ |
||
218 | public function iShouldBeNotifiedThatMyPasswordHasBeenSuccessfullyChanged() |
||
222 | |||
223 | /** |
||
224 | * @Given I specify the current password as :password |
||
225 | */ |
||
226 | public function iSpecifyTheCurrentPasswordAs($password) |
||
230 | |||
231 | /** |
||
232 | * @Given I specify the new password as :password |
||
233 | */ |
||
234 | public function iSpecifyTheNewPasswordAs($password) |
||
238 | |||
239 | /** |
||
240 | * @Given I confirm this password as :password |
||
241 | */ |
||
242 | public function iSpecifyTheConfirmationPasswordAs($password) |
||
246 | |||
247 | /** |
||
248 | * @Then I should be notified that provided password is different than the current one |
||
249 | */ |
||
250 | public function iShouldBeNotifiedThatProvidedPasswordIsDifferentThanTheCurrentOne() |
||
257 | |||
258 | /** |
||
259 | * @Then I should be notified that the entered passwords do not match |
||
260 | */ |
||
261 | public function iShouldBeNotifiedThatTheEnteredPasswordsDoNotMatch() |
||
268 | |||
269 | /** |
||
270 | * @Then I should be notified that the password should be at least 4 characters long |
||
271 | */ |
||
272 | public function iShouldBeNotifiedThatThePasswordShouldBeAtLeastCharactersLong() |
||
279 | |||
280 | /** |
||
281 | * @When I browse my orders |
||
282 | */ |
||
283 | public function iBrowseMyOrders() |
||
287 | |||
288 | /** |
||
289 | * @Then I should see a single order in the list |
||
290 | */ |
||
291 | public function iShouldSeeASingleOrderInTheList() |
||
295 | |||
296 | /** |
||
297 | * @Then this order should have :order number |
||
298 | */ |
||
299 | public function thisOrderShouldHaveNumber(OrderInterface $order) |
||
303 | |||
304 | /** |
||
305 | * @When I view the summary of the order :order |
||
306 | */ |
||
307 | public function iViewTheSummaryOfTheOrder(OrderInterface $order) |
||
311 | |||
312 | /** |
||
313 | * @When I am viewing the summary of my last order |
||
314 | */ |
||
315 | public function iViewingTheSummaryOfMyLastOrder() |
||
320 | |||
321 | /** |
||
322 | * @Then it should has number :orderNumber |
||
323 | */ |
||
324 | public function itShouldHasNumber($orderNumber) |
||
328 | |||
329 | /** |
||
330 | * @Then I should see :customerName, :street, :postcode, :city, :countryName as shipping address |
||
331 | */ |
||
332 | public function iShouldSeeAsShippingAddress($customerName, $street, $postcode, $city, $countryName) |
||
336 | |||
337 | /** |
||
338 | * @Then I should see :customerName, :street, :postcode, :city, :countryName as billing address |
||
339 | */ |
||
340 | public function itShouldBeShippedTo($customerName, $street, $postcode, $city, $countryName) |
||
344 | |||
345 | /** |
||
346 | * @Then I should see :total as order's total |
||
347 | */ |
||
348 | public function iShouldSeeAsOrderSTotal($total) |
||
352 | |||
353 | /** |
||
354 | * @Then I should see :itemsTotal as order's subtotal |
||
355 | */ |
||
356 | public function iShouldSeeAsOrderSSubtotal($subtotal) |
||
360 | |||
361 | /** |
||
362 | * @Then I should see that I have to pay :paymentAmount for this order |
||
363 | * @Then I should see :paymentTotal as payment total |
||
364 | */ |
||
365 | public function iShouldSeeIHaveToPayForThisOrder($paymentAmount) |
||
369 | |||
370 | /** |
||
371 | * @Then I should see :numberOfItems items in the list |
||
372 | */ |
||
373 | public function iShouldSeeItemsInTheList($numberOfItems) |
||
377 | |||
378 | /** |
||
379 | * @Then the product named :productName should be in the items list |
||
380 | */ |
||
381 | public function theProductShouldBeInTheItemsList($productName) |
||
385 | |||
386 | /** |
||
387 | * @Then I should see :itemPrice as item price |
||
388 | */ |
||
389 | public function iShouldSeeAsItemPrice($itemPrice) |
||
393 | |||
394 | /** |
||
395 | * @When I subscribe to the newsletter |
||
396 | */ |
||
397 | public function iSubscribeToTheNewsletter() |
||
401 | |||
402 | /** |
||
403 | * @Then I should be subscribed to the newsletter |
||
404 | */ |
||
405 | public function iShouldBeSubscribedToTheNewsletter() |
||
409 | |||
410 | /** |
||
411 | * @Then I should see :provinceName as province in the shipping address |
||
412 | */ |
||
413 | public function iShouldSeeAsProvinceInTheShippingAddress($provinceName) |
||
417 | |||
418 | /** |
||
419 | * @Then I should see :provinceName as province in the billing address |
||
420 | */ |
||
421 | public function iShouldSeeAsProvinceInTheBillingAddress($provinceName) |
||
425 | |||
426 | /** |
||
427 | * @Then /^I should be able to change payment method for (this order)$/ |
||
428 | */ |
||
429 | public function iShouldBeAbleToChangePaymentMethodForThisOrder(OrderInterface $order) |
||
433 | |||
434 | /** |
||
435 | * @Then I should be redirected to my account dashboard |
||
436 | */ |
||
437 | public function iShouldBeRedirectedToMyAccountDashboard() |
||
441 | |||
442 | /** |
||
443 | * @Then my last order total should be :total |
||
444 | */ |
||
445 | public function myLastOrderTotalShouldBe(string $total): void |
||
453 | |||
454 | /** |
||
455 | * @When I want to log in |
||
456 | */ |
||
457 | public function iWantToLogIn() |
||
461 | } |
||
462 |