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 |
||
| 33 | final class PromotionContext implements Context |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var SharedStorageInterface |
||
| 37 | */ |
||
| 38 | private $sharedStorage; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var PromotionActionFactoryInterface |
||
| 42 | */ |
||
| 43 | private $actionFactory; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var PromotionCouponFactoryInterface |
||
| 47 | */ |
||
| 48 | private $couponFactory; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var PromotionRuleFactoryInterface |
||
| 52 | */ |
||
| 53 | private $ruleFactory; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var TestPromotionFactoryInterface |
||
| 57 | */ |
||
| 58 | private $testPromotionFactory; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var PromotionRepositoryInterface |
||
| 62 | */ |
||
| 63 | private $promotionRepository; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var ObjectManager |
||
| 67 | */ |
||
| 68 | private $objectManager; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param SharedStorageInterface $sharedStorage |
||
| 72 | * @param PromotionActionFactoryInterface $actionFactory |
||
| 73 | * @param PromotionCouponFactoryInterface $couponFactory |
||
| 74 | * @param PromotionRuleFactoryInterface $ruleFactory |
||
| 75 | * @param TestPromotionFactoryInterface $testPromotionFactory |
||
| 76 | * @param PromotionRepositoryInterface $promotionRepository |
||
| 77 | * @param ObjectManager $objectManager |
||
| 78 | */ |
||
| 79 | public function __construct( |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @Given there is a promotion :promotionName |
||
| 99 | * @Given there is a promotion :promotionName identified by :promotionCode code |
||
| 100 | */ |
||
| 101 | public function thereIsPromotion($promotionName, $promotionCode = null) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @Given /^there is a promotion "([^"]+)" with priority ([^"]+)$/ |
||
| 117 | */ |
||
| 118 | public function thereIsAPromotionWithPriority($promotionName, $priority) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @Given /^there is an exclusive promotion "([^"]+)"(?:| with priority ([^"]+))$/ |
||
| 132 | */ |
||
| 133 | public function thereIsAnExclusivePromotionWithPriority($promotionName, $priority = 0) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @Given there is a promotion :promotionName limited to :usageLimit usages |
||
| 148 | */ |
||
| 149 | public function thereIsPromotionLimitedToUsages($promotionName, $usageLimit) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @Given the store has promotion :promotionName with coupon :couponCode |
||
| 161 | * @Given the store has also 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($promotionName, $couponCode, $usageLimit = null) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @Given /^(this promotion) has already expired$/ |
||
| 185 | */ |
||
| 186 | public function thisPromotionHasExpired(PromotionInterface $promotion) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @Given /^(this coupon) has already expired$/ |
||
| 195 | */ |
||
| 196 | public function thisCouponHasExpired(PromotionCouponInterface $coupon) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @Given /^(this coupon) expires tomorrow$/ |
||
| 205 | */ |
||
| 206 | public function thisCouponExpiresTomorrow(PromotionCouponInterface $coupon) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @Given /^(this coupon) has already reached its usage limit$/ |
||
| 215 | */ |
||
| 216 | public function thisCouponHasReachedItsUsageLimit(PromotionCouponInterface $coupon) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @Given /^(this coupon) can be used (\d+) times?$/ |
||
| 226 | */ |
||
| 227 | public function thisCouponCanBeUsedNTimes(PromotionCouponInterface $coupon, $usageLimit) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @Given /^(this coupon) can be used twice per customer$/ |
||
| 236 | */ |
||
| 237 | public function thisCouponCanBeUsedTwicePerCustomer(PromotionCouponInterface $coupon) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order$/ |
||
| 246 | */ |
||
| 247 | public function itGivesFixedDiscountToEveryOrder(PromotionInterface $promotion, $discount) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @Given /^(this promotion) gives ("(?:€|£|\$)[^"]+") in base currency or ("(?:€|£|\$)[^"]+") in "([^"]+)" discount to every order$/ |
||
| 254 | */ |
||
| 255 | public function itGivesFixedDiscountInDifferentCurrencyToEveryOrder( |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @Given /^([^"]+) gives ("[^"]+%") discount to every order$/ |
||
| 270 | */ |
||
| 271 | public function itGivesPercentageDiscountToEveryOrder(PromotionInterface $promotion, $discount) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order with quantity at least ([^"]+)$/ |
||
| 278 | */ |
||
| 279 | public function itGivesFixedDiscountToEveryOrderWithQuantityAtLeast( |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order with items total at least ("[^"]+")$/ |
||
| 291 | */ |
||
| 292 | public function itGivesFixedDiscountToEveryOrderWithItemsTotalAtLeast( |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @Given /^([^"]+) gives ("[^"]+%") off on every product when the item total is at least ("(?:€|£|\$)[^"]+")$/ |
||
| 304 | */ |
||
| 305 | public function itGivesOffOnEveryItemWhenItemTotalExceeds( |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @Given /^([^"]+) gives ("[^"]+%") discount on shipping to every order$/ |
||
| 317 | */ |
||
| 318 | public function itGivesPercentageDiscountOnShippingToEveryOrder(PromotionInterface $promotion, $discount) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @Given /^([^"]+) gives free shipping to every order$/ |
||
| 328 | */ |
||
| 329 | public function thePromotionGivesFreeShippingToEveryOrder(PromotionInterface $promotion) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @Given /^([^"]+) gives ("[^"]+%") off every product (classified as "[^"]+")$/ |
||
| 336 | */ |
||
| 337 | public function itGivesPercentageOffEveryProductClassifiedAs( |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product (classified as "[^"]+")$/ |
||
| 347 | */ |
||
| 348 | public function itGivesFixedOffEveryProductClassifiedAs( |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product with minimum price at ("(?:€|£|\$)[^"]+")$/ |
||
| 358 | */ |
||
| 359 | public function thisPromotionGivesOffOnEveryProductWithMinimumPriceAt( |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") in base currency or ("(?:€|£|\$)[^"]+") in "([^"]+)" off on every product with minimum price at ("(?:€|£|\$)[^"]+")$/ |
||
| 369 | */ |
||
| 370 | public function thisPromotionGivesInDifferentCurrenciesOffOnEveryProductWithMinimumPriceAt( |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product priced between ("(?:€|£|\$)[^"]+") and ("(?:€|£|\$)[^"]+")$/ |
||
| 385 | */ |
||
| 386 | public function thisPromotionGivesOffOnEveryProductPricedBetween( |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @Given /^([^"]+) gives ("[^"]+%") off on every product with minimum price at ("(?:€|£|\$)[^"]+")$/ |
||
| 401 | */ |
||
| 402 | public function thisPromotionPercentageGivesOffOnEveryProductWithMinimumPriceAt( |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @Given /^([^"]+) gives ("[^"]+%") off on every product priced between ("(?:€|£|\$)[^"]+") and ("(?:€|£|\$)[^"]+")$/ |
||
| 412 | */ |
||
| 413 | public function thisPromotionPercentageGivesOffOnEveryProductPricedBetween( |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+")$/ |
||
| 428 | */ |
||
| 429 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAs( |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+" or "[^"]+")$/ |
||
| 441 | */ |
||
| 442 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAsOr( |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+") with a minimum value of ("(?:€|£|\$)[^"]+")$/ |
||
| 454 | */ |
||
| 455 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAsAndPricedAt( |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off customer's (\d)(?:st|nd|rd|th) order$/ |
||
| 468 | */ |
||
| 469 | public function itGivesFixedOffCustomersNthOrder(PromotionInterface $promotion, $discount, $nth) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @Given /^([^"]+) gives ("[^"]+%") off on the customer's (\d)(?:st|nd|rd|th) order$/ |
||
| 478 | */ |
||
| 479 | public function itGivesPercentageOffCustomersNthOrder(PromotionInterface $promotion, $discount, $nth) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") and ("(?:€|£|\$)[^"]+") discount on every order$/ |
||
| 488 | */ |
||
| 489 | public function itGivesPercentageOffOnEveryProductClassifiedAsAndAmountDiscountOnOrder( |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product (classified as "[^"]+") and a free shipping to every order with items total equal at least ("[^"]+")$/ |
||
| 501 | */ |
||
| 502 | public function itGivesOffOnEveryProductClassifiedAsAndAFreeShippingToEveryOrderWithItemsTotalEqualAtLeast( |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") and a ("(?:€|£|\$)[^"]+") discount to every order with items total equal at least ("(?:€|£|\$)[^"]+")$/ |
||
| 518 | */ |
||
| 519 | public function itGivesOffOnEveryProductClassifiedAsAndAFixedDiscountToEveryOrderWithItemsTotalEqualAtLeast( |
||
| 538 | |||
| 539 | /** |
||
| 540 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+" or "[^"]+") if order contains any product (classified as "[^"]+" or "[^"]+")$/ |
||
| 541 | */ |
||
| 542 | public function itGivesOffOnEveryProductClassifiedAsOrIfOrderContainsAnyProductClassifiedAsOr( |
||
| 560 | |||
| 561 | /** |
||
| 562 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") if order contains any product (classified as "[^"]+")$/ |
||
| 563 | */ |
||
| 564 | public function itGivesOffOnEveryProductClassifiedAsIfOrderContainsAnyProductClassifiedAs( |
||
| 579 | |||
| 580 | /** |
||
| 581 | * @Given /^(it) is coupon based promotion$/ |
||
| 582 | */ |
||
| 583 | public function itIsCouponBasedPromotion(PromotionInterface $promotion) |
||
| 589 | |||
| 590 | /** |
||
| 591 | * @Given /^(the promotion) was disabled for the (channel "[^"]+")$/ |
||
| 592 | */ |
||
| 593 | public function thePromotionWasDisabledForTheChannel(PromotionInterface $promotion, ChannelInterface $channel) |
||
| 599 | |||
| 600 | /** |
||
| 601 | * @Given /^the (coupon "[^"]+") was used up to its usage limit$/ |
||
| 602 | */ |
||
| 603 | public function theCouponWasUsed(PromotionCouponInterface $coupon) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains (?:a|an) ("[^"]+" product)$/ |
||
| 612 | */ |
||
| 613 | public function thePromotionGivesOffIfOrderContainsProducts(PromotionInterface $promotion, $discount, ProductInterface $product) |
||
| 619 | |||
| 620 | /** |
||
| 621 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on a ("[^"]*" product)$/ |
||
| 622 | */ |
||
| 623 | public function itGivesFixedDiscountOffOnAProduct(PromotionInterface $promotion, $discount, ProductInterface $product) |
||
| 627 | |||
| 628 | /** |
||
| 629 | * @Given /^([^"]+) gives ("[^"]+%") off on a ("[^"]*" product)$/ |
||
| 630 | */ |
||
| 631 | public function itGivesPercentageDiscountOffOnAProduct(PromotionInterface $promotion, $discount, ProductInterface $product) |
||
| 635 | |||
| 636 | /** |
||
| 637 | * @param array $taxonCodes |
||
| 638 | * |
||
| 639 | * @return array |
||
| 640 | */ |
||
| 641 | private function getTaxonFilterConfiguration(array $taxonCodes) |
||
| 645 | |||
| 646 | /** |
||
| 647 | * @param array $productCodes |
||
| 648 | * |
||
| 649 | * @return array |
||
| 650 | */ |
||
| 651 | private function getProductsFilterConfiguration(array $productCodes) |
||
| 655 | |||
| 656 | /** |
||
| 657 | * @param int $minAmount |
||
| 658 | * @param int $maxAmount |
||
| 659 | * |
||
| 660 | * @return array |
||
| 661 | */ |
||
| 662 | private function getPriceRangeFilterConfiguration($minAmount, $maxAmount = null) |
||
| 671 | |||
| 672 | /** |
||
| 673 | * @param PromotionInterface $promotion |
||
| 674 | * @param int $discount |
||
| 675 | * @param array $configuration |
||
| 676 | * @param PromotionRuleInterface|null $rule |
||
| 677 | */ |
||
| 678 | private function createUnitFixedPromotion(PromotionInterface $promotion, $discount, array $configuration = [], PromotionRuleInterface $rule = null) |
||
| 682 | |||
| 683 | /** |
||
| 684 | * @param PromotionInterface $promotion |
||
| 685 | * @param int $discount |
||
| 686 | * @param array $configuration |
||
| 687 | * @param PromotionRuleInterface|null $rule |
||
| 688 | */ |
||
| 689 | private function createUnitPercentagePromotion(PromotionInterface $promotion, $discount, array $configuration = [], PromotionRuleInterface $rule = null) |
||
| 693 | |||
| 694 | /** |
||
| 695 | * @param PromotionInterface $promotion |
||
| 696 | * @param int $discount |
||
| 697 | * @param array $configuration |
||
| 698 | * @param PromotionRuleInterface $rule |
||
| 699 | */ |
||
| 700 | private function createFixedPromotion(PromotionInterface $promotion, $discount, array $configuration = [], PromotionRuleInterface $rule = null) |
||
| 704 | |||
| 705 | /** |
||
| 706 | * @param PromotionInterface $promotion |
||
| 707 | * @param float $discount |
||
| 708 | * @param array $configuration |
||
| 709 | * @param PromotionRuleInterface $rule |
||
| 710 | */ |
||
| 711 | private function createPercentagePromotion(PromotionInterface $promotion, $discount, array $configuration = [], PromotionRuleInterface $rule = null) |
||
| 715 | |||
| 716 | /** |
||
| 717 | * @param PromotionInterface $promotion |
||
| 718 | * @param PromotionActionInterface $action |
||
| 719 | * @param array $configuration |
||
| 720 | * @param PromotionRuleInterface|null $rule |
||
| 721 | */ |
||
| 722 | private function persistPromotion(PromotionInterface $promotion, PromotionActionInterface $action, array $configuration, PromotionRuleInterface $rule = null) |
||
| 734 | } |
||
| 735 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.