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 | * @Given 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 | * @Then the :promotionName promotion should appear in the registry |
||
| 122 | * @Then the :promotionName promotion should exist in the registry |
||
| 123 | * @Then this promotion should still be named :promotionName |
||
| 124 | * @Then promotion :promotionName should still exist in the registry |
||
| 125 | */ |
||
| 126 | public function thePromotionShouldAppearInTheRegistry($promotionName) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @When I add it |
||
| 138 | * @When I try to add it |
||
| 139 | */ |
||
| 140 | public function iAddIt() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @When I add the "Has at least one from taxons" rule configured with :firstTaxon |
||
| 147 | * @When I add the "Has at least one from taxons" rule configured with :firstTaxon and :secondTaxon |
||
| 148 | */ |
||
| 149 | public function iAddTheHasTaxonRuleConfiguredWith(...$taxons) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @When I add the "Total price of items from taxon" rule configured with :count :taxonName |
||
| 160 | */ |
||
| 161 | public function iAddTheRuleConfiguredWith($count, $taxonName) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @When /^I add the "([^"]+)" action configured with amount of "(?:€|£|\$)([^"]+)"$/ |
||
| 170 | */ |
||
| 171 | public function iAddTheActionConfiguredWithAmount($actionType, $amount) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @When /^I specify that this action should be applied to items with price greater then "(?:€|£|\$)([^"]+)"$/ |
||
| 179 | */ |
||
| 180 | public function iAddAMinPriceFilterRange($minimum) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @When /^I specify that this action should be applied to items with price lesser then "(?:€|£|\$)([^"]+)"$/ |
||
| 187 | */ |
||
| 188 | public function iAddAMaxPriceFilterRange($maximum) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @When /^I specify that this action should be applied to items with price between "(?:€|£|\$)([^"]+)" and "(?:€|£|\$)([^"]+)"$/ |
||
| 195 | */ |
||
| 196 | public function iAddAMinMaxPriceFilterRange($minimum, $maximum) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @When I specify that this action should be applied to items from :taxonName category |
||
| 204 | */ |
||
| 205 | public function iSpecifyThatThisActionShouldBeAppliedToItemsFromCategory($taxonName) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @Given I add the :actionType action configured with a percentage value of :percentage% |
||
| 213 | * @Given I add the :actionType action configured without a percentage value |
||
| 214 | */ |
||
| 215 | public function iAddTheActionConfiguredWithAPercentageValue($actionType, $percentage = null) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @Then /^there should be (\d+) promotion(?:|s)$/ |
||
| 223 | */ |
||
| 224 | public function thereShouldBePromotion($number) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @Then /^(this promotion) should be coupon based$/ |
||
| 235 | */ |
||
| 236 | public function thisPromotionShouldBeCouponBased(PromotionInterface $promotion) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @Then /^I should be able to manage coupons for (this promotion)$/ |
||
| 246 | */ |
||
| 247 | public function iShouldBeAbleToManageCouponsForThisPromotion(PromotionInterface $promotion) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @Then I should be notified that :element is required |
||
| 257 | */ |
||
| 258 | public function iShouldBeNotifiedThatIsRequired($element) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @Then I should be notified that a :element value should be a numeric value |
||
| 265 | */ |
||
| 266 | public function iShouldBeNotifiedThatAMinimalValueShouldBeNumeric($element) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @Then I should be notified that promotion with this code already exists |
||
| 273 | */ |
||
| 274 | public function iShouldBeNotifiedThatPromotionWithThisCodeAlreadyExists() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @Then promotion with :element :name should not be added |
||
| 281 | */ |
||
| 282 | public function promotionWithElementValueShouldNotBeAdded($element, $name) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @Then there should still be only one promotion with :element :value |
||
| 294 | */ |
||
| 295 | public function thereShouldStillBeOnlyOnePromotionWith($element, $value) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @When I set its usage limit to :usageLimit |
||
| 307 | */ |
||
| 308 | public function iSetItsUsageLimitTo($usageLimit) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @Then the :promotion promotion should be available to be used only :usageLimit times |
||
| 317 | */ |
||
| 318 | public function thePromotionShouldBeAvailableToUseOnlyTimes(PromotionInterface $promotion, $usageLimit) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @When I make it exclusive |
||
| 330 | */ |
||
| 331 | public function iMakeItExclusive() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @Then the :promotion promotion should be exclusive |
||
| 340 | */ |
||
| 341 | public function thePromotionShouldBeExclusive(PromotionInterface $promotion) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @When I make it coupon based |
||
| 348 | */ |
||
| 349 | public function iMakeItCouponBased() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @Then the :promotion promotion should be coupon based |
||
| 358 | */ |
||
| 359 | public function thePromotionShouldBeCouponBased(PromotionInterface $promotion) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @When I make it applicable for the :channelName channel |
||
| 366 | */ |
||
| 367 | public function iMakeItApplicableForTheChannel($channelName) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @Then the :promotion promotion should be applicable for the :channelName channel |
||
| 376 | */ |
||
| 377 | public function thePromotionShouldBeApplicableForTheChannel(PromotionInterface $promotion, $channelName) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @Given I want to modify a :promotion promotion |
||
| 389 | * @Given /^I want to modify (this promotion)$/ |
||
| 390 | */ |
||
| 391 | public function iWantToModifyAPromotion(PromotionInterface $promotion) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @Then the code field should be disabled |
||
| 398 | */ |
||
| 399 | public function theCodeFieldShouldBeDisabled() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @When I save my changes |
||
| 409 | * @When I try to save my changes |
||
| 410 | */ |
||
| 411 | public function iSaveMyChanges() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @When /^I delete a ("([^"]+)" promotion)$/ |
||
| 418 | * @When /^I try to delete a ("([^"]+)" promotion)$/ |
||
| 419 | */ |
||
| 420 | public function iDeletePromotion(PromotionInterface $promotion) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @Then /^(this promotion) should no longer exist in the promotion registry$/ |
||
| 430 | */ |
||
| 431 | public function promotionShouldNotExistInTheRegistry(PromotionInterface $promotion) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @Then I should be notified that it is in use and cannot be deleted |
||
| 443 | */ |
||
| 444 | public function iShouldBeNotifiedOfFailure() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @When I make it available from :startsDate to :endsDate |
||
| 454 | */ |
||
| 455 | public function iMakeItAvailableFromTo(\DateTime $startsDate, \DateTime $endsDate) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @Then the :promotion promotion should be available from :startsDate to :endsDate |
||
| 465 | */ |
||
| 466 | public function thePromotionShouldBeAvailableFromTo(PromotionInterface $promotion, \DateTime $startsDate, \DateTime $endsDate) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @Then I should be notified that promotion cannot end before it start |
||
| 483 | */ |
||
| 484 | public function iShouldBeNotifiedThatPromotionCannotEndBeforeItsEvenStart() |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @Then I should be notified that this value should not be blank |
||
| 494 | */ |
||
| 495 | public function iShouldBeNotifiedThatThisValueShouldNotBeBlank() |
||
| 502 | |||
| 503 | /** |
||
| 504 | * @Then I should be notified that the maximum value of a percentage discount is 100% |
||
| 505 | */ |
||
| 506 | public function iShouldBeNotifiedThatTheMaximumValueOfAPercentageDiscountIs100() |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @Then I should be notified that a percentage discount value must be at least 0% |
||
| 516 | */ |
||
| 517 | public function iShouldBeNotifiedThatAPercentageDiscountValueMustBeAtLeast0() |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @Then the promotion :promotion should be used :usage time(s) |
||
| 527 | */ |
||
| 528 | public function thePromotionShouldBeUsedTime(PromotionInterface $promotion, $usage) |
||
| 536 | |||
| 537 | /** |
||
| 538 | * @When I add the "Contains product" rule configured with the :productName product |
||
| 539 | */ |
||
| 540 | public function iAddTheRuleConfiguredWithTheProduct($productName) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * @When I specify that this action should be applied to the :productName product |
||
| 548 | */ |
||
| 549 | public function iSpecifyThatThisActionShouldBeAppliedToTheProduct($productName) |
||
| 553 | |||
| 554 | /** |
||
| 555 | * @Then I should see :count promotions on the list |
||
| 556 | */ |
||
| 557 | public function iShouldSeePromotionsOnTheList($count) |
||
| 567 | |||
| 568 | /** |
||
| 569 | * @Then the first promotion on the list should have :field :value |
||
| 570 | */ |
||
| 571 | public function theFirstPromotionOnTheListShouldHave($field, $value) |
||
| 582 | |||
| 583 | /** |
||
| 584 | * @Then the last promotion on the list should have :field :value |
||
| 585 | */ |
||
| 586 | public function theLastPromotionOnTheListShouldHave($field, $value) |
||
| 597 | |||
| 598 | /** |
||
| 599 | * @param string $element |
||
| 600 | * @param string $expectedMessage |
||
| 601 | */ |
||
| 602 | private function assertFieldValidationMessage($element, $expectedMessage) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * @param PromotionInterface $promotion |
||
| 612 | * @param string $field |
||
| 613 | */ |
||
| 614 | private function assertIfFieldIsTrue(PromotionInterface $promotion, $field) |
||
| 623 | } |
||
| 624 |