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 /^([^"]+) gives ("[^"]+%") discount to every order$/ |
||
| 254 | */ |
||
| 255 | public function itGivesPercentageDiscountToEveryOrder(PromotionInterface $promotion, $discount) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order with quantity at least ([^"]+)$/ |
||
| 262 | */ |
||
| 263 | public function itGivesFixedDiscountToEveryOrderWithQuantityAtLeast( |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order with items total at least ("[^"]+")$/ |
||
| 275 | */ |
||
| 276 | public function itGivesFixedDiscountToEveryOrderWithItemsTotalAtLeast( |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @Given /^([^"]+) gives ("[^"]+%") off on every product when the item total is at least ("(?:€|£|\$)[^"]+")$/ |
||
| 288 | */ |
||
| 289 | public function itGivesOffOnEveryItemWhenItemTotalExceeds( |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @Given /^([^"]+) gives ("[^"]+%") discount on shipping to every order$/ |
||
| 301 | */ |
||
| 302 | public function itGivesPercentageDiscountOnShippingToEveryOrder(PromotionInterface $promotion, $discount) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @Given /^([^"]+) gives free shipping to every order$/ |
||
| 312 | */ |
||
| 313 | public function thePromotionGivesFreeShippingToEveryOrder(PromotionInterface $promotion) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @Given /^([^"]+) gives ("[^"]+%") off every product (classified as "[^"]+")$/ |
||
| 320 | */ |
||
| 321 | public function itGivesPercentageOffEveryProductClassifiedAs( |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product (classified as "[^"]+")$/ |
||
| 331 | */ |
||
| 332 | public function itGivesFixedOffEveryProductClassifiedAs( |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product with minimum price at ("(?:€|£|\$)[^"]+")$/ |
||
| 342 | */ |
||
| 343 | public function thisPromotionGivesOffOnEveryProductWithMinimumPriceAt( |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product priced between ("(?:€|£|\$)[^"]+") and ("(?:€|£|\$)[^"]+")$/ |
||
| 353 | */ |
||
| 354 | public function thisPromotionGivesOffOnEveryProductPricedBetween( |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @Given /^([^"]+) gives ("[^"]+%") off on every product with minimum price at ("(?:€|£|\$)[^"]+")$/ |
||
| 369 | */ |
||
| 370 | public function thisPromotionPercentageGivesOffOnEveryProductWithMinimumPriceAt( |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @Given /^([^"]+) gives ("[^"]+%") off on every product priced between ("(?:€|£|\$)[^"]+") and ("(?:€|£|\$)[^"]+")$/ |
||
| 380 | */ |
||
| 381 | public function thisPromotionPercentageGivesOffOnEveryProductPricedBetween( |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+")$/ |
||
| 396 | */ |
||
| 397 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAs( |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+" or "[^"]+")$/ |
||
| 409 | */ |
||
| 410 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAsOr( |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+") with a minimum value of ("(?:€|£|\$)[^"]+")$/ |
||
| 422 | */ |
||
| 423 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAsAndPricedAt( |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off customer's (\d)(?:st|nd|rd|th) order$/ |
||
| 436 | */ |
||
| 437 | public function itGivesFixedOffCustomersNthOrder(PromotionInterface $promotion, $discount, $nth) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @Given /^([^"]+) gives ("[^"]+%") off on the customer's (\d)(?:st|nd|rd|th) order$/ |
||
| 446 | */ |
||
| 447 | public function itGivesPercentageOffCustomersNthOrder(PromotionInterface $promotion, $discount, $nth) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") and ("(?:€|£|\$)[^"]+") discount on every order$/ |
||
| 456 | */ |
||
| 457 | public function itGivesPercentageOffOnEveryProductClassifiedAsAndAmountDiscountOnOrder( |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product (classified as "[^"]+") and a free shipping to every order with items total equal at least ("[^"]+")$/ |
||
| 469 | */ |
||
| 470 | public function itGivesOffOnEveryProductClassifiedAsAndAFreeShippingToEveryOrderWithItemsTotalEqualAtLeast( |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") and a ("(?:€|£|\$)[^"]+") discount to every order with items total equal at least ("(?:€|£|\$)[^"]+")$/ |
||
| 486 | */ |
||
| 487 | public function itGivesOffOnEveryProductClassifiedAsAndAFixedDiscountToEveryOrderWithItemsTotalEqualAtLeast( |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+" or "[^"]+") if order contains any product (classified as "[^"]+" or "[^"]+")$/ |
||
| 509 | */ |
||
| 510 | public function itGivesOffOnEveryProductClassifiedAsOrIfOrderContainsAnyProductClassifiedAsOr( |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") if order contains any product (classified as "[^"]+")$/ |
||
| 531 | */ |
||
| 532 | public function itGivesOffOnEveryProductClassifiedAsIfOrderContainsAnyProductClassifiedAs( |
||
| 547 | |||
| 548 | /** |
||
| 549 | * @Given /^(it) is coupon based promotion$/ |
||
| 550 | */ |
||
| 551 | public function itIsCouponBasedPromotion(PromotionInterface $promotion) |
||
| 557 | |||
| 558 | /** |
||
| 559 | * @Given /^(the promotion) was disabled for the (channel "[^"]+")$/ |
||
| 560 | */ |
||
| 561 | public function thePromotionWasDisabledForTheChannel(PromotionInterface $promotion, ChannelInterface $channel) |
||
| 567 | |||
| 568 | /** |
||
| 569 | * @Given /^the (coupon "[^"]+") was used up to its usage limit$/ |
||
| 570 | */ |
||
| 571 | public function theCouponWasUsed(PromotionCouponInterface $coupon) |
||
| 577 | |||
| 578 | /** |
||
| 579 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains (?:a|an) ("[^"]+" product)$/ |
||
| 580 | */ |
||
| 581 | public function thePromotionGivesOffIfOrderContainsProducts(PromotionInterface $promotion, $discount, ProductInterface $product) |
||
| 587 | |||
| 588 | /** |
||
| 589 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on a ("[^"]*" product)$/ |
||
| 590 | */ |
||
| 591 | public function itGivesFixedDiscountOffOnAProduct(PromotionInterface $promotion, $discount, ProductInterface $product) |
||
| 595 | |||
| 596 | /** |
||
| 597 | * @Given /^([^"]+) gives ("[^"]+%") off on a ("[^"]*" product)$/ |
||
| 598 | */ |
||
| 599 | public function itGivesPercentageDiscountOffOnAProduct(PromotionInterface $promotion, $discount, ProductInterface $product) |
||
| 603 | |||
| 604 | /** |
||
| 605 | * @param array $taxonCodes |
||
| 606 | * |
||
| 607 | * @return array |
||
| 608 | */ |
||
| 609 | private function getTaxonFilterConfiguration(array $taxonCodes) |
||
| 613 | |||
| 614 | /** |
||
| 615 | * @param array $productCodes |
||
| 616 | * |
||
| 617 | * @return array |
||
| 618 | */ |
||
| 619 | private function getProductsFilterConfiguration(array $productCodes) |
||
| 623 | |||
| 624 | /** |
||
| 625 | * @param int $minAmount |
||
| 626 | * @param int $maxAmount |
||
| 627 | * |
||
| 628 | * @return array |
||
| 629 | */ |
||
| 630 | private function getPriceRangeFilterConfiguration($minAmount, $maxAmount = null) |
||
| 639 | |||
| 640 | /** |
||
| 641 | * @param PromotionInterface $promotion |
||
| 642 | * @param int $discount |
||
| 643 | * @param array $configuration |
||
| 644 | */ |
||
| 645 | private function createUnitFixedPromotion(PromotionInterface $promotion, $discount, array $configuration = [], $rule = null) |
||
| 649 | |||
| 650 | /** |
||
| 651 | * @param PromotionInterface $promotion |
||
| 652 | * @param int $discount |
||
| 653 | * @param array $configuration |
||
| 654 | */ |
||
| 655 | private function createUnitPercentagePromotion(PromotionInterface $promotion, $discount, array $configuration = [], $rule = null) |
||
| 659 | |||
| 660 | /** |
||
| 661 | * @param PromotionInterface $promotion |
||
| 662 | * @param int $discount |
||
| 663 | * @param array $configuration |
||
| 664 | * @param PromotionRuleInterface $rule |
||
| 665 | */ |
||
| 666 | private function createFixedPromotion(PromotionInterface $promotion, $discount, array $configuration = [], PromotionRuleInterface $rule = null) |
||
| 670 | |||
| 671 | /** |
||
| 672 | * @param PromotionInterface $promotion |
||
| 673 | * @param float $discount |
||
| 674 | * @param array $configuration |
||
| 675 | * @param PromotionRuleInterface $rule |
||
| 676 | */ |
||
| 677 | private function createPercentagePromotion(PromotionInterface $promotion, $discount, array $configuration = [], PromotionRuleInterface $rule = null) |
||
| 681 | |||
| 682 | /** |
||
| 683 | * @param PromotionInterface $promotion |
||
| 684 | * @param PromotionActionInterface $action |
||
| 685 | * @param array $configuration |
||
| 686 | * @param PromotionRuleInterface|null $rule |
||
| 687 | */ |
||
| 688 | private function persistPromotion(PromotionInterface $promotion, PromotionActionInterface $action, array $configuration, PromotionRuleInterface $rule = null) |
||
| 700 | } |
||
| 701 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.