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 in :language |
||
| 124 | */ |
||
| 125 | public function iNameItIn($name, $language) |
||
| 126 | { |
||
| 127 | $this->createPage->nameItIn($name, $language); |
||
| 128 | } |
||
| 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 :optionName option to :optionValue |
||
| 198 | */ |
||
| 199 | public function iSetItsOptionAs($optionName, $optionValue) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @Then the :productVariantCode variant of the :product product should appear in the store |
||
| 206 | */ |
||
| 207 | public function theProductVariantShouldAppearInTheShop($productVariantCode, ProductInterface $product) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @Then the :productVariantCode variant of the :product product should not appear in the store |
||
| 219 | */ |
||
| 220 | public function theProductVariantShouldNotAppearInTheShop($productVariantCode, ProductInterface $product) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @Then the :product product should have no variants |
||
| 232 | */ |
||
| 233 | public function theProductShouldHaveNoVariants(ProductInterface $product) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @Then the :product product should have only one variant |
||
| 240 | */ |
||
| 241 | public function theProductShouldHaveOnlyOneVariant(ProductInterface $product) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @Then /^the (variant with code "[^"]+") should be priced at (?:€|£|\$)([^"]+) for channel "([^"]+)"$/ |
||
| 248 | */ |
||
| 249 | public function theVariantWithCodeShouldBePricedAtForChannel(ProductVariantInterface $productVariant, $price, $channelName) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @Then /^the (variant with code "[^"]+") should be named "([^"]+)" in ("([^"]+)" locale)$/ |
||
| 261 | */ |
||
| 262 | public function theVariantWithCodeShouldBeNamedIn(ProductVariantInterface $productVariant, $name, $language) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @When /^I (?:|want to )view all variants of (this product)$/ |
||
| 271 | * @When /^I view(?:| all) variants of the (product "[^"]+")$/ |
||
| 272 | */ |
||
| 273 | public function iWantToViewAllVariantsOfThisProduct(ProductInterface $product) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @Then I should see :numberOfProductVariants variants in the list |
||
| 280 | * @Then I should see :numberOfProductVariants variant in the list |
||
| 281 | * @Then I should not see any variants in the list |
||
| 282 | */ |
||
| 283 | public function iShouldSeeProductVariantsInTheList($numberOfProductVariants = 0) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @When /^I delete the ("[^"]+" variant of product "[^"]+")$/ |
||
| 296 | * @When /^I try to delete the ("[^"]+" variant of product "[^"]+")$/ |
||
| 297 | */ |
||
| 298 | public function iDeleteTheVariantOfProduct(ProductVariantInterface $productVariant) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @Then /^(this variant) should not exist in the product catalog$/ |
||
| 307 | */ |
||
| 308 | public function productVariantShouldNotExist(ProductVariantInterface $productVariant) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @Then I should be notified that this variant is in use and cannot be deleted |
||
| 320 | */ |
||
| 321 | public function iShouldBeNotifiedOfFailure() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @Then /^(this variant) should still exist in the product catalog$/ |
||
| 331 | */ |
||
| 332 | public function productShouldExistInTheProductCatalog(ProductVariantInterface $productVariant) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @When /^I want to modify the ("[^"]+" product variant)$/ |
||
| 339 | */ |
||
| 340 | public function iWantToModifyAProduct(ProductVariantInterface $productVariant) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @Then the code field should be disabled |
||
| 347 | */ |
||
| 348 | public function theCodeFieldShouldBeDisabled() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @Then I should be notified that :element is required |
||
| 358 | */ |
||
| 359 | public function iShouldBeNotifiedThatIsRequired($element) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @Then I should be notified that code has to be unique |
||
| 366 | */ |
||
| 367 | public function iShouldBeNotifiedThatCodeHasToBeUnique() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @Then I should be notified that current stock is required |
||
| 374 | */ |
||
| 375 | public function iShouldBeNotifiedThatOnHandIsRequired() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @Then I should be notified that height, width, depth and weight cannot be lower than 0 |
||
| 382 | */ |
||
| 383 | public function iShouldBeNotifiedThatIsHeightWidthDepthWeightCannotBeLowerThan() |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @Then I should be notified that price cannot be lower than 0.01 |
||
| 393 | */ |
||
| 394 | public function iShouldBeNotifiedThatPriceCannotBeLowerThen() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @Then I should be notified that this variant already exists |
||
| 404 | */ |
||
| 405 | public function iShouldBeNotifiedThatThisVariantAlreadyExists() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @Then /^I should be notified that code is required for the (\d)(?:st|nd|rd|th) variant$/ |
||
| 415 | */ |
||
| 416 | public function iShouldBeNotifiedThatCodeIsRequiredForVariant($position) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @Then /^I should be notified that prices in all channels must be defined for the (\d)(?:st|nd|rd|th) variant$/ |
||
| 426 | */ |
||
| 427 | public function iShouldBeNotifiedThatPricesInAllChannelsMustBeDefinedForTheVariant($position) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @Then /^I should be notified that variant code must be unique within this product for the (\d)(?:st|nd|rd|th) variant$/ |
||
| 437 | */ |
||
| 438 | public function iShouldBeNotifiedThatVariantCodeMustBeUniqueWithinThisProductForYheVariant($position) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @Then I should be notified that prices in all channels must be defined |
||
| 448 | */ |
||
| 449 | public function iShouldBeNotifiedThatPricesInAllChannelsMustBeDefined() |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @When I save my changes |
||
| 459 | * @When I try to save my changes |
||
| 460 | */ |
||
| 461 | public function iSaveMyChanges() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @When I remove its name |
||
| 468 | */ |
||
| 469 | public function iRemoveItsNameFromTranslation() |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @Then /^the variant "([^"]+)" should have (\d+) items on hand$/ |
||
| 476 | */ |
||
| 477 | public function thisVariantShouldHaveItemsOnHand($productVariantName, $quantity) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @Then /^the "([^"]+)" variant of ("[^"]+" product) should have (\d+) items on hand$/ |
||
| 487 | */ |
||
| 488 | public function theVariantOfProductShouldHaveItemsOnHand($productVariantName, ProductInterface $product, $quantity) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @Then /^inventory of (this variant) should not be tracked$/ |
||
| 500 | */ |
||
| 501 | public function thisProductVariantShouldNotBeTracked(ProductVariantInterface $productVariant) |
||
| 510 | |||
| 511 | /** |
||
| 512 | * @Then /^inventory of (this variant) should be tracked$/ |
||
| 513 | */ |
||
| 514 | public function thisProductVariantShouldBeTracked(ProductVariantInterface $productVariant) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * @Then /^I should see that the ("([^"]+)" variant) is not tracked$/ |
||
| 526 | */ |
||
| 527 | public function iShouldSeeThatIsNotTracked(ProductVariantInterface $productVariant) |
||
| 534 | |||
| 535 | /** |
||
| 536 | * @Then /^I should see that the ("[^"]+" variant) has zero on hand quantity$/ |
||
| 537 | */ |
||
| 538 | public function iShouldSeeThatTheVariantHasZeroOnHandQuantity(ProductVariantInterface $productVariant) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * @Then /^(\d+) units of (this product) should be on hold$/ |
||
| 548 | */ |
||
| 549 | public function unitsOfThisProductShouldBeOnHold($quantity, ProductInterface $product) |
||
| 556 | |||
| 557 | /** |
||
| 558 | * @Then /^(\d+) units of (this product) should be on hand$/ |
||
| 559 | */ |
||
| 560 | public function unitsOfThisProductShouldBeOnHand($quantity, ProductInterface $product) |
||
| 577 | |||
| 578 | /** |
||
| 579 | * @Then /^there should be no units of (this product) on hold$/ |
||
| 580 | */ |
||
| 581 | public function thereShouldBeNoUnitsOfThisProductOnHold(ProductInterface $product) |
||
| 588 | |||
| 589 | /** |
||
| 590 | * @Then the :variant variant should have :amount items on hold |
||
| 591 | */ |
||
| 592 | public function thisVariantShouldHaveItemsOnHold(ProductVariantInterface $variant, $amount) |
||
| 596 | |||
| 597 | /** |
||
| 598 | * @Then the :variant variant of :product product should have :amount items on hold |
||
| 599 | */ |
||
| 600 | public function theVariantOfProductShouldHaveItemsOnHold(ProductVariantInterface $variant, ProductInterface $product, $amount) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * @When /^I want to generate new variants for (this product)$/ |
||
| 609 | */ |
||
| 610 | public function iWantToGenerateNewVariantsForThisProduct(ProductInterface $product) |
||
| 614 | |||
| 615 | /** |
||
| 616 | * @When I generate it |
||
| 617 | * @When I try to generate it |
||
| 618 | */ |
||
| 619 | public function iClickGenerate() |
||
| 623 | |||
| 624 | /** |
||
| 625 | * @When /^I specify that the (\d)(?:st|nd|rd|th) variant is identified by "([^"]+)" code and costs "(?:€|£|\$)([^"]+)" in ("[^"]+") channel$/ |
||
| 626 | */ |
||
| 627 | public function iSpecifyThereAreVariantsIdentifiedByCodeWithCost($nthVariant, $code, $price, $channelName) |
||
| 632 | |||
| 633 | /** |
||
| 634 | * @When /^I specify that the (\d)(?:st|nd|rd|th) variant is identified by "([^"]+)" code$/ |
||
| 635 | */ |
||
| 636 | public function iSpecifyThereAreVariantsIdentifiedByCode($nthVariant, $code) |
||
| 640 | |||
| 641 | /** |
||
| 642 | * @When /^I specify that the (\d)(?:st|nd|rd|th) variant costs "(?:€|£|\$)([^"]+)" in ("[^"]+") channel$/ |
||
| 643 | */ |
||
| 644 | public function iSpecifyThereAreVariantsWithCost($nthVariant, $price, $channelName) |
||
| 648 | |||
| 649 | /** |
||
| 650 | * @When /^I remove (\d)(?:st|nd|rd|th) variant from the list$/ |
||
| 651 | */ |
||
| 652 | public function iRemoveVariantFromTheList($nthVariant) |
||
| 656 | |||
| 657 | /** |
||
| 658 | * @Then I should be notified that it has been successfully generated |
||
| 659 | */ |
||
| 660 | public function iShouldBeNotifiedThatItHasBeenSuccessfullyGenerated() |
||
| 664 | |||
| 665 | /** |
||
| 666 | * @When I set its shipping category as :shippingCategoryName |
||
| 667 | */ |
||
| 668 | public function iSetItsShippingCategoryAs($shippingCategoryName) |
||
| 672 | |||
| 673 | /** |
||
| 674 | * @When I do not specify any information about variants |
||
| 675 | */ |
||
| 676 | public function iDoNotSpecifyAnyInformationAboutVariants() |
||
| 680 | |||
| 681 | /** |
||
| 682 | * @param string $element |
||
| 683 | * @param $message |
||
| 684 | */ |
||
| 685 | private function assertValidationMessage($element, $message) |
||
| 692 | |||
| 693 | /** |
||
| 694 | * @param int $expectedAmount |
||
| 695 | * @param ProductVariantInterface $variant |
||
| 696 | * |
||
| 697 | * @throws \InvalidArgumentException |
||
| 698 | */ |
||
| 699 | private function assertOnHoldQuantityOfVariant($expectedAmount, $variant) |
||
| 714 | |||
| 715 | /** |
||
| 716 | * @param ProductInterface $product |
||
| 717 | * @param int $amount |
||
| 718 | */ |
||
| 719 | private function assertNumberOfVariantsOnProductPage(ProductInterface $product, $amount) |
||
| 725 | } |
||
| 726 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.