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 | */ |
||
| 166 | public function iSetItsPriceTo($price) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @When I choose :calculatorName calculator |
||
| 173 | */ |
||
| 174 | public function iChooseCalculator($calculatorName) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @When /^I set its price to "(?:€|£|\$)([^"]+)" for ("[^"]+" currency) and ("[^"]+" channel)$/ |
||
| 181 | */ |
||
| 182 | public function iSetItsPriceToForCurrencyAndChannel($price, CurrencyInterface $currency, ChannelInterface $channel) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @When I set its :optionName option to :optionValue |
||
| 189 | */ |
||
| 190 | public function iSetItsOptionAs($optionName, $optionValue) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @Then the :productVariantCode variant of the :product product should appear in the shop |
||
| 197 | */ |
||
| 198 | public function theProductVariantShouldAppearInTheShop($productVariantCode, ProductInterface $product) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @When /^I (?:|want to )view all variants of (this product)$/ |
||
| 210 | * @When /^I view(?:| all) variants of the (product "[^"]+")$/ |
||
| 211 | */ |
||
| 212 | public function iWantToViewAllVariantsOfThisProduct(ProductInterface $product) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @Then I should see :numberOfProductVariants variants in the list |
||
| 219 | * @Then I should see :numberOfProductVariants variant in the list |
||
| 220 | */ |
||
| 221 | public function iShouldSeeProductVariantsInTheList($numberOfProductVariants) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @When /^I delete the ("[^"]+" variant of product "[^"]+")$/ |
||
| 234 | * @When /^I try to delete the ("[^"]+" variant of product "[^"]+")$/ |
||
| 235 | */ |
||
| 236 | public function iDeleteTheVariantOfProduct(ProductVariantInterface $productVariant) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @Then /^(this variant) should not exist in the product catalog$/ |
||
| 245 | */ |
||
| 246 | public function productVariantShouldNotExist(ProductVariantInterface $productVariant) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @Then I should be notified that this variant is in use and cannot be deleted |
||
| 258 | */ |
||
| 259 | public function iShouldBeNotifiedOfFailure() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @Then /^(this variant) should still exist in the product catalog$/ |
||
| 269 | */ |
||
| 270 | public function productShouldExistInTheProductCatalog(ProductVariantInterface $productVariant) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @When /^I want to modify the ("[^"]+" product variant)$/ |
||
| 277 | * @When /^I want to modify (this product variant)$/ |
||
| 278 | */ |
||
| 279 | public function iWantToModifyAProduct(ProductVariantInterface $productVariant) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @Then the code field should be disabled |
||
| 286 | */ |
||
| 287 | public function theCodeFieldShouldBeDisabled() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @Then I should be notified that price is required |
||
| 297 | */ |
||
| 298 | public function iShouldBeNotifiedThatPriceIsRequired() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @Then /^I should be notified that price is required for the (\d)(?:st|nd|rd|th) variant$/ |
||
| 305 | */ |
||
| 306 | public function iShouldBeNotifiedThatPriceIsRequiredForVariant($position) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @Then /^I should be notified that code is required for the (\d)(?:st|nd|rd|th) variant$/ |
||
| 313 | */ |
||
| 314 | public function iShouldBeNotifiedThatCodeIsRequiredForVariant($position) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @When I save my changes |
||
| 321 | * @When I try to save my changes |
||
| 322 | */ |
||
| 323 | public function iSaveMyChanges() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @When /^I change its price to "(?:€|£|\$)([^"]+)"$/ |
||
| 330 | */ |
||
| 331 | public function iChangeItsPriceTo($price) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @When I remove its name |
||
| 338 | */ |
||
| 339 | public function iRemoveItsNameFromTranslation() |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @Then /^the variant "([^"]+)" should have (\d+) items on hand$/ |
||
| 346 | */ |
||
| 347 | public function thisVariantShouldHaveItemsOnHand($productVariantName, $quantity) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @Then /^the "([^"]+)" variant of ("[^"]+" product) should have (\d+) items on hand$/ |
||
| 357 | */ |
||
| 358 | public function theVariantOfProductShouldHaveItemsOnHand($productVariantName, ProductInterface $product, $quantity) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @Then /^inventory of (this variant) should not be tracked$/ |
||
| 370 | */ |
||
| 371 | public function thisProductVariantShouldNotBeTracked(ProductVariantInterface $productVariant) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @Then /^inventory of (this variant) should be tracked$/ |
||
| 383 | */ |
||
| 384 | public function thisProductVariantShouldBeTracked(ProductVariantInterface $productVariant) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @Then /^I should see that the ("([^"]+)" variant) is not tracked$/ |
||
| 396 | */ |
||
| 397 | public function iShouldSeeThatIsNotTracked(ProductVariantInterface $productVariant) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @Then /^I should see that the ("[^"]+" variant) has zero on hand quantity$/ |
||
| 407 | */ |
||
| 408 | public function iShouldSeeThatTheVariantHasZeroOnHandQuantity(ProductVariantInterface $productVariant) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @Then /^(\d+) units of (this product) should be on hold$/ |
||
| 418 | */ |
||
| 419 | public function unitsOfThisProductShouldBeOnHold($quantity, ProductInterface $product) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @Then /^(\d+) units of (this product) should be on hand$/ |
||
| 429 | */ |
||
| 430 | public function unitsOfThisProductShouldBeOnHand($quantity, ProductInterface $product) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @Then /^there should be no units of (this product) on hold$/ |
||
| 450 | */ |
||
| 451 | public function thereShouldBeNoUnitsOfThisProductOnHold(ProductInterface $product) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @Then the :variant variant should have :amount items on hold |
||
| 461 | * @Then /^(this variant) should have (\d+) items on hold$/ |
||
| 462 | */ |
||
| 463 | public function thisVariantShouldHaveItemsOnHold(ProductVariantInterface $variant, $amount) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @Then the :variant variant of :product product should have :amount items on hold |
||
| 470 | */ |
||
| 471 | public function theVariantOfProductShouldHaveItemsOnHold(ProductVariantInterface $variant, ProductInterface $product, $amount) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @Then /^(variant with code "[^"]+") for ("[^"]+" currency) and ("[^"]+" channel) should have "(?:€|£|\$)([^"]+)"$/ |
||
| 480 | */ |
||
| 481 | public function theProductForCurrencyAndChannelShouldHave( |
||
| 494 | |||
| 495 | /** |
||
| 496 | * @Given /^I want to generate new variants for (this product)$/ |
||
| 497 | */ |
||
| 498 | public function iWantToGenerateNewVariantsForThisProduct(ProductInterface $product) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * @When I generate it |
||
| 505 | * @When I try to generate it |
||
| 506 | */ |
||
| 507 | public function iClickGenerate() |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @When /^I specify that the (\d)(?:st|nd|rd|th) variant is identified by ("[^"]+") code and costs "(?:€|£|\$)([^"]+)"$/ |
||
| 514 | */ |
||
| 515 | public function iSpecifyThereAreVariantsIdentifiedByCodeWithCost($nthVariant, $code, $price) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * @When /^I specify that the (\d)(?:st|nd|rd|th) variant is identified by ("[^"]+") code$/ |
||
| 523 | */ |
||
| 524 | public function iSpecifyThereAreVariantsIdentifiedByCode($nthVariant, $code) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @When /^I specify that the (\d)(?:st|nd|rd|th) variant costs "(?:€|£|\$)([^"]+)"$/ |
||
| 531 | */ |
||
| 532 | public function iSpecifyThereAreVariantsWithCost($nthVariant, $price) |
||
| 536 | |||
| 537 | /** |
||
| 538 | * @When /^I remove (\d)(?:st|nd|rd|th) variant from the list$/ |
||
| 539 | */ |
||
| 540 | public function iRemoveVariantFromTheList($nthVariant) |
||
| 544 | |||
| 545 | /** |
||
| 546 | * @Then I should be notified that it has been successfully generated |
||
| 547 | */ |
||
| 548 | public function iShouldBeNotifiedThatItHasBeenSuccessfullyGenerated() |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @param string $element |
||
| 555 | * @param $message |
||
| 556 | */ |
||
| 557 | private function assertValidationMessage($element, $message) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * @param int $expectedAmount |
||
| 567 | * @param ProductVariantInterface $variant |
||
| 568 | * |
||
| 569 | * @throws \InvalidArgumentException |
||
| 570 | */ |
||
| 571 | private function assertOnHoldQuantityOfVariant($expectedAmount, $variant) |
||
| 586 | } |
||
| 587 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.