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 ("[^"]+%") off on every product when the item total is at least ("(?:€|£|\$)[^"]+")$/ | ||
| 352 | */ | ||
| 353 | public function itGivesOffOnEveryItemWhenItemTotalExceeds( | ||
| 363 | |||
| 364 | /** | ||
| 365 |      * @Given /^([^"]+) gives ("[^"]+%") discount on shipping to every order$/ | ||
| 366 | */ | ||
| 367 | public function itGivesPercentageDiscountOnShippingToEveryOrder(PromotionInterface $promotion, $discount) | ||
| 374 | |||
| 375 | /** | ||
| 376 | * @Given /^([^"]+) gives free shipping to every order$/ | ||
| 377 | */ | ||
| 378 | public function thePromotionGivesFreeShippingToEveryOrder(PromotionInterface $promotion) | ||
| 382 | |||
| 383 | /** | ||
| 384 |      * @Given /^([^"]+) gives(?:| another) ("[^"]+%") off every product (classified as "[^"]+")$/ | ||
| 385 | */ | ||
| 386 | public function itGivesPercentageOffEveryProductClassifiedAs( | ||
| 393 | |||
| 394 | /** | ||
| 395 |      * @Given /^([^"]+) gives(?:| another) ("(?:€|£|\$)[^"]+") off on every product (classified as "[^"]+")$/ | ||
| 396 | */ | ||
| 397 | public function itGivesFixedOffEveryProductClassifiedAs( | ||
| 404 | |||
| 405 | /** | ||
| 406 |      * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product with minimum price at ("(?:€|£|\$)[^"]+")$/ | ||
| 407 | */ | ||
| 408 | public function thisPromotionGivesOffOnEveryProductWithMinimumPriceAt( | ||
| 415 | |||
| 416 | /** | ||
| 417 |      * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product priced between ("(?:€|£|\$)[^"]+") and ("(?:€|£|\$)[^"]+")$/ | ||
| 418 | */ | ||
| 419 | public function thisPromotionGivesOffOnEveryProductPricedBetween( | ||
| 431 | |||
| 432 | /** | ||
| 433 |      * @Given /^([^"]+) gives ("[^"]+%") off on every product with minimum price at ("(?:€|£|\$)[^"]+")$/ | ||
| 434 | */ | ||
| 435 | public function thisPromotionPercentageGivesOffOnEveryProductWithMinimumPriceAt( | ||
| 442 | |||
| 443 | /** | ||
| 444 |      * @Given /^([^"]+) gives ("[^"]+%") off on every product priced between ("(?:€|£|\$)[^"]+") and ("(?:€|£|\$)[^"]+")$/ | ||
| 445 | */ | ||
| 446 | public function thisPromotionPercentageGivesOffOnEveryProductPricedBetween( | ||
| 458 | |||
| 459 | /** | ||
| 460 |      * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+")$/ | ||
| 461 | */ | ||
| 462 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAs( | ||
| 471 | |||
| 472 | /** | ||
| 473 |      * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+" or "[^"]+")$/ | ||
| 474 | */ | ||
| 475 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAsOr( | ||
| 484 | |||
| 485 | /** | ||
| 486 |      * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+") with a minimum value of ("(?:€|£|\$)[^"]+")$/ | ||
| 487 | */ | ||
| 488 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAsAndPricedAt( | ||
| 499 | |||
| 500 | /** | ||
| 501 |      * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off customer's (\d)(?:st|nd|rd|th) order$/ | ||
| 502 | */ | ||
| 503 | public function itGivesFixedOffCustomersNthOrder(PromotionInterface $promotion, $discount, $nth) | ||
| 509 | |||
| 510 | /** | ||
| 511 |      * @Given /^([^"]+) gives ("[^"]+%") off on the customer's (\d)(?:st|nd|rd|th) order$/ | ||
| 512 | */ | ||
| 513 | public function itGivesPercentageOffCustomersNthOrder(PromotionInterface $promotion, $discount, $nth) | ||
| 519 | |||
| 520 | /** | ||
| 521 |      * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") and ("(?:€|£|\$)[^"]+") discount on every order$/ | ||
| 522 | */ | ||
| 523 | public function itGivesPercentageOffOnEveryProductClassifiedAsAndAmountDiscountOnOrder( | ||
| 532 | |||
| 533 | /** | ||
| 534 |      * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product (classified as "[^"]+") and a free shipping to every order with items total equal at least ("[^"]+")$/ | ||
| 535 | */ | ||
| 536 | public function itGivesOffOnEveryProductClassifiedAsAndAFreeShippingToEveryOrderWithItemsTotalEqualAtLeast( | ||
| 550 | |||
| 551 | /** | ||
| 552 |      * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") and a ("(?:€|£|\$)[^"]+") discount to every order with items total equal at least ("(?:€|£|\$)[^"]+")$/ | ||
| 553 | */ | ||
| 554 | public function itGivesOffOnEveryProductClassifiedAsAndAFixedDiscountToEveryOrderWithItemsTotalEqualAtLeast( | ||
| 574 | |||
| 575 | /** | ||
| 576 |      * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+" or "[^"]+") if order contains any product (classified as "[^"]+" or "[^"]+")$/ | ||
| 577 | */ | ||
| 578 | public function itGivesOffOnEveryProductClassifiedAsOrIfOrderContainsAnyProductClassifiedAsOr( | ||
| 596 | |||
| 597 | /** | ||
| 598 |      * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") if order contains any product (classified as "[^"]+")$/ | ||
| 599 | */ | ||
| 600 | public function itGivesOffOnEveryProductClassifiedAsIfOrderContainsAnyProductClassifiedAs( | ||
| 615 | |||
| 616 | /** | ||
| 617 | * @Given /^(it) is coupon based promotion$/ | ||
| 618 | */ | ||
| 619 | public function itIsCouponBasedPromotion(PromotionInterface $promotion) | ||
| 625 | |||
| 626 | /** | ||
| 627 | * @Given /^(the promotion) was disabled for the (channel "[^"]+")$/ | ||
| 628 | */ | ||
| 629 | public function thePromotionWasDisabledForTheChannel(PromotionInterface $promotion, ChannelInterface $channel) | ||
| 635 | |||
| 636 | /** | ||
| 637 | * @Given /^the (coupon "[^"]+") was used up to its usage limit$/ | ||
| 638 | */ | ||
| 639 | public function theCouponWasUsed(PromotionCouponInterface $coupon) | ||
| 645 | |||
| 646 | /** | ||
| 647 |      * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains (?:a|an) ("[^"]+" product)$/ | ||
| 648 | */ | ||
| 649 | public function thePromotionGivesOffIfOrderContainsProducts(PromotionInterface $promotion, $discount, ProductInterface $product) | ||
| 655 | |||
| 656 | /** | ||
| 657 |      * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on a ("[^"]*" product)$/ | ||
| 658 | */ | ||
| 659 | public function itGivesFixedDiscountOffOnAProduct(PromotionInterface $promotion, $discount, ProductInterface $product) | ||
| 663 | |||
| 664 | /** | ||
| 665 |      * @Given /^([^"]+) gives ("[^"]+%") off on a ("[^"]*" product)$/ | ||
| 666 | */ | ||
| 667 | public function itGivesPercentageDiscountOffOnAProduct(PromotionInterface $promotion, $discount, ProductInterface $product) | ||
| 671 | |||
| 672 | /** | ||
| 673 |      * @Given /^([^"]+) gives ("[^"]+%") off the order for customers from ("[^"]*" group)$/ | ||
| 674 | */ | ||
| 675 | public function thePromotionGivesOffTheOrderForCustomersFromGroup( | ||
| 686 | |||
| 687 | /** | ||
| 688 |      * @Given /^([^"]+) gives ("[^"]+%") discount on shipping to every order over ("(?:€|£|\$)[^"]+")$/ | ||
| 689 | */ | ||
| 690 | public function itGivesDiscountOnShippingToEveryOrderOver( | ||
| 701 | |||
| 702 | /** | ||
| 703 |      * @Given /^([^"]+) gives free shipping to every order over ("(?:€|£|\$)[^"]+")$/ | ||
| 704 | */ | ||
| 705 | public function itGivesFreeShippingToEveryOrderOver(PromotionInterface $promotion, $itemTotal) | ||
| 709 | |||
| 710 | /** | ||
| 711 | * @param array $taxonCodes | ||
| 712 | * | ||
| 713 | * @return array | ||
| 714 | */ | ||
| 715 | private function getTaxonFilterConfiguration(array $taxonCodes) | ||
| 719 | |||
| 720 | /** | ||
| 721 | * @param array $productCodes | ||
| 722 | * | ||
| 723 | * @return array | ||
| 724 | */ | ||
| 725 | private function getProductsFilterConfiguration(array $productCodes) | ||
| 729 | |||
| 730 | /** | ||
| 731 | * @param int $minAmount | ||
| 732 | * @param int $maxAmount | ||
| 733 | * | ||
| 734 | * @return array | ||
| 735 | */ | ||
| 736 | private function getPriceRangeFilterConfiguration($minAmount, $maxAmount = null) | ||
| 745 | |||
| 746 | /** | ||
| 747 | * @param PromotionInterface $promotion | ||
| 748 | * @param int $discount | ||
| 749 | * @param array $configuration | ||
| 750 | * @param PromotionRuleInterface|null $rule | ||
| 751 | */ | ||
| 752 | private function createUnitFixedPromotion(PromotionInterface $promotion, $discount, array $configuration = [], PromotionRuleInterface $rule = null) | ||
| 763 | |||
| 764 | /** | ||
| 765 | * @param PromotionInterface $promotion | ||
| 766 | * @param int $discount | ||
| 767 | * @param array $configuration | ||
| 768 | * @param PromotionRuleInterface|null $rule | ||
| 769 | */ | ||
| 770 | private function createUnitPercentagePromotion(PromotionInterface $promotion, $discount, array $configuration = [], PromotionRuleInterface $rule = null) | ||
| 781 | |||
| 782 | /** | ||
| 783 | * @param PromotionInterface $promotion | ||
| 784 | * @param int $discount | ||
| 785 | * @param array $configuration | ||
| 786 | * @param PromotionRuleInterface|null $rule | ||
| 787 | * @param ChannelInterface|null $channel | ||
| 788 | */ | ||
| 789 | private function createFixedPromotion( | ||
| 800 | |||
| 801 | /** | ||
| 802 | * @param PromotionInterface $promotion | ||
| 803 | * @param float $discount | ||
| 804 | * @param array $configuration | ||
| 805 | * @param PromotionRuleInterface $rule | ||
| 806 | */ | ||
| 807 | private function createPercentagePromotion( | ||
| 815 | |||
| 816 | /** | ||
| 817 | * @param PromotionInterface $promotion | ||
| 818 | * @param PromotionActionInterface $action | ||
| 819 | * @param array $configuration | ||
| 820 | * @param PromotionRuleInterface|null $rule | ||
| 821 | */ | ||
| 822 | private function persistPromotion(PromotionInterface $promotion, PromotionActionInterface $action, array $configuration, PromotionRuleInterface $rule = null) | ||
| 834 | |||
| 835 | /** | ||
| 836 | * @param string $couponCode | ||
| 837 | * @param int|null $usageLimit | ||
| 838 | * | ||
| 839 | * @return PromotionCouponInterface | ||
| 840 | */ | ||
| 841 | private function createCoupon(string $couponCode, ?int $usageLimit = null): PromotionCouponInterface | ||
| 850 | } | ||
| 851 | 
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.