Complex classes like ManagingProductsContext 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 ManagingProductsContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | final class ManagingProductsContext implements Context |
||
37 | { |
||
38 | /** |
||
39 | * @var SharedStorageInterface |
||
40 | */ |
||
41 | private $sharedStorage; |
||
42 | |||
43 | /**x |
||
44 | * @var CreateSimpleProductPageInterface |
||
45 | */ |
||
46 | private $createSimpleProductPage; |
||
47 | |||
48 | /** |
||
49 | * @var CreateConfigurableProductPageInterface |
||
50 | */ |
||
51 | private $createConfigurableProductPage; |
||
52 | |||
53 | /** |
||
54 | * @var IndexPageInterface |
||
55 | */ |
||
56 | private $indexPage; |
||
57 | |||
58 | /** |
||
59 | * @var UpdateSimpleProductPageInterface |
||
60 | */ |
||
61 | private $updateSimpleProductPage; |
||
62 | |||
63 | /** |
||
64 | * @var UpdateConfigurableProductPageInterface |
||
65 | */ |
||
66 | private $updateConfigurableProductPage; |
||
67 | |||
68 | /** |
||
69 | * @var ProductReviewIndexPageInterface |
||
70 | */ |
||
71 | private $productReviewIndexPage; |
||
72 | |||
73 | /** |
||
74 | * @var CurrentProductPageResolverInterface |
||
75 | */ |
||
76 | private $currentPageResolver; |
||
77 | |||
78 | /** |
||
79 | * @var NotificationCheckerInterface |
||
80 | */ |
||
81 | private $notificationChecker; |
||
82 | |||
83 | /** |
||
84 | * @param SharedStorageInterface $sharedStorage |
||
85 | * @param CreateSimpleProductPageInterface $createSimpleProductPage |
||
86 | * @param CreateConfigurableProductPageInterface $createConfigurableProductPage |
||
87 | * @param IndexPageInterface $indexPage |
||
88 | * @param UpdateSimpleProductPageInterface $updateSimpleProductPage |
||
89 | * @param UpdateConfigurableProductPageInterface $updateConfigurableProductPage |
||
90 | * @param ProductReviewIndexPageInterface $productReviewIndexPage |
||
91 | * @param CurrentProductPageResolverInterface $currentPageResolver |
||
92 | * @param NotificationCheckerInterface $notificationChecker |
||
93 | */ |
||
94 | public function __construct( |
||
115 | |||
116 | /** |
||
117 | * @Given I want to create a new simple product |
||
118 | */ |
||
119 | public function iWantToCreateANewSimpleProduct() |
||
123 | |||
124 | /** |
||
125 | * @Given I want to create a new configurable product |
||
126 | */ |
||
127 | public function iWantToCreateANewConfigurableProduct() |
||
131 | |||
132 | /** |
||
133 | * @When I specify its code as :code |
||
134 | * @When I do not specify its code |
||
135 | */ |
||
136 | public function iSpecifyItsCodeAs($code = null) |
||
145 | |||
146 | /** |
||
147 | * @When I name it :name in :language |
||
148 | */ |
||
149 | public function iNameItIn($name, $language) |
||
158 | |||
159 | /** |
||
160 | * @When I rename it to :name in :language |
||
161 | */ |
||
162 | public function iRenameItToIn($name, $language) |
||
171 | |||
172 | /** |
||
173 | * @When I add it |
||
174 | * @When I try to add it |
||
175 | */ |
||
176 | public function iAddIt() |
||
188 | |||
189 | /** |
||
190 | * @When I disable its inventory tracking |
||
191 | */ |
||
192 | public function iDisableItsTracking() |
||
196 | |||
197 | /** |
||
198 | * @When I enable its inventory tracking |
||
199 | */ |
||
200 | public function iEnableItsTracking() |
||
204 | |||
205 | /** |
||
206 | * @When /^I set its price to ("(?:€|£|\$)[^"]+")$/ |
||
207 | */ |
||
208 | public function iSetItsPriceTo($price) |
||
212 | |||
213 | /** |
||
214 | * @When I set its slug to :slug |
||
215 | * @When I remove its slug |
||
216 | */ |
||
217 | public function iSetItsSlugTo($slug = null) |
||
221 | |||
222 | /** |
||
223 | * @When I enable slug modification |
||
224 | */ |
||
225 | public function iEnableSlugModification() |
||
229 | |||
230 | /** |
||
231 | * @Then the product :productName should appear in the shop |
||
232 | * @Then the product :productName should be in the shop |
||
233 | * @Then this product should still be named :productName |
||
234 | */ |
||
235 | public function theProductShouldAppearInTheShop($productName) |
||
244 | |||
245 | /** |
||
246 | * @Given I am browsing products |
||
247 | * @When I want to browse products |
||
248 | */ |
||
249 | public function iWantToBrowseProducts() |
||
253 | |||
254 | /** |
||
255 | * @When I filter them by :taxonName taxon |
||
256 | */ |
||
257 | public function iFilterThemByTaxon($taxonName) |
||
261 | |||
262 | /** |
||
263 | * @Then I should( still) see a product with :field :value |
||
264 | */ |
||
265 | public function iShouldSeeProductWith($field, $value) |
||
272 | |||
273 | /** |
||
274 | * @Then I should not see any product with :field :value |
||
275 | */ |
||
276 | public function iShouldNotSeeAnyProductWith($field, $value) |
||
283 | |||
284 | /** |
||
285 | * @Then the first product on the list should have :field :value |
||
286 | */ |
||
287 | public function theFirstProductOnTheListShouldHave($field, $value) |
||
297 | |||
298 | /** |
||
299 | * @When I switch the way products are sorted by :field |
||
300 | * @When I start sorting products by :field |
||
301 | * @Given the products are already sorted by :field |
||
302 | */ |
||
303 | public function iSortProductsBy($field) |
||
307 | |||
308 | /** |
||
309 | * @Then I should see :numberOfProducts products in the list |
||
310 | */ |
||
311 | public function iShouldSeeProductsInTheList($numberOfProducts) |
||
321 | |||
322 | /** |
||
323 | * @When I delete the :product product |
||
324 | * @When I try to delete the :product product |
||
325 | */ |
||
326 | public function iDeleteProduct(ProductInterface $product) |
||
333 | |||
334 | /** |
||
335 | * @Then /^(this product) should not exist in the product catalog$/ |
||
336 | */ |
||
337 | public function productShouldNotExist(ProductInterface $product) |
||
346 | |||
347 | /** |
||
348 | * @Then I should be notified that this product is in use and cannot be deleted |
||
349 | */ |
||
350 | public function iShouldBeNotifiedOfFailure() |
||
357 | |||
358 | /** |
||
359 | * @Then /^(this product) should still exist in the product catalog$/ |
||
360 | */ |
||
361 | public function productShouldExistInTheProductCatalog(ProductInterface $product) |
||
365 | |||
366 | /** |
||
367 | * @When I want to modify the :product product |
||
368 | * @When /^I want to modify (this product)$/ |
||
369 | */ |
||
370 | public function iWantToModifyAProduct(ProductInterface $product) |
||
381 | |||
382 | /** |
||
383 | * @Then the code field should be disabled |
||
384 | */ |
||
385 | public function theCodeFieldShouldBeDisabled() |
||
398 | |||
399 | /** |
||
400 | * @Then the slug field should not be editable |
||
401 | */ |
||
402 | public function theSlugFieldShouldNotBeEditable() |
||
409 | |||
410 | /** |
||
411 | * @Then /^this product price should be "(?:€|£|\$)([^"]+)"$/ |
||
412 | */ |
||
413 | public function thisProductPriceShouldBeEqualTo($price) |
||
417 | |||
418 | /** |
||
419 | * @Then this product name should be :name |
||
420 | */ |
||
421 | public function thisProductElementShouldBe($name) |
||
425 | |||
426 | /** |
||
427 | * @Then /^I should be notified that (code|name|slug) is required$/ |
||
428 | */ |
||
429 | public function iShouldBeNotifiedThatIsRequired($element) |
||
433 | |||
434 | /** |
||
435 | * @Then I should be notified that price is required |
||
436 | */ |
||
437 | public function iShouldBeNotifiedThatPriceIsRequired() |
||
441 | |||
442 | /** |
||
443 | * @When I save my changes |
||
444 | * @When I try to save my changes |
||
445 | */ |
||
446 | public function iSaveMyChanges() |
||
458 | |||
459 | /** |
||
460 | * @When /^I change its price to "(?:€|£|\$)([^"]+)"$/ |
||
461 | */ |
||
462 | public function iChangeItsPriceTo($price) |
||
466 | |||
467 | /** |
||
468 | * @Given I add the :optionName option to it |
||
469 | */ |
||
470 | public function iAddTheOptionToIt($optionName) |
||
474 | |||
475 | /** |
||
476 | * @When I set its :attribute attribute to :value |
||
477 | */ |
||
478 | public function iSetItsAttributeTo($attribute, $value) |
||
482 | |||
483 | /** |
||
484 | * @When I remove its :attribute attribute |
||
485 | */ |
||
486 | public function iRemoveItsAttribute($attribute) |
||
490 | |||
491 | /** |
||
492 | * @Then /^attribute "([^"]+)" of (product "[^"]+") should be "([^"]+)"$/ |
||
493 | */ |
||
494 | public function itsAttributeShouldBe($attribute, ProductInterface $product, $value) |
||
504 | |||
505 | /** |
||
506 | * @Then /^(product "[^"]+") should not have a "([^"]+)" attribute$/ |
||
507 | */ |
||
508 | public function productShouldNotHaveAttribute(ProductInterface $product, $attribute) |
||
517 | |||
518 | /** |
||
519 | * @Given product with :element :value should not be added |
||
520 | */ |
||
521 | public function productWithNameShouldNotBeAdded($element, $value) |
||
530 | |||
531 | /** |
||
532 | * @When I remove its name from :language translation |
||
533 | */ |
||
534 | public function iRemoveItsNameFromTranslation($language) |
||
543 | |||
544 | /** |
||
545 | * @Then /^this product should have (?:a|an) "([^"]+)" option$/ |
||
546 | */ |
||
547 | public function thisProductShouldHaveOption($productOption) |
||
551 | |||
552 | /** |
||
553 | * @Then the option field should be disabled |
||
554 | */ |
||
555 | public function theOptionFieldShouldBeDisabled() |
||
562 | |||
563 | /** |
||
564 | * @When /^I choose main (taxon "([^"]+)")$/ |
||
565 | */ |
||
566 | public function iChooseMainTaxon(TaxonInterface $taxon) |
||
575 | |||
576 | /** |
||
577 | * @Then /^the slug of the ("[^"]+" product) should(?:| still) be "([^"]+)"$/ |
||
578 | */ |
||
579 | public function productSlugShouldBe(ProductInterface $product, $slug) |
||
588 | |||
589 | /** |
||
590 | * @Then /^(this product) main taxon should be "([^"]+)"$/ |
||
591 | */ |
||
592 | public function thisProductMainTaxonShouldBe(ProductInterface $product, $taxonName) |
||
607 | |||
608 | /** |
||
609 | * @Then /^inventory of (this product) should not be tracked$/ |
||
610 | */ |
||
611 | public function thisProductShouldNotBeTracked(ProductInterface $product) |
||
620 | |||
621 | /** |
||
622 | * @Then /^inventory of (this product) should be tracked$/ |
||
623 | */ |
||
624 | public function thisProductShouldBeTracked(ProductInterface $product) |
||
633 | |||
634 | /** |
||
635 | * @When I attach the :path image with a code :code |
||
636 | */ |
||
637 | public function iAttachImageWithACode($path, $code) |
||
649 | |||
650 | /** |
||
651 | * @When I attach the :path image without a code |
||
652 | */ |
||
653 | public function iAttachImageWithoutACode($path) |
||
663 | |||
664 | /** |
||
665 | * @Then /^(this product) should have(?:| also) an image with a code "([^"]*)"$/ |
||
666 | * @Then /^the (product "[^"]+") should have(?:| also) an image with a code "([^"]*)"$/ |
||
667 | */ |
||
668 | public function thisProductShouldHaveAnImageWithCode(ProductInterface $product, $code) |
||
683 | |||
684 | /** |
||
685 | * @Then /^(this product) should not have(?:| also) an image with a code "([^"]*)"$/ |
||
686 | */ |
||
687 | public function thisProductShouldNotHaveAnImageWithCode(ProductInterface $product, $code) |
||
700 | |||
701 | /** |
||
702 | * @When I change the image with the :code code to :path |
||
703 | */ |
||
704 | public function iChangeItsImageToPathForTheCode($path, $code) |
||
714 | |||
715 | /** |
||
716 | * @When /^I remove(?:| also) an image with a code "([^"]*)"$/ |
||
717 | */ |
||
718 | public function iRemoveAnImageWithACode($code) |
||
728 | |||
729 | /** |
||
730 | * @When I remove the first image |
||
731 | */ |
||
732 | public function iRemoveTheFirstImage() |
||
742 | |||
743 | /** |
||
744 | * @Then /^(this product) should not have any images$/ |
||
745 | */ |
||
746 | public function thisProductShouldNotHaveImages(ProductInterface $product) |
||
762 | |||
763 | /** |
||
764 | * @Then the image code field should be disabled |
||
765 | */ |
||
766 | public function theImageCodeFieldShouldBeDisabled() |
||
779 | |||
780 | /** |
||
781 | * @Then I should be notified that the image with this code already exists |
||
782 | */ |
||
783 | public function iShouldBeNotifiedThatTheImageWithThisCodeAlreadyExists() |
||
787 | |||
788 | /** |
||
789 | * @Then I should be notified that an image code is required |
||
790 | */ |
||
791 | public function iShouldBeNotifiedThatAnImageCodeIsRequired() |
||
804 | |||
805 | /** |
||
806 | * @Then there should still be only one image in the :product product |
||
807 | */ |
||
808 | public function thereShouldStillBeOnlyOneImageInThisTaxon(ProductInterface $product) |
||
824 | |||
825 | /** |
||
826 | * @Then /^there should be no reviews of (this product)$/ |
||
827 | */ |
||
828 | public function thereAreNoProductReviews(ProductInterface $product) |
||
837 | |||
838 | /** |
||
839 | * @param string $element |
||
840 | * @param string $value |
||
841 | */ |
||
842 | private function assertElementValue($element, $value) |
||
859 | |||
860 | /** |
||
861 | * @param string $element |
||
862 | * @param string $message |
||
863 | */ |
||
864 | private function assertValidationMessage($element, $message) |
||
878 | } |
||
879 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.