Complex classes like ManagingShippingMethodsContext 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 ManagingShippingMethodsContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | final class ManagingShippingMethodsContext implements Context |
||
30 | { |
||
31 | /** |
||
32 | * @var SharedStorageInterface |
||
33 | */ |
||
34 | private $sharedStorage; |
||
35 | |||
36 | /** |
||
37 | * @var IndexPageInterface |
||
38 | */ |
||
39 | private $indexPage; |
||
40 | |||
41 | /** |
||
42 | * @var CreatePageInterface |
||
43 | */ |
||
44 | private $createPage; |
||
45 | |||
46 | /** |
||
47 | * @var UpdatePageInterface |
||
48 | */ |
||
49 | private $updatePage; |
||
50 | |||
51 | /** |
||
52 | * @var CurrentPageResolverInterface |
||
53 | */ |
||
54 | private $currentPageResolver; |
||
55 | |||
56 | /** |
||
57 | * @var NotificationCheckerInterface |
||
58 | */ |
||
59 | private $notificationChecker; |
||
60 | |||
61 | /** |
||
62 | * @param SharedStorageInterface $sharedStorage |
||
63 | * @param IndexPageInterface $indexPage |
||
64 | * @param CreatePageInterface $createPage |
||
65 | * @param UpdatePageInterface $updatePage |
||
66 | * @param CurrentPageResolverInterface $currentPageResolver |
||
67 | * @param NotificationCheckerInterface $notificationChecker |
||
68 | */ |
||
69 | public function __construct( |
||
84 | |||
85 | /** |
||
86 | * @Given I want to create a new shipping method |
||
87 | */ |
||
88 | public function iWantToCreateANewShippingMethod() |
||
92 | |||
93 | /** |
||
94 | * @When I specify its code as :code |
||
95 | * @When I do not specify its code |
||
96 | */ |
||
97 | public function iSpecifyItsCodeAs($code = null) |
||
101 | |||
102 | /** |
||
103 | * @When I specify its position as :position |
||
104 | * @When I do not specify its position |
||
105 | */ |
||
106 | public function iSpecifyItsPositionAs($position = null) |
||
110 | |||
111 | /** |
||
112 | * @When I name it :name in :language |
||
113 | * @When I rename it to :name in :language |
||
114 | */ |
||
115 | public function iNameItIn($name, $language) |
||
119 | |||
120 | /** |
||
121 | * @When I describe it as :description in :language |
||
122 | */ |
||
123 | public function iDescribeItAsIn($description, $language) |
||
127 | |||
128 | /** |
||
129 | * @When I define it for the :zoneName zone |
||
130 | */ |
||
131 | public function iDefineItForTheZone($zoneName) |
||
135 | |||
136 | /** |
||
137 | * @When I specify its amount as :amount |
||
138 | */ |
||
139 | public function iSpecifyItsAmountAs($amount) |
||
143 | |||
144 | /** |
||
145 | * @When I make it available in channel :channel |
||
146 | */ |
||
147 | public function iMakeItAvaialbelInChannel($channel) |
||
151 | |||
152 | /** |
||
153 | * @When I add it |
||
154 | * @When I try to add it |
||
155 | */ |
||
156 | public function iAddIt() |
||
160 | |||
161 | /** |
||
162 | * @When I choose :calculatorName calculator |
||
163 | * @When I do not specify amount for :calculatorName calculator |
||
164 | */ |
||
165 | public function iChooseCalculator($calculatorName) |
||
169 | |||
170 | /** |
||
171 | * @Then the shipping method :shipmentMethod should appear in the registry |
||
172 | * @Then the shipping method :shipmentMethod should be in the registry |
||
173 | */ |
||
174 | public function theShipmentMethodShouldAppearInTheRegistry($shipmentMethodName) |
||
183 | |||
184 | /** |
||
185 | * @Given /^(this shipping method) should still be in the registry$/ |
||
186 | */ |
||
187 | public function thisShippingMethodShouldStillBeInTheRegistry(ShippingMethodInterface $shippingMethod) |
||
191 | |||
192 | /** |
||
193 | * @Then the shipping method :shippingMethod should be available in channel :channelName |
||
194 | */ |
||
195 | public function theShippingMethodShouldBeAvailableInChannel( |
||
206 | |||
207 | /** |
||
208 | * @Then I should be notified that shipping method with this code already exists |
||
209 | */ |
||
210 | public function iShouldBeNotifiedThatShippingMethodWithThisCodeAlreadyExists() |
||
214 | |||
215 | /** |
||
216 | * @Then there should still be only one shipping method with :element :code |
||
217 | */ |
||
218 | public function thereShouldStillBeOnlyOneShippingMethodWith($element, $code) |
||
227 | |||
228 | /** |
||
229 | * @Given I want to modify a shipping method :shippingMethod |
||
230 | * @Given /^I want to modify (this shipping method)$/ |
||
231 | */ |
||
232 | public function iWantToModifyAShippingMethod(ShippingMethodInterface $shippingMethod) |
||
236 | |||
237 | /** |
||
238 | * @Then the code field should be disabled |
||
239 | */ |
||
240 | public function theCodeFieldShouldBeDisabled() |
||
247 | |||
248 | /** |
||
249 | * @Then /^(this shipping method) name should be "([^"]+)"$/ |
||
250 | * @Then /^(this shipping method) should still be named "([^"]+)"$/ |
||
251 | */ |
||
252 | public function thisShippingMethodNameShouldBe(ShippingMethodInterface $shippingMethod, $shippingMethodName) |
||
266 | |||
267 | /** |
||
268 | * @When I save my changes |
||
269 | * @When I try to save my changes |
||
270 | */ |
||
271 | public function iSaveMyChanges() |
||
275 | |||
276 | /** |
||
277 | * @Then I should be notified that :element is required |
||
278 | */ |
||
279 | public function iShouldBeNotifiedThatIsRequired($element) |
||
283 | |||
284 | /** |
||
285 | * @Then shipping method with :element :name should not be added |
||
286 | */ |
||
287 | public function shippingMethodWithElementValueShouldNotBeAdded($element, $name) |
||
296 | |||
297 | /** |
||
298 | * @When I do not name it |
||
299 | */ |
||
300 | public function iDoNotNameIt() |
||
304 | |||
305 | /** |
||
306 | * @When I do not specify its zone |
||
307 | */ |
||
308 | public function iDoNotSpecifyItsZone() |
||
312 | |||
313 | /** |
||
314 | * @Then I should be notified that :element has to be selected |
||
315 | */ |
||
316 | public function iShouldBeNotifiedThatElementHasToBeSelected($element) |
||
320 | |||
321 | /** |
||
322 | * @When I remove its name from :language translation |
||
323 | */ |
||
324 | public function iRemoveItsNameFromTranslation($language) |
||
328 | |||
329 | /** |
||
330 | * @Then I should be notified that :field should not be blank |
||
331 | */ |
||
332 | public function iShouldBeNotifiedThatAmountShouldNotBeBlank($field) |
||
336 | /** |
||
337 | * @Given I am browsing shipping methods |
||
338 | * @When I want to browse shipping methods |
||
339 | */ |
||
340 | public function iWantToBrowseShippingMethods() |
||
344 | |||
345 | /** |
||
346 | * @Then the first shipping method on the list should have :field :value |
||
347 | */ |
||
348 | public function theFirstShippingMethodOnTheListShouldHave($field, $value) |
||
359 | |||
360 | /** |
||
361 | * @Then the last shipping method on the list should have :field :value |
||
362 | */ |
||
363 | public function theLastShippingMethodOnTheListShouldHave($field, $value) |
||
374 | |||
375 | /** |
||
376 | * @When I switch the way shipping methods are sorted by :field |
||
377 | * @When I start sorting shipping methods by :field |
||
378 | * @Given the shipping methods are already sorted by :field |
||
379 | */ |
||
380 | public function iSortShippingMethodsBy($field) |
||
384 | |||
385 | /** |
||
386 | * @Then I should see :numberOfShippingMethods shipping methods in the list |
||
387 | */ |
||
388 | public function iShouldSeeShippingMethodsInTheList($numberOfShippingMethods) |
||
397 | |||
398 | /** |
||
399 | * @When I enable it |
||
400 | */ |
||
401 | public function iEnableIt() |
||
405 | |||
406 | /** |
||
407 | * @When I disable it |
||
408 | */ |
||
409 | public function iDisableIt() |
||
413 | |||
414 | /** |
||
415 | * @Then /^(this shipping method) should be disabled$/ |
||
416 | */ |
||
417 | public function thisShippingMethodShouldBeDisabled(ShippingMethodInterface $shippingMethod) |
||
421 | |||
422 | /** |
||
423 | * @Then /^(this shipping method) should be enabled$/ |
||
424 | */ |
||
425 | public function thisShippingMethodShouldBeEnabled(ShippingMethodInterface $shippingMethod) |
||
429 | |||
430 | /** |
||
431 | * @When I delete shipping method :shippingMethod |
||
432 | * @When I try to delete shipping method :shippingMethod |
||
433 | */ |
||
434 | public function iDeleteShippingMethod(ShippingMethodInterface $shippingMethod) |
||
439 | |||
440 | /** |
||
441 | * @Then /^(this shipping method) should no longer exist in the registry$/ |
||
442 | */ |
||
443 | public function thisShippingMethodShouldNoLongerExistInTheRegistry(ShippingMethodInterface $shippingMethod) |
||
450 | |||
451 | /** |
||
452 | * @Then I should be notified that it is in use |
||
453 | */ |
||
454 | public function iShouldBeNotifiedThatItIsInUse() |
||
458 | |||
459 | /** |
||
460 | * @param string $element |
||
461 | * @param string $expectedMessage |
||
462 | */ |
||
463 | private function assertFieldValidationMessage($element, $expectedMessage) |
||
470 | |||
471 | /** |
||
472 | * @param ShippingMethodInterface $shippingMethod |
||
473 | * @param bool $state |
||
474 | */ |
||
475 | private function assertShippingMethodState(ShippingMethodInterface $shippingMethod, $state) |
||
487 | } |
||
488 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.