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 |
||
| 27 | final class ManagingPromotionsContext implements Context |
||
| 28 | { |
||
| 29 | /** @var SharedStorageInterface */ |
||
| 30 | private $sharedStorage; |
||
| 31 | |||
| 32 | /** @var IndexPageInterface */ |
||
| 33 | private $indexPage; |
||
| 34 | |||
| 35 | /** @var CreatePageInterface */ |
||
| 36 | private $createPage; |
||
| 37 | |||
| 38 | /** @var UpdatePageInterface */ |
||
| 39 | private $updatePage; |
||
| 40 | |||
| 41 | /** @var CurrentPageResolverInterface */ |
||
| 42 | private $currentPageResolver; |
||
| 43 | |||
| 44 | /** @var NotificationCheckerInterface */ |
||
| 45 | private $notificationChecker; |
||
| 46 | |||
| 47 | public function __construct( |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @When I want to create a new promotion |
||
| 65 | */ |
||
| 66 | public function iWantToCreateANewPromotion() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @Given I want to browse promotions |
||
| 73 | * @When I browse promotions |
||
| 74 | */ |
||
| 75 | public function iWantToBrowsePromotions() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @When I specify its code as :code |
||
| 82 | * @When I do not specify its code |
||
| 83 | */ |
||
| 84 | public function iSpecifyItsCodeAs($code = null) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @When I name it :name |
||
| 91 | * @When I do not name it |
||
| 92 | * @When I remove its name |
||
| 93 | */ |
||
| 94 | public function iNameIt($name = null) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @When I remove its priority |
||
| 101 | */ |
||
| 102 | public function iRemoveItsPriority() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @Then I should see the promotion :promotionName in the list |
||
| 109 | * @Then the :promotionName promotion should appear in the registry |
||
| 110 | * @Then the :promotionName promotion should exist in the registry |
||
| 111 | * @Then this promotion should still be named :promotionName |
||
| 112 | * @Then promotion :promotionName should still exist in the registry |
||
| 113 | */ |
||
| 114 | public function thePromotionShouldAppearInTheRegistry(string $promotionName): void |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @When I add it |
||
| 123 | * @When I try to add it |
||
| 124 | */ |
||
| 125 | public function iAddIt() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @When I add the "Has at least one from taxons" rule configured with :firstTaxon |
||
| 132 | * @When I add the "Has at least one from taxons" rule configured with :firstTaxon and :secondTaxon |
||
| 133 | */ |
||
| 134 | public function iAddTheHasTaxonRuleConfiguredWith(...$taxons) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @When /^I add the "Total price of items from taxon" rule configured with "([^"]+)" taxon and (?:€|£|\$)([^"]+) amount for "([^"]+)" channel$/ |
||
| 143 | */ |
||
| 144 | public function iAddTheRuleConfiguredWith($taxonName, $amount, $channelName) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @When /^I add the "Item total" rule configured with (?:€|£|\$)([^"]+) amount for "([^"]+)" channel and (?:€|£|\$)([^"]+) amount for "([^"]+)" channel$/ |
||
| 153 | */ |
||
| 154 | public function iAddTheItemTotalRuleConfiguredWithTwoChannel( |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @When /^I add the "([^"]+)" action configured with amount of "(?:€|£|\$)([^"]+)" for "([^"]+)" channel$/ |
||
| 167 | */ |
||
| 168 | public function iAddTheActionConfiguredWithAmountForChannel($actionType, $amount, $channelName) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @When /^it is(?:| also) configured with amount of "(?:€|£|\$)([^"]+)" for "([^"]+)" channel$/ |
||
| 176 | */ |
||
| 177 | public function itIsConfiguredWithAmountForChannel($amount, $channelName) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @When /^I specify that on "([^"]+)" channel this action should be applied to items with price greater then "(?:€|£|\$)([^"]+)"$/ |
||
| 184 | */ |
||
| 185 | public function iAddAMinPriceFilterRangeForChannel($channelName, $minimum) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @When /^I specify that on "([^"]+)" channel this action should be applied to items with price lesser then "(?:€|£|\$)([^"]+)"$/ |
||
| 192 | */ |
||
| 193 | public function iAddAMaxPriceFilterRangeForChannel($channelName, $maximum) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @When /^I specify that on "([^"]+)" channel this action should be applied to items with price between "(?:€|£|\$)([^"]+)" and "(?:€|£|\$)([^"]+)"$/ |
||
| 200 | */ |
||
| 201 | public function iAddAMinMaxPriceFilterRangeForChannel($channelName, $minimum, $maximum) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @When I specify that this action should be applied to items from :taxonName category |
||
| 209 | */ |
||
| 210 | public function iSpecifyThatThisActionShouldBeAppliedToItemsFromCategory($taxonName) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @When /^I add the "([^"]+)" action configured with a percentage value of (?:|-)([^"]+)% for ("[^"]+") channel$/ |
||
| 217 | */ |
||
| 218 | public function iAddTheActionConfiguredWithAPercentageValueForChannel( |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @When /^I add the "([^"]+)" action configured with a percentage value of (?:|-)([^"]+)%$/ |
||
| 229 | * @When I add the :actionType action configured without a percentage value |
||
| 230 | */ |
||
| 231 | public function iAddTheActionConfiguredWithAPercentageValue($actionType, $percentage = null) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @When I add the "Customer group" rule for :customerGroupName group |
||
| 239 | */ |
||
| 240 | public function iAddTheCustomerGroupRuleConfiguredForGroup($customerGroupName) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @When I check (also) the :promotionName promotion |
||
| 248 | */ |
||
| 249 | public function iCheckThePromotion(string $promotionName): void |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @When I delete them |
||
| 256 | */ |
||
| 257 | public function iDeleteThem(): void |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @Then I should see a single promotion in the list |
||
| 264 | * @Then there should be :amount promotions |
||
| 265 | */ |
||
| 266 | public function thereShouldBePromotion(int $amount = 1): void |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @Then /^(this promotion) should be coupon based$/ |
||
| 273 | */ |
||
| 274 | public function thisPromotionShouldBeCouponBased(PromotionInterface $promotion) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @Then /^I should be able to manage coupons for (this promotion)$/ |
||
| 281 | */ |
||
| 282 | public function iShouldBeAbleToManageCouponsForThisPromotion(PromotionInterface $promotion) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @Then I should be notified that :element is required |
||
| 289 | */ |
||
| 290 | public function iShouldBeNotifiedThatIsRequired($element) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @Then I should be notified that a :element value should be a numeric value |
||
| 297 | */ |
||
| 298 | public function iShouldBeNotifiedThatAMinimalValueShouldBeNumeric($element) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @Then I should be notified that promotion with this code already exists |
||
| 305 | */ |
||
| 306 | public function iShouldBeNotifiedThatPromotionWithThisCodeAlreadyExists() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @Then promotion with :element :name should not be added |
||
| 313 | */ |
||
| 314 | public function promotionWithElementValueShouldNotBeAdded($element, $name) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @Then there should still be only one promotion with :element :value |
||
| 323 | */ |
||
| 324 | public function thereShouldStillBeOnlyOnePromotionWith($element, $value) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @When I set its usage limit to :usageLimit |
||
| 333 | */ |
||
| 334 | public function iSetItsUsageLimitTo($usageLimit) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @Then the :promotion promotion should be available to be used only :usageLimit times |
||
| 344 | */ |
||
| 345 | public function thePromotionShouldBeAvailableToUseOnlyTimes(PromotionInterface $promotion, $usageLimit) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @When I make it exclusive |
||
| 354 | */ |
||
| 355 | public function iMakeItExclusive() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @Then the :promotion promotion should be exclusive |
||
| 365 | */ |
||
| 366 | public function thePromotionShouldBeExclusive(PromotionInterface $promotion) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @When I make it coupon based |
||
| 373 | */ |
||
| 374 | public function iMakeItCouponBased() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @Then the :promotion promotion should be coupon based |
||
| 384 | */ |
||
| 385 | public function thePromotionShouldBeCouponBased(PromotionInterface $promotion) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @When I make it applicable for the :channelName channel |
||
| 392 | */ |
||
| 393 | public function iMakeItApplicableForTheChannel($channelName) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @Then the :promotion promotion should be applicable for the :channelName channel |
||
| 403 | */ |
||
| 404 | public function thePromotionShouldBeApplicableForTheChannel(PromotionInterface $promotion, $channelName) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @Given I want to modify a :promotion promotion |
||
| 413 | * @Given /^I want to modify (this promotion)$/ |
||
| 414 | */ |
||
| 415 | public function iWantToModifyAPromotion(PromotionInterface $promotion) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @Then the code field should be disabled |
||
| 422 | */ |
||
| 423 | public function theCodeFieldShouldBeDisabled() |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @When I save my changes |
||
| 430 | * @When I try to save my changes |
||
| 431 | */ |
||
| 432 | public function iSaveMyChanges() |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @When /^I delete a ("([^"]+)" promotion)$/ |
||
| 439 | * @When /^I try to delete a ("([^"]+)" promotion)$/ |
||
| 440 | */ |
||
| 441 | public function iDeletePromotion(PromotionInterface $promotion) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @Then /^(this promotion) should no longer exist in the promotion registry$/ |
||
| 451 | */ |
||
| 452 | public function promotionShouldNotExistInTheRegistry(PromotionInterface $promotion) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @Then I should be notified that it is in use and cannot be deleted |
||
| 461 | */ |
||
| 462 | public function iShouldBeNotifiedOfFailure() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @When I make it available from :startsDate to :endsDate |
||
| 472 | */ |
||
| 473 | public function iMakeItAvailableFromTo(\DateTimeInterface $startsDate, \DateTimeInterface $endsDate) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * @Then the :promotion promotion should be available from :startsDate to :endsDate |
||
| 484 | */ |
||
| 485 | public function thePromotionShouldBeAvailableFromTo(PromotionInterface $promotion, \DateTimeInterface $startsDate, \DateTimeInterface $endsDate) |
||
| 493 | |||
| 494 | /** |
||
| 495 | * @Then I should be notified that promotion cannot end before it start |
||
| 496 | */ |
||
| 497 | public function iShouldBeNotifiedThatPromotionCannotEndBeforeItsEvenStart() |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @Then I should be notified that this value should not be blank |
||
| 507 | */ |
||
| 508 | public function iShouldBeNotifiedThatThisValueShouldNotBeBlank() |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @Then I should be notified that the maximum value of a percentage discount is 100% |
||
| 518 | */ |
||
| 519 | public function iShouldBeNotifiedThatTheMaximumValueOfAPercentageDiscountIs100() |
||
| 526 | |||
| 527 | /** |
||
| 528 | * @Then I should be notified that a percentage discount value must be at least 0% |
||
| 529 | */ |
||
| 530 | public function iShouldBeNotifiedThatAPercentageDiscountValueMustBeAtLeast0() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @Then the promotion :promotion should be used :usage time(s) |
||
| 540 | * @Then the promotion :promotion should not be used |
||
| 541 | */ |
||
| 542 | public function thePromotionShouldBeUsedTime(PromotionInterface $promotion, $usage = 0) |
||
| 550 | |||
| 551 | /** |
||
| 552 | * @When I add the "Contains product" rule configured with the :productName product |
||
| 553 | */ |
||
| 554 | public function iAddTheRuleConfiguredWithTheProduct($productName) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * @When I specify that this action should be applied to the :productName product |
||
| 562 | */ |
||
| 563 | public function iSpecifyThatThisActionShouldBeAppliedToTheProduct($productName) |
||
| 567 | |||
| 568 | /** |
||
| 569 | * @Then I should see :count promotions on the list |
||
| 570 | */ |
||
| 571 | public function iShouldSeePromotionsOnTheList($count) |
||
| 581 | |||
| 582 | /** |
||
| 583 | * @Then the first promotion on the list should have :field :value |
||
| 584 | */ |
||
| 585 | public function theFirstPromotionOnTheListShouldHave($field, $value) |
||
| 596 | |||
| 597 | /** |
||
| 598 | * @Then the last promotion on the list should have :field :value |
||
| 599 | */ |
||
| 600 | public function theLastPromotionOnTheListShouldHave($field, $value) |
||
| 611 | |||
| 612 | /** |
||
| 613 | * @Given the :promotion promotion should have priority :priority |
||
| 614 | */ |
||
| 615 | public function thePromotionsShouldHavePriority(PromotionInterface $promotion, int $priority) |
||
| 621 | |||
| 622 | /** |
||
| 623 | * @Then I should be able to modify a :promotion promotion |
||
| 624 | */ |
||
| 625 | public function iShouldBeAbleToModifyAPromotion(PromotionInterface $promotion): void |
||
| 630 | |||
| 631 | /** |
||
| 632 | * @Then the :promotion promotion should have :ruleName rule configured |
||
| 633 | */ |
||
| 634 | public function thePromotionShouldHaveRuleConfigured(PromotionInterface $promotion, string $ruleName): void |
||
| 641 | |||
| 642 | /** |
||
| 643 | * @Then the :promotion promotion should not have any rule configured |
||
| 644 | */ |
||
| 645 | public function thePromotionShouldNotHaveAnyRuleConfigured(PromotionInterface $promotion): void |
||
| 651 | |||
| 652 | /** |
||
| 653 | * @param string $element |
||
| 654 | * @param string $expectedMessage |
||
| 655 | */ |
||
| 656 | private function assertFieldValidationMessage($element, $expectedMessage) |
||
| 663 | |||
| 664 | /** |
||
| 665 | * @param string $field |
||
| 666 | */ |
||
| 667 | private function assertIfFieldIsTrue(PromotionInterface $promotion, $field) |
||
| 673 | } |
||
| 674 |