Complex classes like ManagingTaxonsContext 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 ManagingTaxonsContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | final class ManagingTaxonsContext implements Context |
||
26 | { |
||
27 | /** |
||
28 | * @var SharedStorageInterface |
||
29 | */ |
||
30 | private $sharedStorage; |
||
31 | |||
32 | /** |
||
33 | * @var CreatePageInterface |
||
34 | */ |
||
35 | private $createPage; |
||
36 | |||
37 | /** |
||
38 | * @var UpdatePageInterface |
||
39 | */ |
||
40 | private $updatePage; |
||
41 | |||
42 | /** |
||
43 | * @var CurrentPageResolverInterface |
||
44 | */ |
||
45 | private $currentPageResolver; |
||
46 | |||
47 | /** |
||
48 | * @param SharedStorageInterface $sharedStorage |
||
49 | * @param CreatePageInterface $createPage |
||
50 | * @param UpdatePageInterface $updatePage |
||
51 | * @param CurrentPageResolverInterface $currentPageResolver |
||
52 | */ |
||
53 | public function __construct( |
||
64 | |||
65 | /** |
||
66 | * @Given I want to create a new taxon |
||
67 | * @Given I want to see all taxons in store |
||
68 | */ |
||
69 | public function iWantToCreateANewTaxon() |
||
73 | |||
74 | /** |
||
75 | * @Given /^I want to modify the ("[^"]+" taxon)$/ |
||
76 | */ |
||
77 | public function iWantToModifyATaxon(TaxonInterface $taxon) |
||
83 | |||
84 | /** |
||
85 | * @When I specify its code as :code |
||
86 | */ |
||
87 | public function iSpecifyItsCodeAs($code) |
||
91 | |||
92 | /** |
||
93 | * @When I name it :name in :language |
||
94 | */ |
||
95 | public function iNameItIn($name, $language) |
||
99 | |||
100 | /** |
||
101 | * @When I rename it to :name in :language |
||
102 | */ |
||
103 | public function iRenameItIn($name, $language) |
||
107 | |||
108 | /** |
||
109 | * @When I change its description to :description in :language |
||
110 | */ |
||
111 | public function iChangeItsDescriptionToIn($description, $language) |
||
115 | |||
116 | /** |
||
117 | * @When I specify its permalink as :permalink in :language |
||
118 | */ |
||
119 | public function iSpecifyItsPermalinkAs($permalink, $language) |
||
123 | |||
124 | /** |
||
125 | * @When I change its permalink to :permalink in :language |
||
126 | */ |
||
127 | public function iChangeItsPermalinkToIn($permalink, $language) |
||
131 | |||
132 | /** |
||
133 | * @When I describe it as :description in :language |
||
134 | */ |
||
135 | public function iDescribeItAs($description, $language) |
||
139 | |||
140 | /** |
||
141 | * @Given /^I choose ("[^"]+" as a parent taxon)$/ |
||
142 | */ |
||
143 | public function iChooseAsAParentTaxon(TaxonInterface $taxon) |
||
147 | |||
148 | /** |
||
149 | * @Given /^I change its (parent taxon to "[^"]+")$/ |
||
150 | */ |
||
151 | public function iChangeItsParentTaxonTo(TaxonInterface $taxon) |
||
155 | |||
156 | /** |
||
157 | * @When I do not specify its code |
||
158 | */ |
||
159 | public function iDoNotSpecifyItsCode() |
||
163 | |||
164 | /** |
||
165 | * @When I do not specify its name |
||
166 | */ |
||
167 | public function iDoNotSpecifyItsName() |
||
171 | |||
172 | /** |
||
173 | * @When I delete taxon named :name |
||
174 | */ |
||
175 | public function iDeleteTaxonNamed($name) |
||
180 | |||
181 | /** |
||
182 | * @When I add it |
||
183 | * @When I try to add it |
||
184 | */ |
||
185 | public function iAddIt() |
||
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 /^the ("[^"]+" taxon) should appear in the registry$/ |
||
201 | */ |
||
202 | public function theTaxonShouldAppearInTheRegistry(TaxonInterface $taxon) |
||
210 | |||
211 | /** |
||
212 | * @Then this taxon :element should be :value |
||
213 | */ |
||
214 | public function thisTaxonElementShouldBe($element, $value) |
||
221 | |||
222 | /** |
||
223 | * @Then the code field should be disabled |
||
224 | */ |
||
225 | public function theCodeFieldShouldBeDisabled() |
||
232 | |||
233 | /** |
||
234 | * @Then /^this taxon should (belongs to "[^"]+")$/ |
||
235 | */ |
||
236 | public function thisTaxonShouldBelongsTo(TaxonInterface $taxon) |
||
243 | |||
244 | /** |
||
245 | * @Then I should be notified that taxon with this code already exists |
||
246 | */ |
||
247 | public function iShouldBeNotifiedThatTaxonWithThisCodeAlreadyExists() |
||
254 | |||
255 | /** |
||
256 | * @Then I should be notified that :element is required |
||
257 | */ |
||
258 | public function iShouldBeNotifiedThatIsRequired($element) |
||
265 | |||
266 | /** |
||
267 | * @Then /^there should still be only one taxon with code "([^"]+)"$/ |
||
268 | */ |
||
269 | public function thereShouldStillBeOnlyOneTaxonWithCode($code) |
||
276 | |||
277 | /** |
||
278 | * @Then /^Taxon named "([^"]+)" should not be added$/ |
||
279 | * @Then the taxon named :name should no longer exist in the registry |
||
280 | */ |
||
281 | public function taxonNamedShouldNotBeAdded($name) |
||
289 | |||
290 | /** |
||
291 | * @Then /^I should see (\d+) taxons on the list$/ |
||
292 | */ |
||
293 | public function iShouldSeeTaxonsInTheList($number) |
||
303 | |||
304 | /** |
||
305 | * @Then I should see the taxon named :name in the list |
||
306 | */ |
||
307 | public function iShouldSeeTheTaxonNamedInTheList($name) |
||
315 | |||
316 | /** |
||
317 | * @When I attach the :path image with a code :code |
||
318 | */ |
||
319 | public function iAttachImageWithACode($path, $code) |
||
326 | |||
327 | /** |
||
328 | * @When I attach the :path image without a code |
||
329 | */ |
||
330 | public function iAttachImageWithoutACode($path) |
||
334 | |||
335 | /** |
||
336 | * @Then /^this taxon should have(?:| also) an image with a code "([^"]*)"$/ |
||
337 | */ |
||
338 | public function thisTaxonShouldHaveAnImageWithCode($code) |
||
345 | |||
346 | /** |
||
347 | * @Then /^this taxon should not have(?:| also) an image with a code "([^"]*)"$/ |
||
348 | */ |
||
349 | public function thisTaxonShouldNotHaveAnImageWithCode($code) |
||
356 | |||
357 | /** |
||
358 | * @When /^I remove(?:| also) an image with a code "([^"]*)"$/ |
||
359 | */ |
||
360 | public function iRemoveAnImageWithACode($code) |
||
364 | |||
365 | /** |
||
366 | * @When I remove the first image |
||
367 | */ |
||
368 | public function iRemoveTheFirstImage() |
||
372 | |||
373 | /** |
||
374 | * @Then /^(this taxon) should not have any images$/ |
||
375 | */ |
||
376 | public function thisTaxonShouldNotHaveImages(TaxonInterface $taxon) |
||
386 | |||
387 | /** |
||
388 | * @When I change the image with the :code code to :path |
||
389 | */ |
||
390 | public function iChangeItsImageToPathForTheCode($path, $code) |
||
394 | |||
395 | /** |
||
396 | * @Then I should be notified that the image with this code already exists |
||
397 | */ |
||
398 | public function iShouldBeNotifiedThatTheImageWithThisCodeAlreadyExists() |
||
402 | |||
403 | /** |
||
404 | * @Then I should be notified that an image code is required |
||
405 | */ |
||
406 | public function iShouldBeNotifiedThatAnImageCodeIsRequired() |
||
413 | |||
414 | /** |
||
415 | * @Then there should still be only one image in the :taxon taxon |
||
416 | */ |
||
417 | public function thereShouldStillBeOnlyOneImageInThisTaxon(TaxonInterface $taxon) |
||
427 | |||
428 | /** |
||
429 | * @Then the image code field should be disabled |
||
430 | */ |
||
431 | public function theImageCodeFieldShouldBeDisabled() |
||
438 | |||
439 | /** |
||
440 | * @Then I should be notified that the :imageNumber image should have an unique code |
||
441 | */ |
||
442 | public function iShouldBeNotifiedThatTheFirstImageShouldHaveAnUniqueCode($imageNumber) |
||
451 | } |
||
452 |