Complex classes like ManagingProductVariantsContext 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 ManagingProductVariantsContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | final class ManagingProductVariantsContext implements Context |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var SharedStorageInterface |
||
| 35 | */ |
||
| 36 | private $sharedStorage; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var DefaultProductVariantResolver |
||
| 40 | */ |
||
| 41 | private $defaultProductVariantResolver; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var CreatePageInterface |
||
| 45 | */ |
||
| 46 | private $createPage; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var IndexPageInterface |
||
| 50 | */ |
||
| 51 | private $indexPage; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var UpdatePageInterface |
||
| 55 | */ |
||
| 56 | private $updatePage; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var GeneratePageInterface |
||
| 60 | */ |
||
| 61 | private $generatePage; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var CurrentPageResolverInterface |
||
| 65 | */ |
||
| 66 | private $currentPageResolver; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var NotificationCheckerInterface |
||
| 70 | */ |
||
| 71 | private $notificationChecker; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param SharedStorageInterface $sharedStorage |
||
| 75 | * @param DefaultProductVariantResolver $defaultProductVariantResolver |
||
| 76 | * @param CreatePageInterface $createPage |
||
| 77 | * @param IndexPageInterface $indexPage |
||
| 78 | * @param UpdatePageInterface $updatePage |
||
| 79 | * @param GeneratePageInterface $generatePage |
||
| 80 | * @param CurrentPageResolverInterface $currentPageResolver |
||
| 81 | * @param NotificationCheckerInterface $notificationChecker |
||
| 82 | */ |
||
| 83 | public function __construct( |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @Given /^I want to create a new variant of (this product)$/ |
||
| 105 | */ |
||
| 106 | public function iWantToCreateANewProduct(ProductInterface $product) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @When I specify its code as :code |
||
| 113 | * @When I do not specify its code |
||
| 114 | */ |
||
| 115 | public function iSpecifyItsCodeAs($code = null) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @When I name it :name in :language |
||
| 122 | */ |
||
| 123 | public function iNameItIn($name, $language) |
||
| 124 | { |
||
| 125 | $this->createPage->nameItIn($name, $language); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @When I rename it to :name |
||
| 130 | */ |
||
| 131 | public function iRenameItTo($name) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @When I add it |
||
| 138 | * @When I try to add it |
||
| 139 | */ |
||
| 140 | public function iAddIt() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @When I disable its inventory tracking |
||
| 147 | */ |
||
| 148 | public function iDisableItsTracking() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @When I enable its inventory tracking |
||
| 155 | */ |
||
| 156 | public function iEnableItsTracking() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @When /^I set its(?:| default) price to "(?:€|£|\$)([^"]+)" for "([^"]+)" channel$/ |
||
| 163 | * @When I do not set its price |
||
| 164 | */ |
||
| 165 | public function iSetItsPriceTo($price = null, $channel = null) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @When I set its height, width, depth and weight to :number |
||
| 172 | */ |
||
| 173 | public function iSetItsDimensionsTo($value) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @When I do not specify its current stock |
||
| 180 | */ |
||
| 181 | public function iDoNetSetItsCurrentStockTo() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @When I choose :calculatorName calculator |
||
| 188 | */ |
||
| 189 | public function iChooseCalculator($calculatorName) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @When I set its :optionName option to :optionValue |
||
| 196 | */ |
||
| 197 | public function iSetItsOptionAs($optionName, $optionValue) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @When I start sorting variants by :field |
||
| 204 | */ |
||
| 205 | public function iSortProductsBy($field) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @When I set the position of :name to :position |
||
| 212 | */ |
||
| 213 | public function iSetThePositionOfTo($name, $position) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @When I save my new configuration |
||
| 220 | */ |
||
| 221 | public function iSaveMyNewConfiguration() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @Then the :productVariantCode variant of the :product product should appear in the store |
||
| 228 | */ |
||
| 229 | public function theProductVariantShouldAppearInTheShop($productVariantCode, ProductInterface $product) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @Then the :productVariantCode variant of the :product product should not appear in the store |
||
| 241 | */ |
||
| 242 | public function theProductVariantShouldNotAppearInTheShop($productVariantCode, ProductInterface $product) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @Then the :product product should have no variants |
||
| 254 | */ |
||
| 255 | public function theProductShouldHaveNoVariants(ProductInterface $product) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @Then the :product product should have only one variant |
||
| 262 | */ |
||
| 263 | public function theProductShouldHaveOnlyOneVariant(ProductInterface $product) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @Then /^the (variant with code "[^"]+") should be priced at (?:€|£|\$)([^"]+) for channel "([^"]+)"$/ |
||
| 270 | */ |
||
| 271 | public function theVariantWithCodeShouldBePricedAtForChannel(ProductVariantInterface $productVariant, $price, $channelName) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @Then /^the (variant with code "[^"]+") should be named "([^"]+)" in ("([^"]+)" locale)$/ |
||
| 283 | */ |
||
| 284 | public function theVariantWithCodeShouldBeNamedIn(ProductVariantInterface $productVariant, $name, $language) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @When /^I (?:|want to )view all variants of (this product)$/ |
||
| 293 | * @When /^I view(?:| all) variants of the (product "[^"]+")$/ |
||
| 294 | */ |
||
| 295 | public function iWantToViewAllVariantsOfThisProduct(ProductInterface $product) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @Then I should see :numberOfProductVariants variants in the list |
||
| 302 | * @Then I should see :numberOfProductVariants variant in the list |
||
| 303 | * @Then I should not see any variants in the list |
||
| 304 | */ |
||
| 305 | public function iShouldSeeProductVariantsInTheList($numberOfProductVariants = 0) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @When /^I delete the ("[^"]+" variant of product "[^"]+")$/ |
||
| 318 | * @When /^I try to delete the ("[^"]+" variant of product "[^"]+")$/ |
||
| 319 | */ |
||
| 320 | public function iDeleteTheVariantOfProduct(ProductVariantInterface $productVariant) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @Then /^(this variant) should not exist in the product catalog$/ |
||
| 329 | */ |
||
| 330 | public function productVariantShouldNotExist(ProductVariantInterface $productVariant) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @Then I should be notified that this variant is in use and cannot be deleted |
||
| 342 | */ |
||
| 343 | public function iShouldBeNotifiedOfFailure() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @Then /^(this variant) should still exist in the product catalog$/ |
||
| 353 | */ |
||
| 354 | public function productShouldExistInTheProductCatalog(ProductVariantInterface $productVariant) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @When /^I want to modify the ("[^"]+" product variant)$/ |
||
| 361 | */ |
||
| 362 | public function iWantToModifyAProduct(ProductVariantInterface $productVariant) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @Then the code field should be disabled |
||
| 369 | */ |
||
| 370 | public function theCodeFieldShouldBeDisabled() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @Then I should be notified that :element is required |
||
| 380 | */ |
||
| 381 | public function iShouldBeNotifiedThatIsRequired($element) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @Then I should be notified that code has to be unique |
||
| 388 | */ |
||
| 389 | public function iShouldBeNotifiedThatCodeHasToBeUnique() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @Then I should be notified that current stock is required |
||
| 396 | */ |
||
| 397 | public function iShouldBeNotifiedThatOnHandIsRequired() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @Then I should be notified that height, width, depth and weight cannot be lower than 0 |
||
| 404 | */ |
||
| 405 | public function iShouldBeNotifiedThatIsHeightWidthDepthWeightCannotBeLowerThan() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @Then I should be notified that price cannot be lower than 0.01 |
||
| 415 | */ |
||
| 416 | public function iShouldBeNotifiedThatPriceCannotBeLowerThen() |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @Then I should be notified that this variant already exists |
||
| 426 | */ |
||
| 427 | public function iShouldBeNotifiedThatThisVariantAlreadyExists() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @Then /^I should be notified that code is required for the (\d)(?:st|nd|rd|th) variant$/ |
||
| 437 | */ |
||
| 438 | public function iShouldBeNotifiedThatCodeIsRequiredForVariant($position) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @Then /^I should be notified that prices in all channels must be defined for the (\d)(?:st|nd|rd|th) variant$/ |
||
| 448 | */ |
||
| 449 | public function iShouldBeNotifiedThatPricesInAllChannelsMustBeDefinedForTheVariant($position) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @Then /^I should be notified that variant code must be unique within this product for the (\d)(?:st|nd|rd|th) variant$/ |
||
| 459 | */ |
||
| 460 | public function iShouldBeNotifiedThatVariantCodeMustBeUniqueWithinThisProductForYheVariant($position) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @Then I should be notified that prices in all channels must be defined |
||
| 470 | */ |
||
| 471 | public function iShouldBeNotifiedThatPricesInAllChannelsMustBeDefined() |
||
| 478 | |||
| 479 | /** |
||
| 480 | * @When I save my changes |
||
| 481 | * @When I try to save my changes |
||
| 482 | */ |
||
| 483 | public function iSaveMyChanges() |
||
| 487 | |||
| 488 | /** |
||
| 489 | * @When I remove its name |
||
| 490 | */ |
||
| 491 | public function iRemoveItsNameFromTranslation() |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @Then /^the variant "([^"]+)" should have (\d+) items on hand$/ |
||
| 498 | */ |
||
| 499 | public function thisVariantShouldHaveItemsOnHand($productVariantName, $quantity) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @Then /^the "([^"]+)" variant of ("[^"]+" product) should have (\d+) items on hand$/ |
||
| 509 | */ |
||
| 510 | public function theVariantOfProductShouldHaveItemsOnHand($productVariantName, ProductInterface $product, $quantity) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @Then /^inventory of (this variant) should not be tracked$/ |
||
| 522 | */ |
||
| 523 | public function thisProductVariantShouldNotBeTracked(ProductVariantInterface $productVariant) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * @Then /^inventory of (this variant) should be tracked$/ |
||
| 535 | */ |
||
| 536 | public function thisProductVariantShouldBeTracked(ProductVariantInterface $productVariant) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * @Then /^I should see that the ("([^"]+)" variant) is not tracked$/ |
||
| 548 | */ |
||
| 549 | public function iShouldSeeThatIsNotTracked(ProductVariantInterface $productVariant) |
||
| 556 | |||
| 557 | /** |
||
| 558 | * @Then /^I should see that the ("[^"]+" variant) has zero on hand quantity$/ |
||
| 559 | */ |
||
| 560 | public function iShouldSeeThatTheVariantHasZeroOnHandQuantity(ProductVariantInterface $productVariant) |
||
| 567 | |||
| 568 | /** |
||
| 569 | * @Then /^(\d+) units of (this product) should be on hold$/ |
||
| 570 | */ |
||
| 571 | public function unitsOfThisProductShouldBeOnHold($quantity, ProductInterface $product) |
||
| 578 | |||
| 579 | /** |
||
| 580 | * @Then /^(\d+) units of (this product) should be on hand$/ |
||
| 581 | */ |
||
| 582 | public function unitsOfThisProductShouldBeOnHand($quantity, ProductInterface $product) |
||
| 599 | |||
| 600 | /** |
||
| 601 | * @Then /^there should be no units of (this product) on hold$/ |
||
| 602 | */ |
||
| 603 | public function thereShouldBeNoUnitsOfThisProductOnHold(ProductInterface $product) |
||
| 610 | |||
| 611 | /** |
||
| 612 | * @Then the :variant variant should have :amount items on hold |
||
| 613 | */ |
||
| 614 | public function thisVariantShouldHaveItemsOnHold(ProductVariantInterface $variant, $amount) |
||
| 618 | |||
| 619 | /** |
||
| 620 | * @Then the :variant variant of :product product should have :amount items on hold |
||
| 621 | */ |
||
| 622 | public function theVariantOfProductShouldHaveItemsOnHold(ProductVariantInterface $variant, ProductInterface $product, $amount) |
||
| 628 | |||
| 629 | /** |
||
| 630 | * @Then the first variant in the list should have :field :value |
||
| 631 | */ |
||
| 632 | public function theFirstVariantInTheListShouldHave($field, $value) |
||
| 642 | |||
| 643 | /** |
||
| 644 | * @Then the last variant in the list should have :field :value |
||
| 645 | */ |
||
| 646 | public function theLastVariantInTheListShouldHave($field, $value) |
||
| 657 | |||
| 658 | /** |
||
| 659 | * @When /^I want to generate new variants for (this product)$/ |
||
| 660 | */ |
||
| 661 | public function iWantToGenerateNewVariantsForThisProduct(ProductInterface $product) |
||
| 665 | |||
| 666 | /** |
||
| 667 | * @When I generate it |
||
| 668 | * @When I try to generate it |
||
| 669 | */ |
||
| 670 | public function iClickGenerate() |
||
| 674 | |||
| 675 | /** |
||
| 676 | * @When /^I specify that the (\d)(?:st|nd|rd|th) variant is identified by "([^"]+)" code and costs "(?:€|£|\$)([^"]+)" in ("[^"]+") channel$/ |
||
| 677 | */ |
||
| 678 | public function iSpecifyThereAreVariantsIdentifiedByCodeWithCost($nthVariant, $code, $price, $channelName) |
||
| 683 | |||
| 684 | /** |
||
| 685 | * @When /^I specify that the (\d)(?:st|nd|rd|th) variant is identified by "([^"]+)" code$/ |
||
| 686 | */ |
||
| 687 | public function iSpecifyThereAreVariantsIdentifiedByCode($nthVariant, $code) |
||
| 691 | |||
| 692 | /** |
||
| 693 | * @When /^I specify that the (\d)(?:st|nd|rd|th) variant costs "(?:€|£|\$)([^"]+)" in ("[^"]+") channel$/ |
||
| 694 | */ |
||
| 695 | public function iSpecifyThereAreVariantsWithCost($nthVariant, $price, $channelName) |
||
| 699 | |||
| 700 | /** |
||
| 701 | * @When /^I remove (\d)(?:st|nd|rd|th) variant from the list$/ |
||
| 702 | */ |
||
| 703 | public function iRemoveVariantFromTheList($nthVariant) |
||
| 707 | |||
| 708 | /** |
||
| 709 | * @Then I should be notified that it has been successfully generated |
||
| 710 | */ |
||
| 711 | public function iShouldBeNotifiedThatItHasBeenSuccessfullyGenerated() |
||
| 715 | |||
| 716 | /** |
||
| 717 | * @When I set its shipping category as :shippingCategoryName |
||
| 718 | */ |
||
| 719 | public function iSetItsShippingCategoryAs($shippingCategoryName) |
||
| 723 | |||
| 724 | /** |
||
| 725 | * @When I do not specify any information about variants |
||
| 726 | */ |
||
| 727 | public function iDoNotSpecifyAnyInformationAboutVariants() |
||
| 731 | |||
| 732 | /** |
||
| 733 | * @When I change its quantity of inventory to :amount |
||
| 734 | */ |
||
| 735 | public function iChangeItsQuantityOfInventoryTo($amount) |
||
| 739 | |||
| 740 | /** |
||
| 741 | * @Then /^(this variant) should have a (\d+) item currently in stock$/ |
||
| 742 | */ |
||
| 743 | public function thisVariantShouldHaveAItemCurrentlyInStock(ProductVariantInterface $productVariant, $amountInStock) |
||
| 752 | |||
| 753 | /** |
||
| 754 | * @Then I should be notified that on hand quantity must be greater than the number of on hold units |
||
| 755 | */ |
||
| 756 | public function iShouldBeNotifiedThatOnHandQuantityMustBeGreaterThanTheNumberOfOnHoldUnits() |
||
| 763 | |||
| 764 | /** |
||
| 765 | * @param string $element |
||
| 766 | * @param $message |
||
| 767 | */ |
||
| 768 | private function assertValidationMessage($element, $message) |
||
| 775 | |||
| 776 | /** |
||
| 777 | * @param int $expectedAmount |
||
| 778 | * @param ProductVariantInterface $variant |
||
| 779 | * |
||
| 780 | * @throws \InvalidArgumentException |
||
| 781 | */ |
||
| 782 | private function assertOnHoldQuantityOfVariant($expectedAmount, $variant) |
||
| 797 | |||
| 798 | /** |
||
| 799 | * @param ProductInterface $product |
||
| 800 | * @param int $amount |
||
| 801 | */ |
||
| 802 | private function assertNumberOfVariantsOnProductPage(ProductInterface $product, $amount) |
||
| 808 | } |
||
| 809 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.