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 | /** |
||
| 37 | * @var SharedStorageInterface |
||
| 38 | */ |
||
| 39 | private $sharedStorage; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var PromotionActionFactoryInterface |
||
| 43 | */ |
||
| 44 | private $actionFactory; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var PromotionCouponFactoryInterface |
||
| 48 | */ |
||
| 49 | private $couponFactory; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var PromotionRuleFactoryInterface |
||
| 53 | */ |
||
| 54 | private $ruleFactory; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var TestPromotionFactoryInterface |
||
| 58 | */ |
||
| 59 | private $testPromotionFactory; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var PromotionRepositoryInterface |
||
| 63 | */ |
||
| 64 | private $promotionRepository; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var ObjectManager |
||
| 68 | */ |
||
| 69 | private $objectManager; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param SharedStorageInterface $sharedStorage |
||
| 73 | * @param PromotionActionFactoryInterface $actionFactory |
||
| 74 | * @param PromotionCouponFactoryInterface $couponFactory |
||
| 75 | * @param PromotionRuleFactoryInterface $ruleFactory |
||
| 76 | * @param TestPromotionFactoryInterface $testPromotionFactory |
||
| 77 | * @param PromotionRepositoryInterface $promotionRepository |
||
| 78 | * @param ObjectManager $objectManager |
||
| 79 | */ |
||
| 80 | public function __construct( |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @Given there is (also) a promotion :promotionName |
||
| 100 | * @Given there is a promotion :promotionName identified by :promotionCode code |
||
| 101 | */ |
||
| 102 | public function thereIsPromotion(string $promotionName, ?string $promotionCode = null): void |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @Given /^there is a promotion "([^"]+)" with priority ([^"]+)$/ |
||
| 118 | */ |
||
| 119 | public function thereIsAPromotionWithPriority($promotionName, $priority) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @Given /^there is an exclusive promotion "([^"]+)"(?:| with priority ([^"]+))$/ |
||
| 133 | */ |
||
| 134 | public function thereIsAnExclusivePromotionWithPriority($promotionName, $priority = 0) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @Given there is a promotion :promotionName limited to :usageLimit usages |
||
| 149 | */ |
||
| 150 | public function thereIsPromotionLimitedToUsages($promotionName, $usageLimit) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @Given the store has promotion :promotionName with coupon :couponCode |
||
| 162 | * @Given the store has a promotion :promotionName with a coupon :couponCode that is limited to :usageLimit usages |
||
| 163 | */ |
||
| 164 | public function thereIsPromotionWithCoupon(string $promotionName, string $couponCode, ?int $usageLimit = null): void |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @Given /^(this promotion) has "([^"]+)", "([^"]+)" and "([^"]+)" coupons/ |
||
| 182 | */ |
||
| 183 | public function thisPromotionHasCoupons(PromotionInterface $promotion, string ...$couponCodes): void |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @Given /^(this promotion) has already expired$/ |
||
| 197 | */ |
||
| 198 | public function thisPromotionHasExpired(PromotionInterface $promotion) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @Given /^(this promotion) expires tomorrow$/ |
||
| 207 | */ |
||
| 208 | public function thisPromotionExpiresTomorrow(PromotionInterface $promotion) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @Given /^(this promotion) has started yesterday$/ |
||
| 217 | */ |
||
| 218 | public function thisPromotionHasStartedYesterday(PromotionInterface $promotion) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @Given /^(this promotion) starts tomorrow$/ |
||
| 227 | */ |
||
| 228 | public function thisPromotionStartsTomorrow(PromotionInterface $promotion) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @Given /^(this coupon) has already expired$/ |
||
| 237 | */ |
||
| 238 | public function thisCouponHasExpired(PromotionCouponInterface $coupon) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @Given /^(this coupon) expires tomorrow$/ |
||
| 247 | */ |
||
| 248 | public function thisCouponExpiresTomorrow(PromotionCouponInterface $coupon) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @Given /^(this coupon) has already reached its usage limit$/ |
||
| 257 | */ |
||
| 258 | public function thisCouponHasReachedItsUsageLimit(PromotionCouponInterface $coupon) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @Given /^(this coupon) can be used (\d+) times?$/ |
||
| 268 | */ |
||
| 269 | public function thisCouponCanBeUsedNTimes(PromotionCouponInterface $coupon, $usageLimit) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @Given /^(this coupon) can be used twice per customer$/ |
||
| 278 | */ |
||
| 279 | public function thisCouponCanBeUsedTwicePerCustomer(PromotionCouponInterface $coupon) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order$/ |
||
| 288 | */ |
||
| 289 | public function itGivesFixedDiscountToEveryOrder(PromotionInterface $promotion, $discount) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order in the ("[^"]+" channel) and ("(?:€|£|\$)[^"]+") discount to every order in the ("[^"]+" channel)$/ |
||
| 296 | */ |
||
| 297 | public function thisPromotionGivesDiscountToEveryOrderInTheChannelAndDiscountToEveryOrderInTheChannel( |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @Given /^([^"]+) gives ("[^"]+%") discount to every order$/ |
||
| 317 | */ |
||
| 318 | public function itGivesPercentageDiscountToEveryOrder(PromotionInterface $promotion, $discount) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order with quantity at least ([^"]+)$/ |
||
| 325 | */ |
||
| 326 | public function itGivesFixedDiscountToEveryOrderWithQuantityAtLeast( |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order with items total at least ("[^"]+")$/ |
||
| 338 | */ |
||
| 339 | public function itGivesFixedDiscountToEveryOrderWithItemsTotalAtLeast( |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @Given /^([^"]+) gives ("[^"]+%") discount to every order with items total at least ("[^"]+")$/ |
||
| 352 | */ |
||
| 353 | public function itGivesPercentageDiscountToEveryOrderWithItemsTotalAtLeast( |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @Given /^([^"]+) gives ("[^"]+%") off on every product when the item total is at least ("(?:€|£|\$)[^"]+")$/ |
||
| 365 | */ |
||
| 366 | public function itGivesOffOnEveryItemWhenItemTotalExceeds( |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @Given /^([^"]+) gives ("[^"]+%") discount on shipping to every order$/ |
||
| 379 | */ |
||
| 380 | public function itGivesPercentageDiscountOnShippingToEveryOrder(PromotionInterface $promotion, $discount) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @Given /^([^"]+) gives free shipping to every order$/ |
||
| 390 | */ |
||
| 391 | public function thePromotionGivesFreeShippingToEveryOrder(PromotionInterface $promotion) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @Given /^([^"]+) gives(?:| another) ("[^"]+%") off every product (classified as "[^"]+")$/ |
||
| 398 | */ |
||
| 399 | public function itGivesPercentageOffEveryProductClassifiedAs( |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @Given /^([^"]+) gives(?:| another) ("(?:€|£|\$)[^"]+") off on every product (classified as "[^"]+")$/ |
||
| 409 | */ |
||
| 410 | public function itGivesFixedOffEveryProductClassifiedAs( |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product with minimum price at ("(?:€|£|\$)[^"]+")$/ |
||
| 420 | */ |
||
| 421 | public function thisPromotionGivesOffOnEveryProductWithMinimumPriceAt( |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product priced between ("(?:€|£|\$)[^"]+") and ("(?:€|£|\$)[^"]+")$/ |
||
| 431 | */ |
||
| 432 | public function thisPromotionGivesOffOnEveryProductPricedBetween( |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @Given /^([^"]+) gives ("[^"]+%") off on every product with minimum price at ("(?:€|£|\$)[^"]+")$/ |
||
| 447 | */ |
||
| 448 | public function thisPromotionPercentageGivesOffOnEveryProductWithMinimumPriceAt( |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @Given /^([^"]+) gives ("[^"]+%") off on every product priced between ("(?:€|£|\$)[^"]+") and ("(?:€|£|\$)[^"]+")$/ |
||
| 458 | */ |
||
| 459 | public function thisPromotionPercentageGivesOffOnEveryProductPricedBetween( |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+")$/ |
||
| 474 | */ |
||
| 475 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAs( |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+" or "[^"]+")$/ |
||
| 487 | */ |
||
| 488 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAsOr( |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+") with a minimum value of ("(?:€|£|\$)[^"]+")$/ |
||
| 500 | */ |
||
| 501 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAsAndPricedAt( |
||
| 512 | |||
| 513 | /** |
||
| 514 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off customer's (\d)(?:st|nd|rd|th) order$/ |
||
| 515 | */ |
||
| 516 | public function itGivesFixedOffCustomersNthOrder(PromotionInterface $promotion, $discount, $nth) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @Given /^([^"]+) gives ("[^"]+%") off on the customer's (\d)(?:st|nd|rd|th) order$/ |
||
| 525 | */ |
||
| 526 | public function itGivesPercentageOffCustomersNthOrder(PromotionInterface $promotion, $discount, $nth) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") and ("(?:€|£|\$)[^"]+") discount on every order$/ |
||
| 535 | */ |
||
| 536 | public function itGivesPercentageOffOnEveryProductClassifiedAsAndAmountDiscountOnOrder( |
||
| 545 | |||
| 546 | /** |
||
| 547 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product (classified as "[^"]+") and a free shipping to every order with items total equal at least ("[^"]+")$/ |
||
| 548 | */ |
||
| 549 | public function itGivesOffOnEveryProductClassifiedAsAndAFreeShippingToEveryOrderWithItemsTotalEqualAtLeast( |
||
| 563 | |||
| 564 | /** |
||
| 565 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") and a ("(?:€|£|\$)[^"]+") discount to every order with items total equal at least ("(?:€|£|\$)[^"]+")$/ |
||
| 566 | */ |
||
| 567 | public function itGivesOffOnEveryProductClassifiedAsAndAFixedDiscountToEveryOrderWithItemsTotalEqualAtLeast( |
||
| 587 | |||
| 588 | /** |
||
| 589 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+" or "[^"]+") if order contains any product (classified as "[^"]+" or "[^"]+")$/ |
||
| 590 | */ |
||
| 591 | public function itGivesOffOnEveryProductClassifiedAsOrIfOrderContainsAnyProductClassifiedAsOr( |
||
| 609 | |||
| 610 | /** |
||
| 611 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") if order contains any product (classified as "[^"]+")$/ |
||
| 612 | */ |
||
| 613 | public function itGivesOffOnEveryProductClassifiedAsIfOrderContainsAnyProductClassifiedAs( |
||
| 628 | |||
| 629 | /** |
||
| 630 | * @Given /^(it) is coupon based promotion$/ |
||
| 631 | */ |
||
| 632 | public function itIsCouponBasedPromotion(PromotionInterface $promotion) |
||
| 638 | |||
| 639 | /** |
||
| 640 | * @Given /^(the promotion) was disabled for the (channel "[^"]+")$/ |
||
| 641 | */ |
||
| 642 | public function thePromotionWasDisabledForTheChannel(PromotionInterface $promotion, ChannelInterface $channel) |
||
| 648 | |||
| 649 | /** |
||
| 650 | * @Given /^the (coupon "[^"]+") was used up to its usage limit$/ |
||
| 651 | */ |
||
| 652 | public function theCouponWasUsed(PromotionCouponInterface $coupon) |
||
| 658 | |||
| 659 | /** |
||
| 660 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains (?:a|an) ("[^"]+" product)$/ |
||
| 661 | */ |
||
| 662 | public function thePromotionGivesOffIfOrderContainsProducts(PromotionInterface $promotion, $discount, ProductInterface $product) |
||
| 668 | |||
| 669 | /** |
||
| 670 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on a ("[^"]*" product)$/ |
||
| 671 | */ |
||
| 672 | public function itGivesFixedDiscountOffOnAProduct(PromotionInterface $promotion, $discount, ProductInterface $product) |
||
| 676 | |||
| 677 | /** |
||
| 678 | * @Given /^([^"]+) gives ("[^"]+%") off on a ("[^"]*" product)$/ |
||
| 679 | */ |
||
| 680 | public function itGivesPercentageDiscountOffOnAProduct(PromotionInterface $promotion, $discount, ProductInterface $product) |
||
| 684 | |||
| 685 | /** |
||
| 686 | * @Given /^([^"]+) gives ("[^"]+%") off the order for customers from ("[^"]*" group)$/ |
||
| 687 | */ |
||
| 688 | public function thePromotionGivesOffTheOrderForCustomersFromGroup( |
||
| 700 | |||
| 701 | /** |
||
| 702 | * @Given /^([^"]+) gives ("[^"]+%") discount on shipping to every order over ("(?:€|£|\$)[^"]+")$/ |
||
| 703 | */ |
||
| 704 | public function itGivesDiscountOnShippingToEveryOrderOver( |
||
| 715 | |||
| 716 | /** |
||
| 717 | * @Given /^([^"]+) gives free shipping to every order over ("(?:€|£|\$)[^"]+")$/ |
||
| 718 | */ |
||
| 719 | public function itGivesFreeShippingToEveryOrderOver(PromotionInterface $promotion, $itemTotal) |
||
| 723 | |||
| 724 | /** |
||
| 725 | * @param array $taxonCodes |
||
| 726 | * |
||
| 727 | * @return array |
||
|
|
|||
| 728 | */ |
||
| 729 | private function getTaxonFilterConfiguration(array $taxonCodes) |
||
| 733 | |||
| 734 | /** |
||
| 735 | * @param array $productCodes |
||
| 736 | * |
||
| 737 | * @return array |
||
| 738 | */ |
||
| 739 | private function getProductsFilterConfiguration(array $productCodes) |
||
| 743 | |||
| 744 | /** |
||
| 745 | * @param int $minAmount |
||
| 746 | * @param int $maxAmount |
||
| 747 | * |
||
| 748 | * @return array |
||
| 749 | */ |
||
| 750 | private function getPriceRangeFilterConfiguration($minAmount, $maxAmount = null) |
||
| 759 | |||
| 760 | /** |
||
| 761 | * @param PromotionInterface $promotion |
||
| 762 | * @param int $discount |
||
| 763 | * @param array $configuration |
||
| 764 | * @param PromotionRuleInterface|null $rule |
||
| 765 | */ |
||
| 766 | private function createUnitFixedPromotion(PromotionInterface $promotion, $discount, array $configuration = [], PromotionRuleInterface $rule = null) |
||
| 777 | |||
| 778 | /** |
||
| 779 | * @param PromotionInterface $promotion |
||
| 780 | * @param int $discount |
||
| 781 | * @param array $configuration |
||
| 782 | * @param PromotionRuleInterface|null $rule |
||
| 783 | */ |
||
| 784 | private function createUnitPercentagePromotion(PromotionInterface $promotion, $discount, array $configuration = [], PromotionRuleInterface $rule = null) |
||
| 795 | |||
| 796 | /** |
||
| 797 | * @param PromotionInterface $promotion |
||
| 798 | * @param int $discount |
||
| 799 | * @param array $configuration |
||
| 800 | * @param PromotionRuleInterface|null $rule |
||
| 801 | * @param ChannelInterface|null $channel |
||
| 802 | */ |
||
| 803 | private function createFixedPromotion( |
||
| 814 | |||
| 815 | /** |
||
| 816 | * @param PromotionInterface $promotion |
||
| 817 | * @param float $discount |
||
| 818 | * @param array $configuration |
||
| 819 | * @param PromotionRuleInterface $rule |
||
| 820 | */ |
||
| 821 | private function createPercentagePromotion( |
||
| 829 | |||
| 830 | /** |
||
| 831 | * @param PromotionInterface $promotion |
||
| 832 | * @param PromotionActionInterface $action |
||
| 833 | * @param array $configuration |
||
| 834 | * @param PromotionRuleInterface|null $rule |
||
| 835 | */ |
||
| 836 | private function persistPromotion(PromotionInterface $promotion, PromotionActionInterface $action, array $configuration, PromotionRuleInterface $rule = null) |
||
| 848 | |||
| 849 | /** |
||
| 850 | * @param string $couponCode |
||
| 851 | * @param int|null $usageLimit |
||
| 852 | * |
||
| 853 | * @return PromotionCouponInterface |
||
| 854 | */ |
||
| 855 | private function createCoupon(string $couponCode, ?int $usageLimit = null): PromotionCouponInterface |
||
| 864 | } |
||
| 865 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.