Complex classes like PromotionContext 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 PromotionContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | final class PromotionContext implements Context |
||
| 35 | { |
||
| 36 | /** @var SharedStorageInterface */ |
||
| 37 | private $sharedStorage; |
||
| 38 | |||
| 39 | /** @var PromotionActionFactoryInterface */ |
||
| 40 | private $actionFactory; |
||
| 41 | |||
| 42 | /** @var PromotionCouponFactoryInterface */ |
||
| 43 | private $couponFactory; |
||
| 44 | |||
| 45 | /** @var PromotionRuleFactoryInterface */ |
||
| 46 | private $ruleFactory; |
||
| 47 | |||
| 48 | /** @var TestPromotionFactoryInterface */ |
||
| 49 | private $testPromotionFactory; |
||
| 50 | |||
| 51 | /** @var PromotionRepositoryInterface */ |
||
| 52 | private $promotionRepository; |
||
| 53 | |||
| 54 | /** @var ObjectManager */ |
||
| 55 | private $objectManager; |
||
| 56 | |||
| 57 | public function __construct( |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @Given there is (also) a promotion :name |
||
| 77 | * @Given there is a promotion :name identified by :code code |
||
| 78 | */ |
||
| 79 | public function thereIsPromotion(string $name, ?string $code = null): void |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @Given /^there is a promotion "([^"]+)" with "Has at least one from taxons" rule (configured with "[^"]+" and "[^"]+")$/ |
||
| 86 | */ |
||
| 87 | public function thereIsAPromotionWithHasAtLeastOneFromTaxonsRuleConfiguredWith(string $name, array $taxons): void |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @Given /^there is a promotion "([^"]+)" with "Total price of items from taxon" rule configured with ("[^"]+" taxon) and (?:€|£|\$)([^"]+) amount for ("[^"]+" channel)$/ |
||
| 98 | */ |
||
| 99 | public function thereIsAPromotionWithTotalPriceOfItemsFromTaxonRuleConfiguredWithTaxonAndAmountForChannel( |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @Given /^there is a promotion "([^"]+)" with priority ([^"]+)$/ |
||
| 114 | */ |
||
| 115 | public function thereIsAPromotionWithPriority($promotionName, $priority) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @Given /^there is an exclusive promotion "([^"]+)"(?:| with priority ([^"]+))$/ |
||
| 129 | */ |
||
| 130 | public function thereIsAnExclusivePromotionWithPriority($promotionName, $priority = 0) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @Given there is a promotion :promotionName limited to :usageLimit usages |
||
| 145 | */ |
||
| 146 | public function thereIsPromotionLimitedToUsages($promotionName, $usageLimit) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @Given the store has promotion :promotionName with coupon :couponCode |
||
| 158 | * @Given the store has a promotion :promotionName with a coupon :couponCode that is limited to :usageLimit usages |
||
| 159 | */ |
||
| 160 | public function thereIsPromotionWithCoupon(string $promotionName, string $couponCode, ?int $usageLimit = null): void |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @Given /^(this promotion) has "([^"]+)", "([^"]+)" and "([^"]+)" coupons/ |
||
| 178 | */ |
||
| 179 | public function thisPromotionHasCoupons(PromotionInterface $promotion, string ...$couponCodes): void |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @Given /^(this promotion) has already expired$/ |
||
| 193 | */ |
||
| 194 | public function thisPromotionHasExpired(PromotionInterface $promotion) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @Given /^(this promotion) expires tomorrow$/ |
||
| 203 | */ |
||
| 204 | public function thisPromotionExpiresTomorrow(PromotionInterface $promotion) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @Given /^(this promotion) has started yesterday$/ |
||
| 213 | */ |
||
| 214 | public function thisPromotionHasStartedYesterday(PromotionInterface $promotion) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @Given /^(this promotion) starts tomorrow$/ |
||
| 223 | */ |
||
| 224 | public function thisPromotionStartsTomorrow(PromotionInterface $promotion) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @Given /^(this coupon) has already expired$/ |
||
| 233 | */ |
||
| 234 | public function thisCouponHasExpired(PromotionCouponInterface $coupon) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @Given /^(this coupon) expires tomorrow$/ |
||
| 243 | */ |
||
| 244 | public function thisCouponExpiresTomorrow(PromotionCouponInterface $coupon) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @Given /^(this coupon) is set as non reusable after cancelling the order in which it has been used$/ |
||
| 253 | */ |
||
| 254 | public function thisIsSetAsNonReusableAfterCancellingTheOrderInWhichItHasBeenUsed(PromotionCouponInterface $coupon): void |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @Given /^(this coupon) has already reached its usage limit$/ |
||
| 263 | */ |
||
| 264 | public function thisCouponHasReachedItsUsageLimit(PromotionCouponInterface $coupon) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @Given /^(this coupon) can be used (\d+) times?$/ |
||
| 274 | * @Given /^(this coupon) can be used once$/ |
||
| 275 | */ |
||
| 276 | public function thisCouponCanBeUsedNTimes(PromotionCouponInterface $coupon, int $usageLimit = 1): void |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @Given /^(this coupon) can be used once per customer$/ |
||
| 285 | */ |
||
| 286 | public function thisCouponCanBeUsedOncePerCustomer(PromotionCouponInterface $coupon): void |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @Given /^(this coupon) can be used twice per customer$/ |
||
| 295 | */ |
||
| 296 | public function thisCouponCanBeUsedTwicePerCustomer(PromotionCouponInterface $coupon): void |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order$/ |
||
| 305 | */ |
||
| 306 | public function itGivesFixedDiscountToEveryOrder(PromotionInterface $promotion, $discount) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order in the ("[^"]+" channel) and ("(?:€|£|\$)[^"]+") discount to every order in the ("[^"]+" channel)$/ |
||
| 313 | */ |
||
| 314 | public function thisPromotionGivesDiscountToEveryOrderInTheChannelAndDiscountToEveryOrderInTheChannel( |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @Given /^([^"]+) gives ("[^"]+%") discount to every order$/ |
||
| 334 | */ |
||
| 335 | public function itGivesPercentageDiscountToEveryOrder(PromotionInterface $promotion, $discount) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order with quantity at least ([^"]+)$/ |
||
| 342 | */ |
||
| 343 | public function itGivesFixedDiscountToEveryOrderWithQuantityAtLeast( |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order with items total at least ("[^"]+")$/ |
||
| 355 | */ |
||
| 356 | public function itGivesFixedDiscountToEveryOrderWithItemsTotalAtLeast( |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @Given /^([^"]+) gives ("[^"]+%") discount to every order with items total at least ("[^"]+")$/ |
||
| 369 | */ |
||
| 370 | public function itGivesPercentageDiscountToEveryOrderWithItemsTotalAtLeast( |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @Given /^([^"]+) gives ("[^"]+%") off on every product when the item total is at least ("(?:€|£|\$)[^"]+")$/ |
||
| 382 | */ |
||
| 383 | public function itGivesOffOnEveryItemWhenItemTotalExceeds( |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @Given /^([^"]+) gives ("[^"]+%") discount on shipping to every order$/ |
||
| 396 | */ |
||
| 397 | public function itGivesPercentageDiscountOnShippingToEveryOrder(PromotionInterface $promotion, $discount) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @Given /^([^"]+) gives free shipping to every order$/ |
||
| 407 | */ |
||
| 408 | public function thePromotionGivesFreeShippingToEveryOrder(PromotionInterface $promotion) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @Given /^([^"]+) gives(?:| another) ("[^"]+%") off every product (classified as "[^"]+")$/ |
||
| 415 | */ |
||
| 416 | public function itGivesPercentageOffEveryProductClassifiedAs( |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @Given /^([^"]+) gives(?:| another) ("(?:€|£|\$)[^"]+") off on every product (classified as "[^"]+")$/ |
||
| 426 | */ |
||
| 427 | public function itGivesFixedOffEveryProductClassifiedAs( |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product with minimum price at ("(?:€|£|\$)[^"]+")$/ |
||
| 437 | */ |
||
| 438 | public function thisPromotionGivesOffOnEveryProductWithMinimumPriceAt( |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product priced between ("(?:€|£|\$)[^"]+") and ("(?:€|£|\$)[^"]+")$/ |
||
| 448 | */ |
||
| 449 | public function thisPromotionGivesOffOnEveryProductPricedBetween( |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @Given /^([^"]+) gives ("[^"]+%") off on every product with minimum price at ("(?:€|£|\$)[^"]+")$/ |
||
| 464 | */ |
||
| 465 | public function thisPromotionPercentageGivesOffOnEveryProductWithMinimumPriceAt( |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @Given /^([^"]+) gives ("[^"]+%") off on every product priced between ("(?:€|£|\$)[^"]+") and ("(?:€|£|\$)[^"]+")$/ |
||
| 475 | */ |
||
| 476 | public function thisPromotionPercentageGivesOffOnEveryProductPricedBetween( |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+")$/ |
||
| 491 | */ |
||
| 492 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAs( |
||
| 501 | |||
| 502 | /** |
||
| 503 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+" or "[^"]+")$/ |
||
| 504 | */ |
||
| 505 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAsOr( |
||
| 514 | |||
| 515 | /** |
||
| 516 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+") with a minimum value of ("(?:€|£|\$)[^"]+")$/ |
||
| 517 | */ |
||
| 518 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAsAndPricedAt( |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off customer's (\d)(?:st|nd|rd|th) order$/ |
||
| 532 | */ |
||
| 533 | public function itGivesFixedOffCustomersNthOrder(PromotionInterface $promotion, $discount, $nth) |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @Given /^([^"]+) gives ("[^"]+%") off on the customer's (\d)(?:st|nd|rd|th) order$/ |
||
| 542 | */ |
||
| 543 | public function itGivesPercentageOffCustomersNthOrder(PromotionInterface $promotion, $discount, $nth) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") and ("(?:€|£|\$)[^"]+") discount on every order$/ |
||
| 552 | */ |
||
| 553 | public function itGivesPercentageOffOnEveryProductClassifiedAsAndAmountDiscountOnOrder( |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product (classified as "[^"]+") and a free shipping to every order with items total equal at least ("[^"]+")$/ |
||
| 565 | */ |
||
| 566 | public function itGivesOffOnEveryProductClassifiedAsAndAFreeShippingToEveryOrderWithItemsTotalEqualAtLeast( |
||
| 580 | |||
| 581 | /** |
||
| 582 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") and a ("(?:€|£|\$)[^"]+") discount to every order with items total equal at least ("(?:€|£|\$)[^"]+")$/ |
||
| 583 | */ |
||
| 584 | public function itGivesOffOnEveryProductClassifiedAsAndAFixedDiscountToEveryOrderWithItemsTotalEqualAtLeast( |
||
| 604 | |||
| 605 | /** |
||
| 606 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+" or "[^"]+") if order contains any product (classified as "[^"]+" or "[^"]+")$/ |
||
| 607 | */ |
||
| 608 | public function itGivesOffOnEveryProductClassifiedAsOrIfOrderContainsAnyProductClassifiedAsOr( |
||
| 626 | |||
| 627 | /** |
||
| 628 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") if order contains any product (classified as "[^"]+")$/ |
||
| 629 | */ |
||
| 630 | public function itGivesOffOnEveryProductClassifiedAsIfOrderContainsAnyProductClassifiedAs( |
||
| 645 | |||
| 646 | /** |
||
| 647 | * @Given /^(it) is coupon based promotion$/ |
||
| 648 | * @Given /^(it) is a coupon based promotion$/ |
||
| 649 | */ |
||
| 650 | public function itIsCouponBasedPromotion(PromotionInterface $promotion): void |
||
| 656 | |||
| 657 | /** |
||
| 658 | * @Given /^(the promotion) was disabled for the (channel "[^"]+")$/ |
||
| 659 | */ |
||
| 660 | public function thePromotionWasDisabledForTheChannel(PromotionInterface $promotion, ChannelInterface $channel) |
||
| 666 | |||
| 667 | /** |
||
| 668 | * @Given /^the (coupon "[^"]+") was used up to its usage limit$/ |
||
| 669 | */ |
||
| 670 | public function theCouponWasUsed(PromotionCouponInterface $coupon) |
||
| 676 | |||
| 677 | /** |
||
| 678 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains (?:a|an) ("[^"]+" product)$/ |
||
| 679 | */ |
||
| 680 | public function thePromotionGivesOffIfOrderContainsProducts(PromotionInterface $promotion, $discount, ProductInterface $product) |
||
| 686 | |||
| 687 | /** |
||
| 688 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on a ("[^"]*" product)$/ |
||
| 689 | */ |
||
| 690 | public function itGivesFixedDiscountOffOnAProduct(PromotionInterface $promotion, $discount, ProductInterface $product) |
||
| 694 | |||
| 695 | /** |
||
| 696 | * @Given /^([^"]+) gives ("[^"]+%") off on a ("[^"]*" product)$/ |
||
| 697 | */ |
||
| 698 | public function itGivesPercentageDiscountOffOnAProduct(PromotionInterface $promotion, $discount, ProductInterface $product) |
||
| 702 | |||
| 703 | /** |
||
| 704 | * @Given /^([^"]+) gives ("[^"]+%") off the order for customers from ("[^"]*" group)$/ |
||
| 705 | */ |
||
| 706 | public function thePromotionGivesOffTheOrderForCustomersFromGroup( |
||
| 718 | |||
| 719 | /** |
||
| 720 | * @Given /^([^"]+) gives ("[^"]+%") discount on shipping to every order over ("(?:€|£|\$)[^"]+")$/ |
||
| 721 | */ |
||
| 722 | public function itGivesDiscountOnShippingToEveryOrderOver( |
||
| 733 | |||
| 734 | /** |
||
| 735 | * @Given /^([^"]+) gives free shipping to every order over ("(?:€|£|\$)[^"]+")$/ |
||
| 736 | */ |
||
| 737 | public function itGivesFreeShippingToEveryOrderOver(PromotionInterface $promotion, $itemTotal) |
||
| 741 | |||
| 742 | /** |
||
| 743 | * @return array |
||
| 744 | */ |
||
| 745 | private function getTaxonFilterConfiguration(array $taxonCodes) |
||
| 749 | |||
| 750 | /** |
||
| 751 | * @return array |
||
| 752 | */ |
||
| 753 | private function getProductsFilterConfiguration(array $productCodes) |
||
| 757 | |||
| 758 | /** |
||
| 759 | * @param int $minAmount |
||
| 760 | * @param int $maxAmount |
||
| 761 | * |
||
| 762 | * @return array |
||
| 763 | */ |
||
| 764 | private function getPriceRangeFilterConfiguration($minAmount, $maxAmount = null) |
||
| 773 | |||
| 774 | private function createPromotion(string $name, ?string $code = null): PromotionInterface |
||
| 787 | |||
| 788 | /** |
||
| 789 | * @param int $discount |
||
| 790 | */ |
||
| 791 | private function createUnitFixedPromotion(PromotionInterface $promotion, $discount, array $configuration = [], PromotionRuleInterface $rule = null) |
||
| 802 | |||
| 803 | /** |
||
| 804 | * @param int $discount |
||
| 805 | */ |
||
| 806 | private function createUnitPercentagePromotion(PromotionInterface $promotion, $discount, array $configuration = [], PromotionRuleInterface $rule = null) |
||
| 817 | |||
| 818 | /** |
||
| 819 | * @param int $discount |
||
| 820 | */ |
||
| 821 | private function createFixedPromotion( |
||
| 832 | |||
| 833 | /** |
||
| 834 | * @param float $discount |
||
| 835 | * @param PromotionRuleInterface $rule |
||
| 836 | */ |
||
| 837 | private function createPercentagePromotion( |
||
| 845 | |||
| 846 | private function persistPromotion(PromotionInterface $promotion, PromotionActionInterface $action, array $configuration, PromotionRuleInterface $rule = null) |
||
| 858 | |||
| 859 | private function createCoupon(string $couponCode, ?int $usageLimit = null): PromotionCouponInterface |
||
| 868 | } |
||
| 869 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.