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 |
||
| 29 | final class ManagingPromotionsContext implements Context |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * @var SharedStorageInterface |
||
| 33 | */ |
||
| 34 | private $sharedStorage; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var IndexPageInterface |
||
| 38 | */ |
||
| 39 | private $indexPage; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var CreatePageInterface |
||
| 43 | */ |
||
| 44 | private $createPage; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var UpdatePageInterface |
||
| 48 | */ |
||
| 49 | private $updatePage; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var CurrentPageResolverInterface |
||
| 53 | */ |
||
| 54 | private $currentPageResolver; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var NotificationCheckerInterface |
||
| 58 | */ |
||
| 59 | private $notificationChecker; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var CurrencyProviderInterface |
||
| 63 | */ |
||
| 64 | private $currencyProvider; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param SharedStorageInterface $sharedStorage |
||
| 68 | * @param IndexPageInterface $indexPage |
||
| 69 | * @param CreatePageInterface $createPage |
||
| 70 | * @param UpdatePageInterface $updatePage |
||
| 71 | * @param CurrentPageResolverInterface $currentPageResolver |
||
| 72 | * @param NotificationCheckerInterface $notificationChecker |
||
| 73 | * @param CurrencyProviderInterface $currencyProvider |
||
| 74 | */ |
||
| 75 | public function __construct( |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @When I want to create a new promotion |
||
| 95 | */ |
||
| 96 | public function iWantToCreateANewPromotion() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @Given I want to browse promotions |
||
| 103 | * @When I browse promotions |
||
| 104 | */ |
||
| 105 | public function iWantToBrowsePromotions() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @When I specify its code as :code |
||
| 112 | * @When I do not specify its code |
||
| 113 | */ |
||
| 114 | public function iSpecifyItsCodeAs($code = null) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @When I name it :name |
||
| 121 | * @When I do not name it |
||
| 122 | * @When I remove its name |
||
| 123 | */ |
||
| 124 | public function iNameIt($name = null) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @Then the :promotionName promotion should appear in the registry |
||
| 131 | * @Then the :promotionName promotion should exist in the registry |
||
| 132 | * @Then this promotion should still be named :promotionName |
||
| 133 | * @Then promotion :promotionName should still exist in the registry |
||
| 134 | */ |
||
| 135 | public function thePromotionShouldAppearInTheRegistry($promotionName) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @When I add it |
||
| 147 | * @When I try to add it |
||
| 148 | */ |
||
| 149 | public function iAddIt() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @When I add the "Has at least one from taxons" rule configured with :firstTaxon |
||
| 156 | * @When I add the "Has at least one from taxons" rule configured with :firstTaxon and :secondTaxon |
||
| 157 | */ |
||
| 158 | public function iAddTheHasTaxonRuleConfiguredWith(...$taxons) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @When /^I add the "Total price of items from taxon" rule configured with "([^"]+)" taxon and (?:€|£|\$)([^"]+) amount for "([^"]+)" channel$/ |
||
| 169 | */ |
||
| 170 | public function iAddTheRuleConfiguredWith($taxonName, $amount, $channelName) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @When /^I add the "Item total" rule configured with (?:€|£|\$)([^"]+) amount for "([^"]+)" channel and (?:€|£|\$)([^"]+) amount for "([^"]+)" channel$/ |
||
| 179 | */ |
||
| 180 | public function iAddTheItemTotalRuleConfiguredWithTwoChannel( |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @When /^I add the "([^"]+)" action configured with amount of "(?:€|£|\$)([^"]+)" for "([^"]+)" channel$/ |
||
| 193 | */ |
||
| 194 | public function iAddTheActionConfiguredWithAmountForChannel($actionType, $amount, $channelName) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @When I add the :actionType action |
||
| 202 | */ |
||
| 203 | public function iAddTheAction($actionType) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @When /^it is(?:| also) configured with amount of "(?:€|£|\$)([^"]+)" for "([^"]+)" channel$/ |
||
| 210 | */ |
||
| 211 | public function itIsConfiguredWithAmountForChannel($amount, $channelName) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @When /^I specify that on "([^"]+)" channel this action should be applied to items with price greater then "(?:€|£|\$)([^"]+)"$/ |
||
| 218 | */ |
||
| 219 | public function iAddAMinPriceFilterRangeForChannel($channelName, $minimum) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @When /^I specify that on "([^"]+)" channel this action should be applied to items with price lesser then "(?:€|£|\$)([^"]+)"$/ |
||
| 226 | */ |
||
| 227 | public function iAddAMaxPriceFilterRangeForChannel($channelName, $maximum) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @When /^I specify that on "([^"]+)" channel this action should be applied to items with price between "(?:€|£|\$)([^"]+)" and "(?:€|£|\$)([^"]+)"$/ |
||
| 234 | */ |
||
| 235 | public function iAddAMinMaxPriceFilterRangeForChannel($channelName, $minimum, $maximum) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @When I specify that this action should be applied to items from :taxonName category |
||
| 243 | */ |
||
| 244 | public function iSpecifyThatThisActionShouldBeAppliedToItemsFromCategory($taxonName) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @When /^I add the "([^"]+)" action configured with a percentage value of (?:|-)([^"]+)% for ("[^"]+" channel)$/ |
||
| 251 | * @When I add the :actionType action configured without a percentage value for :channelName channel |
||
| 252 | */ |
||
| 253 | public function iAddTheActionConfiguredWithAPercentageValueForChannel($actionType, $percentage = null, $channelName) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @When /^I add the "([^"]+)" action configured with a percentage value of (?:|-)([^"]+)%$/ |
||
| 261 | * @When I add the :actionType action configured without a percentage value |
||
| 262 | */ |
||
| 263 | public function iAddTheActionConfiguredWithAPercentageValue($actionType, $percentage = null) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @Then /^there should be (\d+) promotion(?:|s)$/ |
||
| 271 | */ |
||
| 272 | public function thereShouldBePromotion($number) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @Then /^(this promotion) should be coupon based$/ |
||
| 283 | */ |
||
| 284 | public function thisPromotionShouldBeCouponBased(PromotionInterface $promotion) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @Then /^I should be able to manage coupons for (this promotion)$/ |
||
| 294 | */ |
||
| 295 | public function iShouldBeAbleToManageCouponsForThisPromotion(PromotionInterface $promotion) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @Then I should be notified that :element is required |
||
| 305 | */ |
||
| 306 | public function iShouldBeNotifiedThatIsRequired($element) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @Then I should be notified that a :element value should be a numeric value |
||
| 313 | */ |
||
| 314 | public function iShouldBeNotifiedThatAMinimalValueShouldBeNumeric($element) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @Then I should be notified that promotion with this code already exists |
||
| 321 | */ |
||
| 322 | public function iShouldBeNotifiedThatPromotionWithThisCodeAlreadyExists() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @Then promotion with :element :name should not be added |
||
| 329 | */ |
||
| 330 | public function promotionWithElementValueShouldNotBeAdded($element, $name) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @Then there should still be only one promotion with :element :value |
||
| 342 | */ |
||
| 343 | public function thereShouldStillBeOnlyOnePromotionWith($element, $value) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @When I set its usage limit to :usageLimit |
||
| 355 | */ |
||
| 356 | public function iSetItsUsageLimitTo($usageLimit) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @Then the :promotion promotion should be available to be used only :usageLimit times |
||
| 365 | */ |
||
| 366 | public function thePromotionShouldBeAvailableToUseOnlyTimes(PromotionInterface $promotion, $usageLimit) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @When I make it exclusive |
||
| 378 | */ |
||
| 379 | public function iMakeItExclusive() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @Then the :promotion promotion should be exclusive |
||
| 388 | */ |
||
| 389 | public function thePromotionShouldBeExclusive(PromotionInterface $promotion) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @When I make it coupon based |
||
| 396 | */ |
||
| 397 | public function iMakeItCouponBased() |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @Then the :promotion promotion should be coupon based |
||
| 406 | */ |
||
| 407 | public function thePromotionShouldBeCouponBased(PromotionInterface $promotion) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @When I make it applicable for the :channelName channel |
||
| 414 | */ |
||
| 415 | public function iMakeItApplicableForTheChannel($channelName) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @Then the :promotion promotion should be applicable for the :channelName channel |
||
| 424 | */ |
||
| 425 | public function thePromotionShouldBeApplicableForTheChannel(PromotionInterface $promotion, $channelName) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @Given I want to modify a :promotion promotion |
||
| 437 | * @Given /^I want to modify (this promotion)$/ |
||
| 438 | */ |
||
| 439 | public function iWantToModifyAPromotion(PromotionInterface $promotion) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @Then the code field should be disabled |
||
| 446 | */ |
||
| 447 | public function theCodeFieldShouldBeDisabled() |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @When I save my changes |
||
| 457 | * @When I try to save my changes |
||
| 458 | */ |
||
| 459 | public function iSaveMyChanges() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @When /^I delete a ("([^"]+)" promotion)$/ |
||
| 466 | * @When /^I try to delete a ("([^"]+)" promotion)$/ |
||
| 467 | */ |
||
| 468 | public function iDeletePromotion(PromotionInterface $promotion) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @Then /^(this promotion) should no longer exist in the promotion registry$/ |
||
| 478 | */ |
||
| 479 | public function promotionShouldNotExistInTheRegistry(PromotionInterface $promotion) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @Then I should be notified that it is in use and cannot be deleted |
||
| 491 | */ |
||
| 492 | public function iShouldBeNotifiedOfFailure() |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @When I make it available from :startsDate to :endsDate |
||
| 502 | */ |
||
| 503 | public function iMakeItAvailableFromTo(\DateTime $startsDate, \DateTime $endsDate) |
||
| 510 | |||
| 511 | /** |
||
| 512 | * @Then the :promotion promotion should be available from :startsDate to :endsDate |
||
| 513 | */ |
||
| 514 | public function thePromotionShouldBeAvailableFromTo(PromotionInterface $promotion, \DateTime $startsDate, \DateTime $endsDate) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @Then I should be notified that promotion cannot end before it start |
||
| 531 | */ |
||
| 532 | public function iShouldBeNotifiedThatPromotionCannotEndBeforeItsEvenStart() |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @Then I should be notified that this value should not be blank |
||
| 542 | */ |
||
| 543 | public function iShouldBeNotifiedThatThisValueShouldNotBeBlank() |
||
| 550 | |||
| 551 | /** |
||
| 552 | * @Then I should be notified that the maximum value of a percentage discount is 100% |
||
| 553 | */ |
||
| 554 | public function iShouldBeNotifiedThatTheMaximumValueOfAPercentageDiscountIs100() |
||
| 561 | |||
| 562 | /** |
||
| 563 | * @Then I should be notified that a percentage discount value must be at least 0% |
||
| 564 | */ |
||
| 565 | public function iShouldBeNotifiedThatAPercentageDiscountValueMustBeAtLeast0() |
||
| 572 | |||
| 573 | /** |
||
| 574 | * @Then the promotion :promotion should be used :usage time(s) |
||
| 575 | */ |
||
| 576 | public function thePromotionShouldBeUsedTime(PromotionInterface $promotion, $usage) |
||
| 584 | |||
| 585 | /** |
||
| 586 | * @When I add the "Contains product" rule configured with the :productName product |
||
| 587 | */ |
||
| 588 | public function iAddTheRuleConfiguredWithTheProduct($productName) |
||
| 593 | |||
| 594 | /** |
||
| 595 | * @When I specify that this action should be applied to the :productName product |
||
| 596 | */ |
||
| 597 | public function iSpecifyThatThisActionShouldBeAppliedToTheProduct($productName) |
||
| 601 | |||
| 602 | /** |
||
| 603 | * @Then I should see :count promotions on the list |
||
| 604 | */ |
||
| 605 | public function iShouldSeePromotionsOnTheList($count) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * @Then the first promotion on the list should have :field :value |
||
| 618 | */ |
||
| 619 | public function theFirstPromotionOnTheListShouldHave($field, $value) |
||
| 630 | |||
| 631 | /** |
||
| 632 | * @Then the last promotion on the list should have :field :value |
||
| 633 | */ |
||
| 634 | public function theLastPromotionOnTheListShouldHave($field, $value) |
||
| 645 | |||
| 646 | /** |
||
| 647 | * @param string $element |
||
| 648 | * @param string $expectedMessage |
||
| 649 | */ |
||
| 650 | private function assertFieldValidationMessage($element, $expectedMessage) |
||
| 657 | |||
| 658 | /** |
||
| 659 | * @param PromotionInterface $promotion |
||
| 660 | * @param string $field |
||
| 661 | */ |
||
| 662 | private function assertIfFieldIsTrue(PromotionInterface $promotion, $field) |
||
| 671 | } |
||
| 672 |
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: