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 :count :taxonName |
||
| 169 | */ |
||
| 170 | public function iAddTheRuleConfiguredWith($count, $taxonName) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @When /^I add the "([^"]+)" action configured with amount of "(?:€|£|\$)([^"]+)"$/ |
||
| 179 | */ |
||
| 180 | public function iAddTheActionConfiguredWithAmount($actionType, $amount) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @When I add the :actionType action |
||
| 188 | */ |
||
| 189 | public function iAddTheAction($actionType) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @When /^it is(?:| also) configured with base amount of "(?:€|£|\$)([^"]+)"$/ |
||
| 196 | */ |
||
| 197 | public function itIsConfiguredWithBaseAmount($amount) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @When /^it is(?:| also) configured with "([^"]+)" amount of "(?:€|£|\$)([^"]+)"$/ |
||
| 204 | */ |
||
| 205 | public function itIsConfiguredWithAmount($currencyCode, $amount) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @When /^I specify that this action should be applied to items with price greater then "(?:€|£|\$)([^"]+)"$/ |
||
| 218 | */ |
||
| 219 | public function iAddAMinPriceFilterRange($minimum) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @When /^I specify that this action should be applied to items with price lesser then "(?:€|£|\$)([^"]+)"$/ |
||
| 226 | */ |
||
| 227 | public function iAddAMaxPriceFilterRange($maximum) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @When /^I specify that this action should be applied to items with price between "(?:€|£|\$)([^"]+)" and "(?:€|£|\$)([^"]+)"$/ |
||
| 234 | */ |
||
| 235 | public function iAddAMinMaxPriceFilterRange($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) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @Given I add the :actionType action configured with a percentage value of :percentage% |
||
| 252 | * @Given I add the :actionType action configured without a percentage value |
||
| 253 | */ |
||
| 254 | public function iAddTheActionConfiguredWithAPercentageValue($actionType, $percentage = null) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @Then /^there should be (\d+) promotion(?:|s)$/ |
||
| 262 | */ |
||
| 263 | public function thereShouldBePromotion($number) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @Then /^(this promotion) should be coupon based$/ |
||
| 274 | */ |
||
| 275 | public function thisPromotionShouldBeCouponBased(PromotionInterface $promotion) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @Then /^I should be able to manage coupons for (this promotion)$/ |
||
| 285 | */ |
||
| 286 | public function iShouldBeAbleToManageCouponsForThisPromotion(PromotionInterface $promotion) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @Then I should be notified that :element is required |
||
| 296 | */ |
||
| 297 | public function iShouldBeNotifiedThatIsRequired($element) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @Then I should be notified that a :element value should be a numeric value |
||
| 304 | */ |
||
| 305 | public function iShouldBeNotifiedThatAMinimalValueShouldBeNumeric($element) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @Then I should be notified that promotion with this code already exists |
||
| 312 | */ |
||
| 313 | public function iShouldBeNotifiedThatPromotionWithThisCodeAlreadyExists() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @Then promotion with :element :name should not be added |
||
| 320 | */ |
||
| 321 | public function promotionWithElementValueShouldNotBeAdded($element, $name) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @Then there should still be only one promotion with :element :value |
||
| 333 | */ |
||
| 334 | public function thereShouldStillBeOnlyOnePromotionWith($element, $value) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @When I set its usage limit to :usageLimit |
||
| 346 | */ |
||
| 347 | public function iSetItsUsageLimitTo($usageLimit) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @Then the :promotion promotion should be available to be used only :usageLimit times |
||
| 356 | */ |
||
| 357 | public function thePromotionShouldBeAvailableToUseOnlyTimes(PromotionInterface $promotion, $usageLimit) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @When I make it exclusive |
||
| 369 | */ |
||
| 370 | public function iMakeItExclusive() |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @Then the :promotion promotion should be exclusive |
||
| 379 | */ |
||
| 380 | public function thePromotionShouldBeExclusive(PromotionInterface $promotion) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @When I make it coupon based |
||
| 387 | */ |
||
| 388 | public function iMakeItCouponBased() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @Then the :promotion promotion should be coupon based |
||
| 397 | */ |
||
| 398 | public function thePromotionShouldBeCouponBased(PromotionInterface $promotion) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @When I make it applicable for the :channelName channel |
||
| 405 | */ |
||
| 406 | public function iMakeItApplicableForTheChannel($channelName) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @Then the :promotion promotion should be applicable for the :channelName channel |
||
| 415 | */ |
||
| 416 | public function thePromotionShouldBeApplicableForTheChannel(PromotionInterface $promotion, $channelName) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @Given I want to modify a :promotion promotion |
||
| 428 | * @Given /^I want to modify (this promotion)$/ |
||
| 429 | */ |
||
| 430 | public function iWantToModifyAPromotion(PromotionInterface $promotion) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @Then the code field should be disabled |
||
| 437 | */ |
||
| 438 | public function theCodeFieldShouldBeDisabled() |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @When I save my changes |
||
| 448 | * @When I try to save my changes |
||
| 449 | */ |
||
| 450 | public function iSaveMyChanges() |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @When /^I delete a ("([^"]+)" promotion)$/ |
||
| 457 | * @When /^I try to delete a ("([^"]+)" promotion)$/ |
||
| 458 | */ |
||
| 459 | public function iDeletePromotion(PromotionInterface $promotion) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @Then /^(this promotion) should no longer exist in the promotion registry$/ |
||
| 469 | */ |
||
| 470 | public function promotionShouldNotExistInTheRegistry(PromotionInterface $promotion) |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @Then I should be notified that it is in use and cannot be deleted |
||
| 482 | */ |
||
| 483 | public function iShouldBeNotifiedOfFailure() |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @When I make it available from :startsDate to :endsDate |
||
| 493 | */ |
||
| 494 | public function iMakeItAvailableFromTo(\DateTime $startsDate, \DateTime $endsDate) |
||
| 501 | |||
| 502 | /** |
||
| 503 | * @Then the :promotion promotion should be available from :startsDate to :endsDate |
||
| 504 | */ |
||
| 505 | public function thePromotionShouldBeAvailableFromTo(PromotionInterface $promotion, \DateTime $startsDate, \DateTime $endsDate) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @Then I should be notified that promotion cannot end before it start |
||
| 522 | */ |
||
| 523 | public function iShouldBeNotifiedThatPromotionCannotEndBeforeItsEvenStart() |
||
| 530 | |||
| 531 | /** |
||
| 532 | * @Then I should be notified that this value should not be blank |
||
| 533 | */ |
||
| 534 | public function iShouldBeNotifiedThatThisValueShouldNotBeBlank() |
||
| 541 | |||
| 542 | /** |
||
| 543 | * @Then I should be notified that the maximum value of a percentage discount is 100% |
||
| 544 | */ |
||
| 545 | public function iShouldBeNotifiedThatTheMaximumValueOfAPercentageDiscountIs100() |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @Then I should be notified that a percentage discount value must be at least 0% |
||
| 555 | */ |
||
| 556 | public function iShouldBeNotifiedThatAPercentageDiscountValueMustBeAtLeast0() |
||
| 563 | |||
| 564 | /** |
||
| 565 | * @Then the promotion :promotion should be used :usage time(s) |
||
| 566 | */ |
||
| 567 | public function thePromotionShouldBeUsedTime(PromotionInterface $promotion, $usage) |
||
| 575 | |||
| 576 | /** |
||
| 577 | * @When I add the "Contains product" rule configured with the :productName product |
||
| 578 | */ |
||
| 579 | public function iAddTheRuleConfiguredWithTheProduct($productName) |
||
| 584 | |||
| 585 | /** |
||
| 586 | * @When I specify that this action should be applied to the :productName product |
||
| 587 | */ |
||
| 588 | public function iSpecifyThatThisActionShouldBeAppliedToTheProduct($productName) |
||
| 592 | |||
| 593 | /** |
||
| 594 | * @Then I should see :count promotions on the list |
||
| 595 | */ |
||
| 596 | public function iShouldSeePromotionsOnTheList($count) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * @Then the first promotion on the list should have :field :value |
||
| 609 | */ |
||
| 610 | public function theFirstPromotionOnTheListShouldHave($field, $value) |
||
| 621 | |||
| 622 | /** |
||
| 623 | * @Then the last promotion on the list should have :field :value |
||
| 624 | */ |
||
| 625 | public function theLastPromotionOnTheListShouldHave($field, $value) |
||
| 636 | |||
| 637 | /** |
||
| 638 | * @param string $element |
||
| 639 | * @param string $expectedMessage |
||
| 640 | */ |
||
| 641 | private function assertFieldValidationMessage($element, $expectedMessage) |
||
| 648 | |||
| 649 | /** |
||
| 650 | * @param PromotionInterface $promotion |
||
| 651 | * @param string $field |
||
| 652 | */ |
||
| 653 | private function assertIfFieldIsTrue(PromotionInterface $promotion, $field) |
||
| 662 | } |
||
| 663 |