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 |
||
| 41 | final class ManagingProductsContext implements Context |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @var SharedStorageInterface |
||
| 45 | */ |
||
| 46 | private $sharedStorage; |
||
| 47 | |||
| 48 | /**x |
||
| 49 | * @var CreateSimpleProductPageInterface |
||
| 50 | */ |
||
| 51 | private $createSimpleProductPage; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var CreateConfigurableProductPageInterface |
||
| 55 | */ |
||
| 56 | private $createConfigurableProductPage; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var IndexPageInterface |
||
| 60 | */ |
||
| 61 | private $indexPage; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var UpdateSimpleProductPageInterface |
||
| 65 | */ |
||
| 66 | private $updateSimpleProductPage; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var UpdateConfigurableProductPageInterface |
||
| 70 | */ |
||
| 71 | private $updateConfigurableProductPage; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var ProductReviewIndexPageInterface |
||
| 75 | */ |
||
| 76 | private $productReviewIndexPage; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var IndexPerTaxonPageInterface |
||
| 80 | */ |
||
| 81 | private $indexPerTaxonPage; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var CurrentPageResolverInterface |
||
| 85 | */ |
||
| 86 | private $currentPageResolver; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var NotificationCheckerInterface |
||
| 90 | */ |
||
| 91 | private $notificationChecker; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param SharedStorageInterface $sharedStorage |
||
| 95 | * @param CreateSimpleProductPageInterface $createSimpleProductPage |
||
| 96 | * @param CreateConfigurableProductPageInterface $createConfigurableProductPage |
||
| 97 | * @param IndexPageInterface $indexPage |
||
| 98 | * @param UpdateSimpleProductPageInterface $updateSimpleProductPage |
||
| 99 | * @param UpdateConfigurableProductPageInterface $updateConfigurableProductPage |
||
| 100 | * @param ProductReviewIndexPageInterface $productReviewIndexPage |
||
| 101 | * @param IndexPerTaxonPageInterface $indexPerTaxonPage |
||
| 102 | * @param CurrentPageResolverInterface $currentPageResolver |
||
| 103 | * @param NotificationCheckerInterface $notificationChecker |
||
| 104 | */ |
||
| 105 | public function __construct( |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @Given I want to create a new simple product |
||
| 131 | */ |
||
| 132 | public function iWantToCreateANewSimpleProduct() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @Given I want to create a new configurable product |
||
| 139 | */ |
||
| 140 | public function iWantToCreateANewConfigurableProduct() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @When I specify its code as :code |
||
| 147 | * @When I do not specify its code |
||
| 148 | */ |
||
| 149 | public function iSpecifyItsCodeAs($code = null) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @When I name it :name in :language |
||
| 158 | * @When I rename it to :name in :language |
||
| 159 | */ |
||
| 160 | public function iRenameItToIn($name, $language) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @When I add it |
||
| 169 | * @When I try to add it |
||
| 170 | */ |
||
| 171 | public function iAddIt() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @When I disable its inventory tracking |
||
| 181 | */ |
||
| 182 | public function iDisableItsTracking() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @When I enable its inventory tracking |
||
| 189 | */ |
||
| 190 | public function iEnableItsTracking() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @When /^I set its(?:| default) price to "(?:€|£|\$)([^"]+)" for "([^"]+)" channel$/ |
||
| 197 | */ |
||
| 198 | public function iSetItsPriceTo($price, $channelName) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @When /^I set its original price to "(?:€|£|\$)([^"]+)" for "([^"]+)" channel$/ |
||
| 205 | */ |
||
| 206 | public function iSetItsOriginalPriceTo($originalPrice, $channelName) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @When I make it available in channel :channel |
||
| 213 | */ |
||
| 214 | public function iMakeItAvailableInChannel($channel) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @When I assign it to channel :channel |
||
| 221 | */ |
||
| 222 | public function iAssignItToChannel($channel) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @When I choose :calculatorName calculator |
||
| 230 | */ |
||
| 231 | public function iChooseCalculator($calculatorName) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @When I set its slug to :slug |
||
| 238 | * @When I set its slug to :slug in :language |
||
| 239 | * @When I remove its slug |
||
| 240 | */ |
||
| 241 | public function iSetItsSlugToIn($slug = null, $language = 'en_US') |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @When I enable slug modification |
||
| 248 | * @When I enable slug modification in :localeCode |
||
| 249 | */ |
||
| 250 | public function iEnableSlugModification($localeCode = 'en_US') |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @Then the product :productName should appear in the store |
||
| 258 | * @Then the product :productName should be in the shop |
||
| 259 | * @Then this product should still be named :productName |
||
| 260 | */ |
||
| 261 | public function theProductShouldAppearInTheShop($productName) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @Given I am browsing products |
||
| 270 | * @When I want to browse products |
||
| 271 | */ |
||
| 272 | public function iWantToBrowseProducts() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @When /^I am browsing products from ("([^"]+)" taxon)$/ |
||
| 279 | */ |
||
| 280 | public function iAmBrowsingProductsFromTaxon(TaxonInterface $taxon) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @When I filter them by :taxonName taxon |
||
| 287 | */ |
||
| 288 | public function iFilterThemByTaxon($taxonName) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @Then I should( still) see a product with :field :value |
||
| 295 | */ |
||
| 296 | public function iShouldSeeProductWith($field, $value) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @Then I should not see any product with :field :value |
||
| 303 | */ |
||
| 304 | public function iShouldNotSeeAnyProductWith($field, $value) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @Then the first product on the list should have :field :value |
||
| 311 | */ |
||
| 312 | public function theFirstProductOnTheListShouldHave($field, $value) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @Then the last product on the list should have :field :value |
||
| 321 | */ |
||
| 322 | public function theLastProductOnTheListShouldHave($field, $value) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @When I switch the way products are sorted by :field |
||
| 331 | * @When I start sorting products by :field |
||
| 332 | * @Given the products are already sorted by :field |
||
| 333 | */ |
||
| 334 | public function iSortProductsBy($field) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @Then I should see :numberOfProducts products in the list |
||
| 341 | */ |
||
| 342 | public function iShouldSeeProductsInTheList($numberOfProducts) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @When I delete the :product product |
||
| 349 | * @When I try to delete the :product product |
||
| 350 | */ |
||
| 351 | public function iDeleteProduct(ProductInterface $product) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @Then /^(this product) should not exist in the product catalog$/ |
||
| 361 | */ |
||
| 362 | public function productShouldNotExist(ProductInterface $product) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @Then I should be notified that this product is in use and cannot be deleted |
||
| 371 | */ |
||
| 372 | public function iShouldBeNotifiedOfFailure() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @Then /^(this product) should still exist in the product catalog$/ |
||
| 382 | */ |
||
| 383 | public function productShouldExistInTheProductCatalog(ProductInterface $product) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @When I want to modify the :product product |
||
| 390 | * @When /^I want to modify (this product)$/ |
||
| 391 | */ |
||
| 392 | public function iWantToModifyAProduct(ProductInterface $product) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @Then the code field should be disabled |
||
| 406 | */ |
||
| 407 | public function theCodeFieldShouldBeDisabled() |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @Then the slug field should not be editable |
||
| 416 | * @Then the slug field in :localeCode (also )should not be editable |
||
| 417 | */ |
||
| 418 | public function theSlugFieldShouldNotBeEditable($localeCode = 'en_US') |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @Then this product name should be :name |
||
| 425 | */ |
||
| 426 | public function thisProductElementShouldBe($name) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @Then /^I should be notified that (code|name|slug) is required$/ |
||
| 433 | */ |
||
| 434 | public function iShouldBeNotifiedThatIsRequired($element) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @When I save my changes |
||
| 441 | * @When I try to save my changes |
||
| 442 | */ |
||
| 443 | public function iSaveMyChanges() |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @When /^I change its price to (?:€|£|\$)([^"]+) for "([^"]+)" channel$/ |
||
| 452 | */ |
||
| 453 | public function iChangeItsPriceTo($price, $channelName) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @When /^I change its original price to "(?:€|£|\$)([^"]+)" for "([^"]+)" channel$/ |
||
| 460 | */ |
||
| 461 | public function iChangeItsOriginalPriceTo($price, $channelName) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @Given I add the :optionName option to it |
||
| 468 | */ |
||
| 469 | public function iAddTheOptionToIt($optionName) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @When I set its :attribute attribute to :value |
||
| 476 | * @When I set its :attribute attribute to :value in :language |
||
| 477 | */ |
||
| 478 | public function iSetItsAttributeTo($attribute, $value, $language = 'en_US') |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @When I remove its :attribute attribute |
||
| 485 | * @When I remove its :attribute attribute from :language |
||
| 486 | */ |
||
| 487 | public function iRemoveItsAttribute($attribute, $language = 'en_US') |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @When I try to add new attributes |
||
| 494 | */ |
||
| 495 | public function iTryToAddNewAttributes() |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @When I do not want to have shipping required for this product |
||
| 502 | */ |
||
| 503 | public function iDoNotWantToHaveShippingRequiredForThisProduct() |
||
| 507 | |||
| 508 | /** |
||
| 509 | * @Then attribute :attributeName of product :product should be :value |
||
| 510 | * @Then attribute :attributeName of product :product should be :value in :language |
||
| 511 | */ |
||
| 512 | public function itsAttributeShouldBe($attributeName, ProductInterface $product, $value, $language = 'en_US') |
||
| 518 | |||
| 519 | /** |
||
| 520 | * @Then /^(product "[^"]+") should not have a "([^"]+)" attribute$/ |
||
| 521 | */ |
||
| 522 | public function productShouldNotHaveAttribute(ProductInterface $product, $attribute) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @Then /^product "[^"]+" should not have any attributes$/ |
||
| 531 | * @Then /^product "[^"]+" should have (\d+) attributes?$/ |
||
| 532 | */ |
||
| 533 | public function productShouldNotHaveAnyAttributes($count = 0) |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @Given product with :element :value should not be added |
||
| 540 | */ |
||
| 541 | public function productWithNameShouldNotBeAdded($element, $value) |
||
| 547 | |||
| 548 | /** |
||
| 549 | * @When I remove its name from :language translation |
||
| 550 | */ |
||
| 551 | public function iRemoveItsNameFromTranslation($language) |
||
| 557 | |||
| 558 | /** |
||
| 559 | * @Then /^this product should have (?:a|an) "([^"]+)" option$/ |
||
| 560 | */ |
||
| 561 | public function thisProductShouldHaveOption($productOption) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * @Then the option field should be disabled |
||
| 568 | */ |
||
| 569 | public function theOptionFieldShouldBeDisabled() |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @When /^I choose main (taxon "[^"]+")$/ |
||
| 576 | */ |
||
| 577 | public function iChooseMainTaxon(TaxonInterface $taxon) |
||
| 583 | |||
| 584 | /** |
||
| 585 | * @Then /^the slug of the ("[^"]+" product) should(?:| still) be "([^"]+)"$/ |
||
| 586 | * @Then /^the slug of the ("[^"]+" product) should(?:| still) be "([^"]+)" (in the "[^"]+" locale)$/ |
||
| 587 | */ |
||
| 588 | public function productSlugShouldBe(ProductInterface $product, $slug, $locale = 'en_US') |
||
| 594 | |||
| 595 | /** |
||
| 596 | * @Then /^(this product) main taxon should be "([^"]+)"$/ |
||
| 597 | */ |
||
| 598 | public function thisProductMainTaxonShouldBe(ProductInterface $product, $taxonName) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * @Then /^inventory of (this product) should not be tracked$/ |
||
| 608 | */ |
||
| 609 | public function thisProductShouldNotBeTracked(ProductInterface $product) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * @Then /^inventory of (this product) should be tracked$/ |
||
| 618 | */ |
||
| 619 | public function thisProductShouldBeTracked(ProductInterface $product) |
||
| 625 | |||
| 626 | /** |
||
| 627 | * @When I attach the :path image with :type type |
||
| 628 | * @When I attach the :path image |
||
| 629 | */ |
||
| 630 | public function iAttachImageWithType($path, $type = null) |
||
| 636 | |||
| 637 | /** |
||
| 638 | * @When I associate as :productAssociationType the :productName product |
||
| 639 | * @When I associate as :productAssociationType the :firstProductName and :secondProductName products |
||
| 640 | */ |
||
| 641 | public function iAssociateProductsAsProductAssociation( |
||
| 649 | |||
| 650 | /** |
||
| 651 | * @When I remove an associated product :productName from :productAssociationType |
||
| 652 | */ |
||
| 653 | public function iRemoveAnAssociatedProductFromProductAssociation( |
||
| 661 | |||
| 662 | /** |
||
| 663 | * @Then /^(?:this product|the product "[^"]+"|it) should(?:| also) have an image with "([^"]*)" type$/ |
||
| 664 | */ |
||
| 665 | public function thisProductShouldHaveAnImageWithType($type) |
||
| 671 | |||
| 672 | /** |
||
| 673 | * @Then /^(?:this product|it)(?:| also) should not have any images with "([^"]*)" type$/ |
||
| 674 | */ |
||
| 675 | public function thisProductShouldNotHaveAnyImagesWithType($code) |
||
| 681 | |||
| 682 | /** |
||
| 683 | * @When I change the image with the :type type to :path |
||
| 684 | */ |
||
| 685 | public function iChangeItsImageToPathForTheType($type, $path) |
||
| 691 | |||
| 692 | /** |
||
| 693 | * @When /^I(?:| also) remove an image with "([^"]*)" type$/ |
||
| 694 | */ |
||
| 695 | public function iRemoveAnImageWithType($code) |
||
| 701 | |||
| 702 | /** |
||
| 703 | * @When I remove the first image |
||
| 704 | */ |
||
| 705 | public function iRemoveTheFirstImage() |
||
| 711 | |||
| 712 | /** |
||
| 713 | * @When I change the first image type to :type |
||
| 714 | */ |
||
| 715 | public function iChangeTheFirstImageTypeTo($type) |
||
| 721 | |||
| 722 | /** |
||
| 723 | * @Then /^(this product) should not have any images$/ |
||
| 724 | */ |
||
| 725 | public function thisProductShouldNotHaveImages(ProductInterface $product) |
||
| 733 | |||
| 734 | /** |
||
| 735 | * @Then /^(this product) should(?:| still) have (?:only one|(\d+)) images?$/ |
||
| 736 | */ |
||
| 737 | public function thereShouldStillBeOnlyOneImageInThisProduct(ProductInterface $product, $count = 1) |
||
| 745 | |||
| 746 | /** |
||
| 747 | * @Then /^there should be no reviews of (this product)$/ |
||
| 748 | */ |
||
| 749 | public function thereAreNoProductReviews(ProductInterface $product) |
||
| 755 | |||
| 756 | /** |
||
| 757 | * @Then this product should( also) have an association :productAssociationType with product :productName |
||
| 758 | * @Then this product should( also) have an association :productAssociationType with products :firstProductName and :secondProductName |
||
| 759 | */ |
||
| 760 | public function theProductShouldHaveAnAssociationWithProducts( |
||
| 775 | |||
| 776 | /** |
||
| 777 | * @Then this product should not have an association :productAssociationType with product :productName |
||
| 778 | */ |
||
| 779 | public function theProductShouldNotHaveAnAssociationWithProduct( |
||
| 785 | |||
| 786 | /** |
||
| 787 | * @Then I should be notified that simple product code has to be unique |
||
| 788 | */ |
||
| 789 | public function iShouldBeNotifiedThatSimpleProductCodeHasToBeUnique() |
||
| 793 | |||
| 794 | /** |
||
| 795 | * @Then I should be notified that slug has to be unique |
||
| 796 | */ |
||
| 797 | public function iShouldBeNotifiedThatSlugHasToBeUnique() |
||
| 801 | |||
| 802 | /** |
||
| 803 | * @Then I should be notified that code has to be unique |
||
| 804 | */ |
||
| 805 | public function iShouldBeNotifiedThatCodeHasToBeUnique() |
||
| 809 | |||
| 810 | /** |
||
| 811 | * @Then I should be notified that price must be defined for every channel |
||
| 812 | */ |
||
| 813 | public function iShouldBeNotifiedThatPriceMustBeDefinedForEveryChannel() |
||
| 817 | |||
| 818 | /** |
||
| 819 | * @Then they should have order like :firstProductName, :secondProductName and :thirdProductName |
||
| 820 | */ |
||
| 821 | public function theyShouldHaveOrderLikeAnd(...$productNames) |
||
| 825 | |||
| 826 | /** |
||
| 827 | * @When I save my new configuration |
||
| 828 | */ |
||
| 829 | public function iSaveMyNewConfiguration() |
||
| 833 | |||
| 834 | /** |
||
| 835 | * @When I set the position of :productName to :position |
||
| 836 | */ |
||
| 837 | public function iSetThePositionOfTo($productName, $position) |
||
| 841 | |||
| 842 | /** |
||
| 843 | * @Then this product should( still) have slug :value in :language |
||
| 844 | */ |
||
| 845 | public function thisProductElementShouldHaveSlugIn($slug, $language) |
||
| 849 | |||
| 850 | /** |
||
| 851 | * @When I set its shipping category as :shippingCategoryName |
||
| 852 | */ |
||
| 853 | public function iSetItsShippingCategoryAs($shippingCategoryName) |
||
| 857 | |||
| 858 | /** |
||
| 859 | * @Then /^(it|this product) should be priced at (?:€|£|\$)([^"]+) for channel "([^"]+)"$/ |
||
| 860 | * @Then /^(product "[^"]+") should be priced at (?:€|£|\$)([^"]+) for channel "([^"]+)"$/ |
||
| 861 | */ |
||
| 862 | public function itShouldBePricedAtForChannel(ProductInterface $product, $price, $channelName) |
||
| 868 | |||
| 869 | /** |
||
| 870 | * @Then /^(its|this products) original price should be "(?:€|£|\$)([^"]+)" for channel "([^"]+)"$/ |
||
| 871 | */ |
||
| 872 | public function itsOriginalPriceForChannel(ProductInterface $product, $originalPrice, $channelName) |
||
| 881 | |||
| 882 | /** |
||
| 883 | * @Then /^(this product) should no longer have price for channel "([^"]+)"$/ |
||
| 884 | */ |
||
| 885 | public function thisProductShouldNoLongerHavePriceForChannel(ProductInterface $product, $channelName) |
||
| 899 | |||
| 900 | /** |
||
| 901 | * @Then I should be notified that I have to define product variants' prices for newly assigned channels first |
||
| 902 | */ |
||
| 903 | public function iShouldBeNotifiedThatIHaveToDefineProductVariantsPricesForNewlyAssignedChannelsFirst() |
||
| 910 | |||
| 911 | /** |
||
| 912 | * @Then /^the (product "[^"]+") should not have shipping required$/ |
||
| 913 | */ |
||
| 914 | public function theProductWithCodeShouldNotHaveShippingRequired(ProductInterface $product) |
||
| 920 | |||
| 921 | /** |
||
| 922 | * @param string $element |
||
| 923 | * @param string $value |
||
| 924 | */ |
||
| 925 | private function assertElementValue($element, $value) |
||
| 937 | |||
| 938 | /** |
||
| 939 | * @param string $element |
||
| 940 | * @param string $message |
||
| 941 | */ |
||
| 942 | private function assertValidationMessage($element, $message) |
||
| 949 | |||
| 950 | /** |
||
| 951 | * @return SymfonyPageInterface|IndexPageInterface|IndexPerTaxonPageInterface|CreateSimpleProductPageInterface|CreateConfigurableProductPageInterface|UpdateSimpleProductPageInterface|UpdateConfigurableProductPageInterface |
||
| 952 | */ |
||
| 953 | private function resolveCurrentPage() |
||
| 964 | } |
||
| 965 |