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 |
||
| 33 | final class ManagingProductVariantsContext implements Context |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var SharedStorageInterface |
||
| 37 | */ |
||
| 38 | private $sharedStorage; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var DefaultProductVariantResolver |
||
| 42 | */ |
||
| 43 | private $defaultProductVariantResolver; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var CreatePageInterface |
||
| 47 | */ |
||
| 48 | private $createPage; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var IndexPageInterface |
||
| 52 | */ |
||
| 53 | private $indexPage; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var UpdatePageInterface |
||
| 57 | */ |
||
| 58 | private $updatePage; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var GeneratePageInterface |
||
| 62 | */ |
||
| 63 | private $generatePage; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var CurrentPageResolverInterface |
||
| 67 | */ |
||
| 68 | private $currentPageResolver; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var NotificationCheckerInterface |
||
| 72 | */ |
||
| 73 | private $notificationChecker; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param SharedStorageInterface $sharedStorage |
||
| 77 | * @param DefaultProductVariantResolver $defaultProductVariantResolver |
||
| 78 | * @param CreatePageInterface $createPage |
||
| 79 | * @param IndexPageInterface $indexPage |
||
| 80 | * @param UpdatePageInterface $updatePage |
||
| 81 | * @param GeneratePageInterface $generatePage |
||
| 82 | * @param CurrentPageResolverInterface $currentPageResolver |
||
| 83 | * @param NotificationCheckerInterface $notificationChecker |
||
| 84 | */ |
||
| 85 | public function __construct( |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @Given /^I want to create a new variant of (this product)$/ |
||
| 107 | */ |
||
| 108 | public function iWantToCreateANewProduct(ProductInterface $product) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @When I specify its code as :code |
||
| 115 | * @When I do not specify its code |
||
| 116 | */ |
||
| 117 | public function iSpecifyItsCodeAs($code = null) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @When I name it :name |
||
| 124 | */ |
||
| 125 | public function iNameItIn($name) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @When I rename it to :name |
||
| 132 | */ |
||
| 133 | public function iRenameItTo($name) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @When I add it |
||
| 140 | * @When I try to add it |
||
| 141 | */ |
||
| 142 | public function iAddIt() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @When I disable its inventory tracking |
||
| 149 | */ |
||
| 150 | public function iDisableItsTracking() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @When I enable its inventory tracking |
||
| 157 | */ |
||
| 158 | public function iEnableItsTracking() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @When /^I set its(?:| default) price to "(?:€|£|\$)([^"]+)" for "([^"]+)" channel$/ |
||
| 165 | * @When I do not set its price |
||
| 166 | */ |
||
| 167 | public function iSetItsPriceTo($price = null, $channel = null) |
||
| 168 | { |
||
| 169 | $this->createPage->specifyPrice($price, (null === $channel) ? $this->sharedStorage->get('channel') :$channel); |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @When I set its height, width, depth and weight to :number |
||
| 174 | */ |
||
| 175 | public function iSetItsDimensionsTo($value) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @When I do not specify its current stock |
||
| 182 | */ |
||
| 183 | public function iDoNetSetItsCurrentStockTo() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @When I choose :calculatorName calculator |
||
| 190 | */ |
||
| 191 | public function iChooseCalculator($calculatorName) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @When /^I set its price to "(?:€|£|\$)([^"]+)" for ("[^"]+" currency) and ("[^"]+" channel)$/ |
||
| 198 | */ |
||
| 199 | public function iSetItsPriceToForCurrencyAndChannel($price, CurrencyInterface $currency, ChannelInterface $channel) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @When I set its :optionName option to :optionValue |
||
| 206 | */ |
||
| 207 | public function iSetItsOptionAs($optionName, $optionValue) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @Then the :productVariantCode variant of the :product product should appear in the store |
||
| 214 | */ |
||
| 215 | public function theProductVariantShouldAppearInTheShop($productVariantCode, ProductInterface $product) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @Then the :productVariantCode variant of the :product product should not appear in the store |
||
| 227 | */ |
||
| 228 | public function theProductVariantShouldNotAppearInTheShop($productVariantCode, ProductInterface $product) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @Then the :product product should have no variants |
||
| 240 | */ |
||
| 241 | public function theProductShouldHaveNoVariants(ProductInterface $product) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @Then the :product product should have only one variant |
||
| 248 | */ |
||
| 249 | public function theProductShouldHaveOnlyOneVariant(ProductInterface $product) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @Then /^the (variant with code "[^"]+") should be priced at (?:€|£|\$)([^"]+) for channel "([^"]+)"$/ |
||
| 256 | */ |
||
| 257 | public function theVariantWithCodeShouldBePricedAtForChannel(ProductVariantInterface $productVariant, $price, $channelName) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @When /^I (?:|want to )view all variants of (this product)$/ |
||
| 269 | * @When /^I view(?:| all) variants of the (product "[^"]+")$/ |
||
| 270 | */ |
||
| 271 | public function iWantToViewAllVariantsOfThisProduct(ProductInterface $product) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @Then I should see :numberOfProductVariants variants in the list |
||
| 278 | * @Then I should see :numberOfProductVariants variant in the list |
||
| 279 | */ |
||
| 280 | public function iShouldSeeProductVariantsInTheList($numberOfProductVariants) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @When /^I delete the ("[^"]+" variant of product "[^"]+")$/ |
||
| 293 | * @When /^I try to delete the ("[^"]+" variant of product "[^"]+")$/ |
||
| 294 | */ |
||
| 295 | public function iDeleteTheVariantOfProduct(ProductVariantInterface $productVariant) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @Then /^(this variant) should not exist in the product catalog$/ |
||
| 304 | */ |
||
| 305 | public function productVariantShouldNotExist(ProductVariantInterface $productVariant) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @Then I should be notified that this variant is in use and cannot be deleted |
||
| 317 | */ |
||
| 318 | public function iShouldBeNotifiedOfFailure() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @Then /^(this variant) should still exist in the product catalog$/ |
||
| 328 | */ |
||
| 329 | public function productShouldExistInTheProductCatalog(ProductVariantInterface $productVariant) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @When /^I want to modify the ("[^"]+" product variant)$/ |
||
| 336 | * @When /^I want to modify (this product variant)$/ |
||
| 337 | */ |
||
| 338 | public function iWantToModifyAProduct(ProductVariantInterface $productVariant) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @Then the code field should be disabled |
||
| 345 | */ |
||
| 346 | public function theCodeFieldShouldBeDisabled() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @Then I should be notified that :element is required |
||
| 356 | */ |
||
| 357 | public function iShouldBeNotifiedThatIsRequired($element) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @Then I should be notified that code has to be unique |
||
| 364 | */ |
||
| 365 | public function iShouldBeNotifiedThatCodeHasToBeUnique() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @Then I should be notified that current stock is required |
||
| 372 | */ |
||
| 373 | public function iShouldBeNotifiedThatOnHandIsRequired() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @Then I should be notified that height, width, depth and weight cannot be lower than 0 |
||
| 380 | */ |
||
| 381 | public function iShouldBeNotifiedThatIsHeightWidthDepthWeightCannotBeLowerThan() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @Then I should be notified that price cannot be lower than 0 |
||
| 391 | */ |
||
| 392 | public function iShouldBeNotifiedThatPriceCannotBeLowerThen() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @Then I should be notified that this variant already exists |
||
| 402 | */ |
||
| 403 | public function iShouldBeNotifiedThatThisVariantAlreadyExists() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @Then /^I should be notified that code is required for the (\d)(?:st|nd|rd|th) variant$/ |
||
| 413 | */ |
||
| 414 | public function iShouldBeNotifiedThatCodeIsRequiredForVariant($position) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @Then /^I should be notified that prices in all channels must be defined for the (\d)(?:st|nd|rd|th) variant$/ |
||
| 424 | */ |
||
| 425 | public function iShouldBeNotifiedThatPricesInAllChannelsMustBeDefinedForTheVariant($position) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @Then I should be notified that prices in all channels must be defined |
||
| 435 | */ |
||
| 436 | public function iShouldBeNotifiedThatPricesInAllChannelsMustBeDefined() |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @When I save my changes |
||
| 446 | * @When I try to save my changes |
||
| 447 | */ |
||
| 448 | public function iSaveMyChanges() |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @When /^I change its price to "(?:€|£|\$)([^"]+)"$/ |
||
| 455 | */ |
||
| 456 | public function iChangeItsPriceTo($price) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @When I remove its name |
||
| 463 | */ |
||
| 464 | public function iRemoveItsNameFromTranslation() |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @Then /^the variant "([^"]+)" should have (\d+) items on hand$/ |
||
| 471 | */ |
||
| 472 | public function thisVariantShouldHaveItemsOnHand($productVariantName, $quantity) |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @Then /^the "([^"]+)" variant of ("[^"]+" product) should have (\d+) items on hand$/ |
||
| 482 | */ |
||
| 483 | public function theVariantOfProductShouldHaveItemsOnHand($productVariantName, ProductInterface $product, $quantity) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * @Then /^inventory of (this variant) should not be tracked$/ |
||
| 495 | */ |
||
| 496 | public function thisProductVariantShouldNotBeTracked(ProductVariantInterface $productVariant) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @Then /^inventory of (this variant) should be tracked$/ |
||
| 508 | */ |
||
| 509 | public function thisProductVariantShouldBeTracked(ProductVariantInterface $productVariant) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * @Then /^I should see that the ("([^"]+)" variant) is not tracked$/ |
||
| 521 | */ |
||
| 522 | public function iShouldSeeThatIsNotTracked(ProductVariantInterface $productVariant) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @Then /^I should see that the ("[^"]+" variant) has zero on hand quantity$/ |
||
| 532 | */ |
||
| 533 | public function iShouldSeeThatTheVariantHasZeroOnHandQuantity(ProductVariantInterface $productVariant) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @Then /^(\d+) units of (this product) should be on hold$/ |
||
| 543 | */ |
||
| 544 | public function unitsOfThisProductShouldBeOnHold($quantity, ProductInterface $product) |
||
| 551 | |||
| 552 | /** |
||
| 553 | * @Then /^(\d+) units of (this product) should be on hand$/ |
||
| 554 | */ |
||
| 555 | public function unitsOfThisProductShouldBeOnHand($quantity, ProductInterface $product) |
||
| 572 | |||
| 573 | /** |
||
| 574 | * @Then /^there should be no units of (this product) on hold$/ |
||
| 575 | */ |
||
| 576 | public function thereShouldBeNoUnitsOfThisProductOnHold(ProductInterface $product) |
||
| 583 | |||
| 584 | /** |
||
| 585 | * @Then the :variant variant should have :amount items on hold |
||
| 586 | * @Then /^(this variant) should have (\d+) items on hold$/ |
||
| 587 | */ |
||
| 588 | public function thisVariantShouldHaveItemsOnHold(ProductVariantInterface $variant, $amount) |
||
| 592 | |||
| 593 | /** |
||
| 594 | * @Then the :variant variant of :product product should have :amount items on hold |
||
| 595 | */ |
||
| 596 | public function theVariantOfProductShouldHaveItemsOnHold(ProductVariantInterface $variant, ProductInterface $product, $amount) |
||
| 602 | |||
| 603 | /** |
||
| 604 | * @Then /^(variant with code "[^"]+") for ("[^"]+" currency) and ("[^"]+" channel) should be priced at "(?:€|£|\$)([^"]+)"$/ |
||
| 605 | */ |
||
| 606 | public function theProductForCurrencyAndChannelShouldBePricedAt( |
||
| 619 | |||
| 620 | /** |
||
| 621 | * @Given /^I want to generate new variants for (this product)$/ |
||
| 622 | */ |
||
| 623 | public function iWantToGenerateNewVariantsForThisProduct(ProductInterface $product) |
||
| 627 | |||
| 628 | /** |
||
| 629 | * @When I generate it |
||
| 630 | * @When I try to generate it |
||
| 631 | */ |
||
| 632 | public function iClickGenerate() |
||
| 636 | |||
| 637 | /** |
||
| 638 | * @When /^I specify that the (\d)(?:st|nd|rd|th) variant is identified by ("[^"]+") code and costs "(?:€|£|\$)([^"]+)" in ("[^"]+") channel$/ |
||
| 639 | */ |
||
| 640 | public function iSpecifyThereAreVariantsIdentifiedByCodeWithCost($nthVariant, $code, $price, $channelName) |
||
| 645 | |||
| 646 | /** |
||
| 647 | * @When /^I specify that the (\d)(?:st|nd|rd|th) variant is identified by ("[^"]+") code$/ |
||
| 648 | */ |
||
| 649 | public function iSpecifyThereAreVariantsIdentifiedByCode($nthVariant, $code) |
||
| 653 | |||
| 654 | /** |
||
| 655 | * @When /^I specify that the (\d)(?:st|nd|rd|th) variant costs "(?:€|£|\$)([^"]+)" in ("[^"]+") channel$/ |
||
| 656 | */ |
||
| 657 | public function iSpecifyThereAreVariantsWithCost($nthVariant, $price, $channelName) |
||
| 661 | |||
| 662 | /** |
||
| 663 | * @When /^I remove (\d)(?:st|nd|rd|th) variant from the list$/ |
||
| 664 | */ |
||
| 665 | public function iRemoveVariantFromTheList($nthVariant) |
||
| 669 | |||
| 670 | /** |
||
| 671 | * @Then I should be notified that it has been successfully generated |
||
| 672 | */ |
||
| 673 | public function iShouldBeNotifiedThatItHasBeenSuccessfullyGenerated() |
||
| 677 | |||
| 678 | /** |
||
| 679 | * @When I set its shipping category as :shippingCategoryName |
||
| 680 | */ |
||
| 681 | public function iSetItsShippingCategoryAs($shippingCategoryName) |
||
| 685 | |||
| 686 | /** |
||
| 687 | * @param string $element |
||
| 688 | * @param $message |
||
| 689 | */ |
||
| 690 | private function assertValidationMessage($element, $message) |
||
| 697 | |||
| 698 | /** |
||
| 699 | * @param int $expectedAmount |
||
| 700 | * @param ProductVariantInterface $variant |
||
| 701 | * |
||
| 702 | * @throws \InvalidArgumentException |
||
| 703 | */ |
||
| 704 | private function assertOnHoldQuantityOfVariant($expectedAmount, $variant) |
||
| 719 | |||
| 720 | /** |
||
| 721 | * @param ProductInterface $product |
||
| 722 | * @param int $amount |
||
| 723 | */ |
||
| 724 | private function assertNumberOfVariantsOnProductPage(ProductInterface $product, $amount) |
||
| 730 | } |
||
| 731 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.