Complex classes like ManagingPromotionsContext 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 ManagingPromotionsContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | final class ManagingPromotionsContext implements Context |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var SharedStorageInterface |
||
| 32 | */ |
||
| 33 | private $sharedStorage; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var IndexPageInterface |
||
| 37 | */ |
||
| 38 | private $indexPage; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var CreatePageInterface |
||
| 42 | */ |
||
| 43 | private $createPage; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var UpdatePageInterface |
||
| 47 | */ |
||
| 48 | private $updatePage; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var CurrentPageResolverInterface |
||
| 52 | */ |
||
| 53 | private $currentPageResolver; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var NotificationCheckerInterface |
||
| 57 | */ |
||
| 58 | private $notificationChecker; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param SharedStorageInterface $sharedStorage |
||
| 62 | * @param IndexPageInterface $indexPage |
||
| 63 | * @param CreatePageInterface $createPage |
||
| 64 | * @param UpdatePageInterface $updatePage |
||
| 65 | * @param CurrentPageResolverInterface $currentPageResolver |
||
| 66 | * @param NotificationCheckerInterface $notificationChecker |
||
| 67 | */ |
||
| 68 | public function __construct( |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @When I want to create a new promotion |
||
| 86 | */ |
||
| 87 | public function iWantToCreateANewPromotion() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @Given I want to browse promotions |
||
| 94 | * @When I browse promotions |
||
| 95 | */ |
||
| 96 | public function iWantToBrowsePromotions() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @When I specify its code as :code |
||
| 103 | * @When I do not specify its code |
||
| 104 | */ |
||
| 105 | public function iSpecifyItsCodeAs($code = null) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @When I name it :name |
||
| 112 | * @When I do not name it |
||
| 113 | * @When I remove its name |
||
| 114 | */ |
||
| 115 | public function iNameIt($name = null) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @When I remove its priority |
||
| 122 | */ |
||
| 123 | public function iRemoveItsPriority() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @Then the :promotionName promotion should appear in the registry |
||
| 130 | * @Then the :promotionName promotion should exist in the registry |
||
| 131 | * @Then this promotion should still be named :promotionName |
||
| 132 | * @Then promotion :promotionName should still exist in the registry |
||
| 133 | */ |
||
| 134 | public function thePromotionShouldAppearInTheRegistry($promotionName) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @When I add it |
||
| 143 | * @When I try to add it |
||
| 144 | */ |
||
| 145 | public function iAddIt() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @When I add the "Has at least one from taxons" rule configured with :firstTaxon |
||
| 152 | * @When I add the "Has at least one from taxons" rule configured with :firstTaxon and :secondTaxon |
||
| 153 | */ |
||
| 154 | public function iAddTheHasTaxonRuleConfiguredWith(...$taxons) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @When /^I add the "Total price of items from taxon" rule configured with "([^"]+)" taxon and (?:€|£|\$)([^"]+) amount for "([^"]+)" channel$/ |
||
| 165 | */ |
||
| 166 | public function iAddTheRuleConfiguredWith($taxonName, $amount, $channelName) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @When /^I add the "Item total" rule configured with (?:€|£|\$)([^"]+) amount for "([^"]+)" channel and (?:€|£|\$)([^"]+) amount for "([^"]+)" channel$/ |
||
| 175 | */ |
||
| 176 | public function iAddTheItemTotalRuleConfiguredWithTwoChannel( |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @When /^I add the "([^"]+)" action configured with amount of "(?:€|£|\$)([^"]+)" for "([^"]+)" channel$/ |
||
| 189 | */ |
||
| 190 | public function iAddTheActionConfiguredWithAmountForChannel($actionType, $amount, $channelName) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @When /^it is(?:| also) configured with amount of "(?:€|£|\$)([^"]+)" for "([^"]+)" channel$/ |
||
| 198 | */ |
||
| 199 | public function itIsConfiguredWithAmountForChannel($amount, $channelName) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @When /^I specify that on "([^"]+)" channel this action should be applied to items with price greater then "(?:€|£|\$)([^"]+)"$/ |
||
| 206 | */ |
||
| 207 | public function iAddAMinPriceFilterRangeForChannel($channelName, $minimum) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @When /^I specify that on "([^"]+)" channel this action should be applied to items with price lesser then "(?:€|£|\$)([^"]+)"$/ |
||
| 214 | */ |
||
| 215 | public function iAddAMaxPriceFilterRangeForChannel($channelName, $maximum) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @When /^I specify that on "([^"]+)" channel this action should be applied to items with price between "(?:€|£|\$)([^"]+)" and "(?:€|£|\$)([^"]+)"$/ |
||
| 222 | */ |
||
| 223 | public function iAddAMinMaxPriceFilterRangeForChannel($channelName, $minimum, $maximum) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @When I specify that this action should be applied to items from :taxonName category |
||
| 231 | */ |
||
| 232 | public function iSpecifyThatThisActionShouldBeAppliedToItemsFromCategory($taxonName) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @When /^I add the "([^"]+)" action configured with a percentage value of (?:|-)([^"]+)% for ("[^"]+" channel)$/ |
||
| 239 | */ |
||
| 240 | public function iAddTheActionConfiguredWithAPercentageValueForChannel($actionType, $percentage = null, $channelName) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @When /^I add the "([^"]+)" action configured with a percentage value of (?:|-)([^"]+)%$/ |
||
| 248 | * @When I add the :actionType action configured without a percentage value |
||
| 249 | */ |
||
| 250 | public function iAddTheActionConfiguredWithAPercentageValue($actionType, $percentage = null) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @When I add the "Customer group" rule for :customerGroupName group |
||
| 258 | */ |
||
| 259 | public function iAddTheCustomerGroupRuleConfiguredForGroup($customerGroupName) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @Then /^there should be (\d+) promotion(?:|s)$/ |
||
| 267 | */ |
||
| 268 | public function thereShouldBePromotion($number) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @Then /^(this promotion) should be coupon based$/ |
||
| 279 | */ |
||
| 280 | public function thisPromotionShouldBeCouponBased(PromotionInterface $promotion) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @Then /^I should be able to manage coupons for (this promotion)$/ |
||
| 287 | */ |
||
| 288 | public function iShouldBeAbleToManageCouponsForThisPromotion(PromotionInterface $promotion) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @Then I should be notified that :element is required |
||
| 295 | */ |
||
| 296 | public function iShouldBeNotifiedThatIsRequired($element) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @Then I should be notified that a :element value should be a numeric value |
||
| 303 | */ |
||
| 304 | public function iShouldBeNotifiedThatAMinimalValueShouldBeNumeric($element) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @Then I should be notified that promotion with this code already exists |
||
| 311 | */ |
||
| 312 | public function iShouldBeNotifiedThatPromotionWithThisCodeAlreadyExists() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @Then promotion with :element :name should not be added |
||
| 319 | */ |
||
| 320 | public function promotionWithElementValueShouldNotBeAdded($element, $name) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @Then there should still be only one promotion with :element :value |
||
| 329 | */ |
||
| 330 | public function thereShouldStillBeOnlyOnePromotionWith($element, $value) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @When I set its usage limit to :usageLimit |
||
| 339 | */ |
||
| 340 | public function iSetItsUsageLimitTo($usageLimit) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @Then the :promotion promotion should be available to be used only :usageLimit times |
||
| 349 | */ |
||
| 350 | public function thePromotionShouldBeAvailableToUseOnlyTimes(PromotionInterface $promotion, $usageLimit) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @When I make it exclusive |
||
| 359 | */ |
||
| 360 | public function iMakeItExclusive() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @Then the :promotion promotion should be exclusive |
||
| 369 | */ |
||
| 370 | public function thePromotionShouldBeExclusive(PromotionInterface $promotion) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @When I make it coupon based |
||
| 377 | */ |
||
| 378 | public function iMakeItCouponBased() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @Then the :promotion promotion should be coupon based |
||
| 387 | */ |
||
| 388 | public function thePromotionShouldBeCouponBased(PromotionInterface $promotion) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @When I make it applicable for the :channelName channel |
||
| 395 | */ |
||
| 396 | public function iMakeItApplicableForTheChannel($channelName) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @Then the :promotion promotion should be applicable for the :channelName channel |
||
| 405 | */ |
||
| 406 | public function thePromotionShouldBeApplicableForTheChannel(PromotionInterface $promotion, $channelName) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @Given I want to modify a :promotion promotion |
||
| 415 | * @Given /^I want to modify (this promotion)$/ |
||
| 416 | * @Then I should be able to modify a :promotion promotion |
||
| 417 | */ |
||
| 418 | public function iWantToModifyAPromotion(PromotionInterface $promotion) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @Then the code field should be disabled |
||
| 425 | */ |
||
| 426 | public function theCodeFieldShouldBeDisabled() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @When I save my changes |
||
| 433 | * @When I try to save my changes |
||
| 434 | */ |
||
| 435 | public function iSaveMyChanges() |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @When /^I delete a ("([^"]+)" promotion)$/ |
||
| 442 | * @When /^I try to delete a ("([^"]+)" promotion)$/ |
||
| 443 | */ |
||
| 444 | public function iDeletePromotion(PromotionInterface $promotion) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @Then /^(this promotion) should no longer exist in the promotion registry$/ |
||
| 454 | */ |
||
| 455 | public function promotionShouldNotExistInTheRegistry(PromotionInterface $promotion) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @Then I should be notified that it is in use and cannot be deleted |
||
| 464 | */ |
||
| 465 | public function iShouldBeNotifiedOfFailure() |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @When I make it available from :startsDate to :endsDate |
||
| 475 | */ |
||
| 476 | public function iMakeItAvailableFromTo(\DateTime $startsDate, \DateTime $endsDate) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @Then the :promotion promotion should be available from :startsDate to :endsDate |
||
| 486 | */ |
||
| 487 | public function thePromotionShouldBeAvailableFromTo(PromotionInterface $promotion, \DateTime $startsDate, \DateTime $endsDate) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @Then I should be notified that promotion cannot end before it start |
||
| 498 | */ |
||
| 499 | public function iShouldBeNotifiedThatPromotionCannotEndBeforeItsEvenStart() |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @Then I should be notified that this value should not be blank |
||
| 509 | */ |
||
| 510 | public function iShouldBeNotifiedThatThisValueShouldNotBeBlank() |
||
| 517 | |||
| 518 | /** |
||
| 519 | * @Then I should be notified that the maximum value of a percentage discount is 100% |
||
| 520 | */ |
||
| 521 | public function iShouldBeNotifiedThatTheMaximumValueOfAPercentageDiscountIs100() |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @Then I should be notified that a percentage discount value must be at least 0% |
||
| 531 | */ |
||
| 532 | public function iShouldBeNotifiedThatAPercentageDiscountValueMustBeAtLeast0() |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @Then the promotion :promotion should be used :usage time(s) |
||
| 542 | * @Then the promotion :promotion should not be used |
||
| 543 | */ |
||
| 544 | public function thePromotionShouldBeUsedTime(PromotionInterface $promotion, $usage = 0) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @When I add the "Contains product" rule configured with the :productName product |
||
| 555 | */ |
||
| 556 | public function iAddTheRuleConfiguredWithTheProduct($productName) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * @When I specify that this action should be applied to the :productName product |
||
| 564 | */ |
||
| 565 | public function iSpecifyThatThisActionShouldBeAppliedToTheProduct($productName) |
||
| 569 | |||
| 570 | /** |
||
| 571 | * @Then I should see :count promotions on the list |
||
| 572 | */ |
||
| 573 | public function iShouldSeePromotionsOnTheList($count) |
||
| 583 | |||
| 584 | /** |
||
| 585 | * @Then the first promotion on the list should have :field :value |
||
| 586 | */ |
||
| 587 | public function theFirstPromotionOnTheListShouldHave($field, $value) |
||
| 598 | |||
| 599 | /** |
||
| 600 | * @Then the last promotion on the list should have :field :value |
||
| 601 | */ |
||
| 602 | public function theLastPromotionOnTheListShouldHave($field, $value) |
||
| 613 | |||
| 614 | /** |
||
| 615 | * @Given the :promotion promotion should have priority :priority |
||
| 616 | */ |
||
| 617 | public function thePromotionsShouldHavePriority(PromotionInterface $promotion, $priority) |
||
| 623 | |||
| 624 | /** |
||
| 625 | * @param string $element |
||
| 626 | * @param string $expectedMessage |
||
| 627 | */ |
||
| 628 | private function assertFieldValidationMessage($element, $expectedMessage) |
||
| 635 | |||
| 636 | /** |
||
| 637 | * @param PromotionInterface $promotion |
||
| 638 | * @param string $field |
||
| 639 | */ |
||
| 640 | private function assertIfFieldIsTrue(PromotionInterface $promotion, $field) |
||
| 646 | } |
||
| 647 |
If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway: