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 CreatePageInterface |
||
| 40 | */ |
||
| 41 | private $createPage; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var IndexPageInterface |
||
| 45 | */ |
||
| 46 | private $indexPage; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var UpdatePageInterface |
||
| 50 | */ |
||
| 51 | private $updatePage; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var GeneratePageInterface |
||
| 55 | */ |
||
| 56 | private $generatePage; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var CurrentPageResolverInterface |
||
| 60 | */ |
||
| 61 | private $currentPageResolver; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var NotificationCheckerInterface |
||
| 65 | */ |
||
| 66 | private $notificationChecker; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param SharedStorageInterface $sharedStorage |
||
| 70 | * @param CreatePageInterface $createPage |
||
| 71 | * @param IndexPageInterface $indexPage |
||
| 72 | * @param UpdatePageInterface $updatePage |
||
| 73 | * @param GeneratePageInterface $generatePage |
||
| 74 | * @param CurrentPageResolverInterface $currentPageResolver |
||
| 75 | * @param NotificationCheckerInterface $notificationChecker |
||
| 76 | */ |
||
| 77 | public function __construct( |
||
| 78 | SharedStorageInterface $sharedStorage, |
||
| 79 | CreatePageInterface $createPage, |
||
| 80 | IndexPageInterface $indexPage, |
||
| 81 | UpdatePageInterface $updatePage, |
||
| 82 | GeneratePageInterface $generatePage, |
||
| 83 | CurrentPageResolverInterface $currentPageResolver, |
||
| 84 | NotificationCheckerInterface $notificationChecker |
||
| 85 | ) { |
||
| 86 | $this->sharedStorage = $sharedStorage; |
||
| 87 | $this->createPage = $createPage; |
||
| 88 | $this->indexPage = $indexPage; |
||
| 89 | $this->updatePage = $updatePage; |
||
| 90 | $this->generatePage = $generatePage; |
||
| 91 | $this->currentPageResolver = $currentPageResolver; |
||
| 92 | $this->notificationChecker = $notificationChecker; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @Given /^I want to create a new variant of (this product)$/ |
||
| 97 | */ |
||
| 98 | public function iWantToCreateANewProduct(ProductInterface $product) |
||
| 99 | { |
||
| 100 | $this->createPage->open(['productId' => $product->getId()]); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @When I specify its code as :code |
||
| 105 | * @When I do not specify its code |
||
| 106 | */ |
||
| 107 | public function iSpecifyItsCodeAs($code = null) |
||
| 108 | { |
||
| 109 | $this->createPage->specifyCode($code); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @When I name it :name in :language |
||
| 114 | */ |
||
| 115 | public function iNameItIn($name, $language) |
||
| 116 | { |
||
| 117 | $this->createPage->nameItIn($name, $language); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @When I rename it to :name |
||
| 122 | */ |
||
| 123 | public function iRenameItTo($name) |
||
| 124 | { |
||
| 125 | $this->updatePage->nameIt($name); |
||
|
|
|||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @When I add it |
||
| 130 | * @When I try to add it |
||
| 131 | */ |
||
| 132 | public function iAddIt() |
||
| 133 | { |
||
| 134 | $this->createPage->create(); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @When I disable its inventory tracking |
||
| 139 | */ |
||
| 140 | public function iDisableItsTracking() |
||
| 141 | { |
||
| 142 | $this->updatePage->disableTracking(); |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @When I enable its inventory tracking |
||
| 147 | */ |
||
| 148 | public function iEnableItsTracking() |
||
| 149 | { |
||
| 150 | $this->updatePage->enableTracking(); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @When /^I set its(?:| default) price to "(?:€|£|\$)([^"]+)" for "([^"]+)" channel$/ |
||
| 155 | * @When I do not set its price |
||
| 156 | */ |
||
| 157 | public function iSetItsPriceTo($price = null, $channelName = null) |
||
| 158 | { |
||
| 159 | $this->createPage->specifyPrice($price, (null === $channelName) ? $this->sharedStorage->get('channel') :$channelName); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @When /^I set its original price to "(?:€|£|\$)([^"]+)" for "([^"]+)" channel$/ |
||
| 164 | */ |
||
| 165 | public function iSetItsOriginalPriceTo($originalPrice, $channelName) |
||
| 166 | { |
||
| 167 | $this->createPage->specifyOriginalPrice($originalPrice, $channelName); |
||
| 168 | } |
||
| 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 set the position of :name to :position |
||
| 204 | */ |
||
| 205 | public function iSetThePositionOfTo($name, $position) |
||
| 206 | { |
||
| 207 | $this->indexPage->setPosition($name, (int) $position); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @When I save my new configuration |
||
| 212 | */ |
||
| 213 | public function iSaveMyNewConfiguration() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @Then /^the (variant with code "[^"]+") should be priced at (?:€|£|\$)([^"]+) for channel "([^"]+)"$/ |
||
| 220 | */ |
||
| 221 | public function theVariantWithCodeShouldBePricedAtForChannel(ProductVariantInterface $productVariant, $price, $channelName) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @Then /^the (variant with code "[^"]+") should be named "([^"]+)" in ("([^"]+)" locale)$/ |
||
| 230 | */ |
||
| 231 | public function theVariantWithCodeShouldBeNamedIn(ProductVariantInterface $productVariant, $name, $language) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @Then /^the (variant with code "[^"]+") should have an original price of (?:€|£|\$)([^"]+) for channel "([^"]+)"$/ |
||
| 240 | */ |
||
| 241 | public function theVariantWithCodeShouldHaveAnOriginalPriceOfForChannel(ProductVariantInterface $productVariant, $originalPrice, $channelName) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @When /^I delete the ("[^"]+" variant of product "[^"]+")$/ |
||
| 253 | * @When /^I try to delete the ("[^"]+" variant of product "[^"]+")$/ |
||
| 254 | */ |
||
| 255 | public function iDeleteTheVariantOfProduct(ProductVariantInterface $productVariant) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @Then I should be notified that this variant is in use and cannot be deleted |
||
| 264 | */ |
||
| 265 | public function iShouldBeNotifiedOfFailure() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @When /^I want to modify the ("[^"]+" product variant)$/ |
||
| 275 | */ |
||
| 276 | public function iWantToModifyAProduct(ProductVariantInterface $productVariant) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @Then the code field should be disabled |
||
| 283 | */ |
||
| 284 | public function theCodeFieldShouldBeDisabled() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @Then I should be notified that :element is required |
||
| 291 | */ |
||
| 292 | public function iShouldBeNotifiedThatIsRequired($element) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @Then I should be notified that code has to be unique |
||
| 299 | */ |
||
| 300 | public function iShouldBeNotifiedThatCodeHasToBeUnique() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @Then I should be notified that current stock is required |
||
| 307 | */ |
||
| 308 | public function iShouldBeNotifiedThatOnHandIsRequired() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @Then I should be notified that height, width, depth and weight cannot be lower than 0 |
||
| 315 | */ |
||
| 316 | public function iShouldBeNotifiedThatIsHeightWidthDepthWeightCannotBeLowerThan() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @Then I should be notified that price cannot be lower than 0.01 |
||
| 326 | */ |
||
| 327 | public function iShouldBeNotifiedThatPriceCannotBeLowerThen() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @Then I should be notified that this variant already exists |
||
| 337 | */ |
||
| 338 | public function iShouldBeNotifiedThatThisVariantAlreadyExists() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @Then /^I should be notified that code is required for the (\d)(?:st|nd|rd|th) variant$/ |
||
| 348 | */ |
||
| 349 | public function iShouldBeNotifiedThatCodeIsRequiredForVariant($position) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @Then /^I should be notified that prices in all channels must be defined for the (\d)(?:st|nd|rd|th) variant$/ |
||
| 359 | */ |
||
| 360 | public function iShouldBeNotifiedThatPricesInAllChannelsMustBeDefinedForTheVariant($position) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @Then /^I should be notified that variant code must be unique within this product for the (\d)(?:st|nd|rd|th) variant$/ |
||
| 370 | */ |
||
| 371 | public function iShouldBeNotifiedThatVariantCodeMustBeUniqueWithinThisProductForYheVariant($position) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @Then I should be notified that prices in all channels must be defined |
||
| 381 | */ |
||
| 382 | public function iShouldBeNotifiedThatPricesInAllChannelsMustBeDefined() |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @When I save my changes |
||
| 392 | * @When I try to save my changes |
||
| 393 | */ |
||
| 394 | public function iSaveMyChanges() |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @When I remove its name |
||
| 401 | */ |
||
| 402 | public function iRemoveItsNameFromTranslation() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @Then /^inventory of (this variant) should not be tracked$/ |
||
| 409 | */ |
||
| 410 | public function thisProductVariantShouldNotBeTracked(ProductVariantInterface $productVariant) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @Then /^inventory of (this variant) should be tracked$/ |
||
| 419 | */ |
||
| 420 | public function thisProductVariantShouldBeTracked(ProductVariantInterface $productVariant) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @When /^I want to generate new variants for (this product)$/ |
||
| 429 | */ |
||
| 430 | public function iWantToGenerateNewVariantsForThisProduct(ProductInterface $product) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @When I generate it |
||
| 437 | * @When I try to generate it |
||
| 438 | */ |
||
| 439 | public function iClickGenerate() |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @When /^I specify that the (\d)(?:st|nd|rd|th) variant is identified by "([^"]+)" code and costs "(?:€|£|\$)([^"]+)" in ("[^"]+") channel$/ |
||
| 446 | */ |
||
| 447 | public function iSpecifyThereAreVariantsIdentifiedByCodeWithCost($nthVariant, $code, $price, $channelName) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @When /^I specify that the (\d)(?:st|nd|rd|th) variant is identified by "([^"]+)" code$/ |
||
| 455 | */ |
||
| 456 | public function iSpecifyThereAreVariantsIdentifiedByCode($nthVariant, $code) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @When /^I specify that the (\d)(?:st|nd|rd|th) variant costs "(?:€|£|\$)([^"]+)" in ("[^"]+") channel$/ |
||
| 463 | */ |
||
| 464 | public function iSpecifyThereAreVariantsWithCost($nthVariant, $price, $channelName) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @When /^I remove (\d)(?:st|nd|rd|th) variant from the list$/ |
||
| 471 | */ |
||
| 472 | public function iRemoveVariantFromTheList($nthVariant) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @Then I should be notified that it has been successfully generated |
||
| 479 | */ |
||
| 480 | public function iShouldBeNotifiedThatItHasBeenSuccessfullyGenerated() |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @When I set its shipping category as :shippingCategoryName |
||
| 487 | */ |
||
| 488 | public function iSetItsShippingCategoryAs($shippingCategoryName) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * @When I do not specify any information about variants |
||
| 495 | */ |
||
| 496 | public function iDoNotSpecifyAnyInformationAboutVariants() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @When I change its quantity of inventory to :amount |
||
| 503 | */ |
||
| 504 | public function iChangeItsQuantityOfInventoryTo($amount) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * @Then I should be notified that on hand quantity must be greater than the number of on hold units |
||
| 511 | */ |
||
| 512 | public function iShouldBeNotifiedThatOnHandQuantityMustBeGreaterThanTheNumberOfOnHoldUnits() |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @param string $element |
||
| 522 | * @param $message |
||
| 523 | */ |
||
| 524 | private function assertValidationMessage($element, $message) |
||
| 531 | } |
||
| 532 |
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.