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 ("(?:-)?(?:€|£|\$)[^"]+")$/ |
||
| 165 | * @When I do not set its price |
||
| 166 | */ |
||
| 167 | public function iSetItsPriceTo($price = null) |
||
| 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 | * @When /^I (?:|want to )view all variants of (this product)$/ |
||
| 256 | * @When /^I view(?:| all) variants of the (product "[^"]+")$/ |
||
| 257 | */ |
||
| 258 | public function iWantToViewAllVariantsOfThisProduct(ProductInterface $product) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @Then I should see :numberOfProductVariants variants in the list |
||
| 265 | * @Then I should see :numberOfProductVariants variant in the list |
||
| 266 | */ |
||
| 267 | public function iShouldSeeProductVariantsInTheList($numberOfProductVariants) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @When /^I delete the ("[^"]+" variant of product "[^"]+")$/ |
||
| 280 | * @When /^I try to delete the ("[^"]+" variant of product "[^"]+")$/ |
||
| 281 | */ |
||
| 282 | public function iDeleteTheVariantOfProduct(ProductVariantInterface $productVariant) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @Then /^(this variant) should not exist in the product catalog$/ |
||
| 291 | */ |
||
| 292 | public function productVariantShouldNotExist(ProductVariantInterface $productVariant) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @Then I should be notified that this variant is in use and cannot be deleted |
||
| 304 | */ |
||
| 305 | public function iShouldBeNotifiedOfFailure() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @Then /^(this variant) should still exist in the product catalog$/ |
||
| 315 | */ |
||
| 316 | public function productShouldExistInTheProductCatalog(ProductVariantInterface $productVariant) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @When /^I want to modify the ("[^"]+" product variant)$/ |
||
| 323 | * @When /^I want to modify (this product variant)$/ |
||
| 324 | */ |
||
| 325 | public function iWantToModifyAProduct(ProductVariantInterface $productVariant) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @Then the code field should be disabled |
||
| 332 | */ |
||
| 333 | public function theCodeFieldShouldBeDisabled() |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @Then I should be notified that :element is required |
||
| 343 | */ |
||
| 344 | public function iShouldBeNotifiedThatIsRequired($element) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @Then I should be notified that code has to be unique |
||
| 351 | */ |
||
| 352 | public function iShouldBeNotifiedThatCodeHasToBeUnique() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @Then I should be notified that current stock is required |
||
| 359 | */ |
||
| 360 | public function iShouldBeNotifiedThatOnHandIsRequired() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @Then I should be notified that height, width, depth and weight cannot be lower than 0 |
||
| 367 | */ |
||
| 368 | public function iShouldBeNotifiedThatIsHeightWidthDepthWeightCannotBeLowerThan() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @Then I should be notified that :element cannot be lower than 0 |
||
| 378 | */ |
||
| 379 | public function iShouldBeNotifiedThatCannotBeLowerThen($element) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @Then I should be notified that this variant already exists |
||
| 386 | */ |
||
| 387 | public function iShouldBeNotifiedThatThisVariantAlreadyExists() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @Then /^I should be notified that (\w+) is required for the (\d)(?:st|nd|rd|th) variant$/ |
||
| 397 | */ |
||
| 398 | public function iShouldBeNotifiedThatIsRequiredForVariant($element, $position) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @When I save my changes |
||
| 405 | * @When I try to save my changes |
||
| 406 | */ |
||
| 407 | public function iSaveMyChanges() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @When /^I change its price to "(?:€|£|\$)([^"]+)"$/ |
||
| 414 | */ |
||
| 415 | public function iChangeItsPriceTo($price) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @When I remove its name |
||
| 422 | */ |
||
| 423 | public function iRemoveItsNameFromTranslation() |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @Then /^the variant "([^"]+)" should have (\d+) items on hand$/ |
||
| 430 | */ |
||
| 431 | public function thisVariantShouldHaveItemsOnHand($productVariantName, $quantity) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @Then /^the "([^"]+)" variant of ("[^"]+" product) should have (\d+) items on hand$/ |
||
| 441 | */ |
||
| 442 | public function theVariantOfProductShouldHaveItemsOnHand($productVariantName, ProductInterface $product, $quantity) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @Then /^inventory of (this variant) should not be tracked$/ |
||
| 454 | */ |
||
| 455 | public function thisProductVariantShouldNotBeTracked(ProductVariantInterface $productVariant) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * @Then /^inventory of (this variant) should be tracked$/ |
||
| 467 | */ |
||
| 468 | public function thisProductVariantShouldBeTracked(ProductVariantInterface $productVariant) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @Then /^I should see that the ("([^"]+)" variant) is not tracked$/ |
||
| 480 | */ |
||
| 481 | public function iShouldSeeThatIsNotTracked(ProductVariantInterface $productVariant) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @Then /^I should see that the ("[^"]+" variant) has zero on hand quantity$/ |
||
| 491 | */ |
||
| 492 | public function iShouldSeeThatTheVariantHasZeroOnHandQuantity(ProductVariantInterface $productVariant) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @Then /^(\d+) units of (this product) should be on hold$/ |
||
| 502 | */ |
||
| 503 | public function unitsOfThisProductShouldBeOnHold($quantity, ProductInterface $product) |
||
| 510 | |||
| 511 | /** |
||
| 512 | * @Then /^(\d+) units of (this product) should be on hand$/ |
||
| 513 | */ |
||
| 514 | public function unitsOfThisProductShouldBeOnHand($quantity, ProductInterface $product) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @Then /^there should be no units of (this product) on hold$/ |
||
| 534 | */ |
||
| 535 | public function thereShouldBeNoUnitsOfThisProductOnHold(ProductInterface $product) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @Then the :variant variant should have :amount items on hold |
||
| 545 | * @Then /^(this variant) should have (\d+) items on hold$/ |
||
| 546 | */ |
||
| 547 | public function thisVariantShouldHaveItemsOnHold(ProductVariantInterface $variant, $amount) |
||
| 551 | |||
| 552 | /** |
||
| 553 | * @Then the :variant variant of :product product should have :amount items on hold |
||
| 554 | */ |
||
| 555 | public function theVariantOfProductShouldHaveItemsOnHold(ProductVariantInterface $variant, ProductInterface $product, $amount) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * @Then /^(variant with code "[^"]+") for ("[^"]+" currency) and ("[^"]+" channel) should be priced at "(?:€|£|\$)([^"]+)"$/ |
||
| 564 | */ |
||
| 565 | public function theProductForCurrencyAndChannelShouldBePricedAt( |
||
| 578 | |||
| 579 | /** |
||
| 580 | * @Given /^I want to generate new variants for (this product)$/ |
||
| 581 | */ |
||
| 582 | public function iWantToGenerateNewVariantsForThisProduct(ProductInterface $product) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * @When I generate it |
||
| 589 | * @When I try to generate it |
||
| 590 | */ |
||
| 591 | public function iClickGenerate() |
||
| 595 | |||
| 596 | /** |
||
| 597 | * @When /^I specify that the (\d)(?:st|nd|rd|th) variant is identified by ("[^"]+") code and costs "(?:€|£|\$)([^"]+)"$/ |
||
| 598 | */ |
||
| 599 | public function iSpecifyThereAreVariantsIdentifiedByCodeWithCost($nthVariant, $code, $price) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * @When /^I specify that the (\d)(?:st|nd|rd|th) variant is identified by ("[^"]+") code$/ |
||
| 607 | */ |
||
| 608 | public function iSpecifyThereAreVariantsIdentifiedByCode($nthVariant, $code) |
||
| 612 | |||
| 613 | /** |
||
| 614 | * @When /^I specify that the (\d)(?:st|nd|rd|th) variant costs "(?:€|£|\$)([^"]+)"$/ |
||
| 615 | */ |
||
| 616 | public function iSpecifyThereAreVariantsWithCost($nthVariant, $price) |
||
| 620 | |||
| 621 | /** |
||
| 622 | * @When /^I remove (\d)(?:st|nd|rd|th) variant from the list$/ |
||
| 623 | */ |
||
| 624 | public function iRemoveVariantFromTheList($nthVariant) |
||
| 628 | |||
| 629 | /** |
||
| 630 | * @Then I should be notified that it has been successfully generated |
||
| 631 | */ |
||
| 632 | public function iShouldBeNotifiedThatItHasBeenSuccessfullyGenerated() |
||
| 636 | |||
| 637 | /** |
||
| 638 | * @param string $element |
||
| 639 | * @param $message |
||
| 640 | */ |
||
| 641 | private function assertValidationMessage($element, $message) |
||
| 648 | |||
| 649 | /** |
||
| 650 | * @param int $expectedAmount |
||
| 651 | * @param ProductVariantInterface $variant |
||
| 652 | * |
||
| 653 | * @throws \InvalidArgumentException |
||
| 654 | */ |
||
| 655 | private function assertOnHoldQuantityOfVariant($expectedAmount, $variant) |
||
| 670 | |||
| 671 | /** |
||
| 672 | * @param ProductInterface $product |
||
| 673 | * @param int $amount |
||
| 674 | */ |
||
| 675 | private function assertNumberOfVariantsOnProductPage(ProductInterface $product, $amount) |
||
| 681 | } |
||
| 682 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.