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 | * @Then the product :productName should appear in the shop |
||
| 215 | * @Then the product :productName should be in the shop |
||
| 216 | * @Then this product should still be named :productName |
||
| 217 | */ |
||
| 218 | public function theProductShouldAppearInTheShop($productName) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @Given I am browsing products |
||
| 230 | * @When I want to browse products |
||
| 231 | */ |
||
| 232 | public function iWantToBrowseProducts() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @When I filter them by :taxonName taxon |
||
| 239 | */ |
||
| 240 | public function iFilterThemByTaxon($taxonName) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @Then I should( still) see a product with :field :value |
||
| 247 | */ |
||
| 248 | public function iShouldSeeProductWith($field, $value) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @Then I should not see any product with :field :value |
||
| 258 | */ |
||
| 259 | public function iShouldNotSeeAnyProductWith($field, $value) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @Then the first product on the list should have :field :value |
||
| 269 | */ |
||
| 270 | public function theFirstProductOnTheListShouldHave($field, $value) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @When I switch the way products are sorted by :field |
||
| 283 | * @When I start sorting products by :field |
||
| 284 | * @Given the products are already sorted by :field |
||
| 285 | */ |
||
| 286 | public function iSortProductsBy($field) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @Then I should see :numberOfProducts products in the list |
||
| 293 | */ |
||
| 294 | public function iShouldSeeProductsInTheList($numberOfProducts) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @When I delete the :product product |
||
| 307 | * @When I try to delete the :product product |
||
| 308 | */ |
||
| 309 | public function iDeleteProduct(ProductInterface $product) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @Then /^(this product) should not exist in the product catalog$/ |
||
| 319 | */ |
||
| 320 | public function productShouldNotExist(ProductInterface $product) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @Then I should be notified that this product is in use and cannot be deleted |
||
| 332 | */ |
||
| 333 | public function iShouldBeNotifiedOfFailure() |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @Then /^(this product) should still exist in the product catalog$/ |
||
| 343 | */ |
||
| 344 | public function productShouldExistInTheProductCatalog(ProductInterface $product) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @When I want to modify the :product product |
||
| 351 | * @When /^I want to modify (this product)$/ |
||
| 352 | */ |
||
| 353 | public function iWantToModifyAProduct(ProductInterface $product) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @Then the code field should be disabled |
||
| 367 | */ |
||
| 368 | public function theCodeFieldShouldBeDisabled() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @Then /^this product price should be "(?:€|£|\$)([^"]+)"$/ |
||
| 384 | */ |
||
| 385 | public function thisProductPriceShouldBeEqualTo($price) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @Then this product name should be :name |
||
| 392 | */ |
||
| 393 | public function thisProductElementShouldBe($name) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @Then /^I should be notified that (code|name) is required$/ |
||
| 400 | */ |
||
| 401 | public function iShouldBeNotifiedThatIsRequired($element) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @Then I should be notified that price is required |
||
| 408 | */ |
||
| 409 | public function iShouldBeNotifiedThatPriceIsRequired() |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @When I save my changes |
||
| 416 | * @When I try to save my changes |
||
| 417 | */ |
||
| 418 | public function iSaveMyChanges() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @When /^I change its price to "(?:€|£|\$)([^"]+)"$/ |
||
| 433 | */ |
||
| 434 | public function iChangeItsPriceTo($price) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @Given I add the :optionName option to it |
||
| 441 | */ |
||
| 442 | public function iAddTheOptionToIt($optionName) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @When I set its :attribute attribute to :value |
||
| 449 | */ |
||
| 450 | public function iSetItsAttributeTo($attribute, $value) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @When I remove its :attribute attribute |
||
| 457 | */ |
||
| 458 | public function iRemoveItsAttribute($attribute) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @Then /^attribute "([^"]+)" of (product "[^"]+") should be "([^"]+)"$/ |
||
| 465 | */ |
||
| 466 | public function itsAttributeShouldBe($attribute, ProductInterface $product, $value) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @Then /^(product "[^"]+") should not have a "([^"]+)" attribute$/ |
||
| 479 | */ |
||
| 480 | public function productShouldNotHaveAttribute(ProductInterface $product, $attribute) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * @Given product with :element :value should not be added |
||
| 492 | */ |
||
| 493 | public function productWithNameShouldNotBeAdded($element, $value) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * @When I remove its name from :language translation |
||
| 505 | */ |
||
| 506 | public function iRemoveItsNameFromTranslation($language) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @Then /^this product should have (?:a|an) "([^"]+)" option$/ |
||
| 518 | */ |
||
| 519 | public function thisProductShouldHaveOption($productOption) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * @Then the option field should be disabled |
||
| 526 | */ |
||
| 527 | public function theOptionFieldShouldBeDisabled() |
||
| 534 | |||
| 535 | /** |
||
| 536 | * @When /^I choose main (taxon "([^"]+)")$/ |
||
| 537 | */ |
||
| 538 | public function iChooseMainTaxon(TaxonInterface $taxon) |
||
| 547 | |||
| 548 | /** |
||
| 549 | * @Then /^(this product) main taxon should be "([^"]+)"$/ |
||
| 550 | */ |
||
| 551 | public function thisProductMainTaxonShouldBe(ProductInterface $product, $taxonName) |
||
| 566 | |||
| 567 | /** |
||
| 568 | * @Then /^inventory of (this product) should not be tracked$/ |
||
| 569 | */ |
||
| 570 | public function thisProductShouldNotBeTracked(ProductInterface $product) |
||
| 579 | |||
| 580 | /** |
||
| 581 | * @Then /^inventory of (this product) should be tracked$/ |
||
| 582 | */ |
||
| 583 | public function thisProductShouldBeTracked(ProductInterface $product) |
||
| 592 | |||
| 593 | /** |
||
| 594 | * @When I attach the :path image with a code :code |
||
| 595 | */ |
||
| 596 | public function iAttachImageWithACode($path, $code) |
||
| 608 | |||
| 609 | /** |
||
| 610 | * @Then /^(this product) should have(?:| also) an image with a code "([^"]*)"$/ |
||
| 611 | * @Then /^the (product "[^"]+") should have(?:| also) an image with a code "([^"]*)"$/ |
||
| 612 | */ |
||
| 613 | public function thisProductShouldHaveAnImageWithCode(ProductInterface $product, $code) |
||
| 628 | |||
| 629 | /** |
||
| 630 | * @Then /^(this product) should not have(?:| also) an image with a code "([^"]*)"$/ |
||
| 631 | */ |
||
| 632 | public function thisProductShouldNotHaveAnImageWithCode(ProductInterface $product, $code) |
||
| 645 | |||
| 646 | /** |
||
| 647 | * @When I change the image with the :code code to :path |
||
| 648 | */ |
||
| 649 | public function iChangeItsImageToPathForTheCode($path, $code) |
||
| 659 | |||
| 660 | /** |
||
| 661 | * @When /^I remove(?:| also) an image with a code "([^"]*)"$/ |
||
| 662 | */ |
||
| 663 | public function iRemoveAnImageWithACode($code) |
||
| 673 | |||
| 674 | /** |
||
| 675 | * @When I remove the first image |
||
| 676 | */ |
||
| 677 | public function iRemoveTheFirstImage() |
||
| 687 | |||
| 688 | /** |
||
| 689 | * @Then this product should not have images |
||
| 690 | */ |
||
| 691 | public function thisProductShouldNotHaveImages() |
||
| 705 | |||
| 706 | /** |
||
| 707 | * @Then the image code field should be disabled |
||
| 708 | */ |
||
| 709 | public function theImageCodeFieldShouldBeDisabled() |
||
| 722 | |||
| 723 | /** |
||
| 724 | * @Then I should be notified that the image with this code already exists |
||
| 725 | */ |
||
| 726 | public function iShouldBeNotifiedThatTheImageWithThisCodeAlreadyExists() |
||
| 730 | |||
| 731 | /** |
||
| 732 | * @Then there should still be only one image in the :product product |
||
| 733 | */ |
||
| 734 | public function thereShouldStillBeOnlyOneImageInThisTaxon(ProductInterface $product) |
||
| 750 | |||
| 751 | /** |
||
| 752 | * @Then /^there should be no reviews of (this product)$/ |
||
| 753 | */ |
||
| 754 | public function thereAreNoProductReviews(ProductInterface $product) |
||
| 763 | |||
| 764 | /** |
||
| 765 | * @param string $element |
||
| 766 | * @param string $value |
||
| 767 | */ |
||
| 768 | private function assertElementValue($element, $value) |
||
| 785 | |||
| 786 | /** |
||
| 787 | * @param string $element |
||
| 788 | * @param string $message |
||
| 789 | */ |
||
| 790 | private function assertValidationMessage($element, $message) |
||
| 804 | } |
||
| 805 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.