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 :promotionName limited to :usageLimit usages |
||
117 | */ |
||
118 | public function thereIsPromotionLimitedToUsages($promotionName, $usageLimit) |
||
127 | |||
128 | /** |
||
129 | * @Given the store has promotion :promotionName with coupon :couponCode |
||
130 | * @Given the store has also promotion :promotionName with coupon :couponCode |
||
131 | * @Given the store has a promotion :promotionName with a coupon :couponCode that is limited to :usageLimit usages |
||
132 | */ |
||
133 | public function thereIsPromotionWithCoupon($promotionName, $couponCode, $usageLimit = null) |
||
151 | |||
152 | /** |
||
153 | * @Given /^(this promotion) has already expired$/ |
||
154 | */ |
||
155 | public function thisPromotionHasExpired(PromotionInterface $promotion) |
||
161 | |||
162 | /** |
||
163 | * @Given /^(this coupon) has already expired$/ |
||
164 | */ |
||
165 | public function thisCouponHasExpired(PromotionCouponInterface $coupon) |
||
171 | |||
172 | /** |
||
173 | * @Given /^(this coupon) expires tomorrow$/ |
||
174 | */ |
||
175 | public function thisCouponExpiresTomorrow(PromotionCouponInterface $coupon) |
||
181 | |||
182 | /** |
||
183 | * @Given /^(this coupon) has already reached its usage limit$/ |
||
184 | */ |
||
185 | public function thisCouponHasReachedItsUsageLimit(PromotionCouponInterface $coupon) |
||
192 | |||
193 | /** |
||
194 | * @Given /^(this coupon) can be used (\d+) times?$/ |
||
195 | */ |
||
196 | public function thisCouponCanBeUsedNTimes(PromotionCouponInterface $coupon, $usageLimit) |
||
202 | |||
203 | /** |
||
204 | * @Given /^(this coupon) can be used twice per customer$/ |
||
205 | */ |
||
206 | public function thisCouponCanBeUsedTwicePerCustomer(PromotionCouponInterface $coupon) |
||
212 | |||
213 | /** |
||
214 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order$/ |
||
215 | */ |
||
216 | public function itGivesFixedDiscountToEveryOrder(PromotionInterface $promotion, $discount) |
||
220 | |||
221 | /** |
||
222 | * @Given /^([^"]+) gives ("[^"]+%") discount to every order$/ |
||
223 | */ |
||
224 | public function itGivesPercentageDiscountToEveryOrder(PromotionInterface $promotion, $discount) |
||
228 | |||
229 | /** |
||
230 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order with quantity at least ([^"]+)$/ |
||
231 | */ |
||
232 | public function itGivesFixedDiscountToEveryOrderWithQuantityAtLeast( |
||
241 | |||
242 | /** |
||
243 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order with items total at least ("[^"]+")$/ |
||
244 | */ |
||
245 | public function itGivesFixedDiscountToEveryOrderWithItemsTotalAtLeast( |
||
254 | |||
255 | /** |
||
256 | * @Given /^([^"]+) gives ("[^"]+%") off on every product when the item total is at least ("(?:€|£|\$)[^"]+")$/ |
||
257 | */ |
||
258 | public function itGivesOffOnEveryItemWhenItemTotalExceeds( |
||
267 | |||
268 | /** |
||
269 | * @Given /^([^"]+) gives ("[^"]+%") discount on shipping to every order$/ |
||
270 | */ |
||
271 | public function itGivesPercentageDiscountOnShippingToEveryOrder(PromotionInterface $promotion, $discount) |
||
278 | |||
279 | /** |
||
280 | * @Given /^([^"]+) gives free shipping to every order$/ |
||
281 | */ |
||
282 | public function thePromotionGivesFreeShippingToEveryOrder(PromotionInterface $promotion) |
||
286 | |||
287 | /** |
||
288 | * @Given /^([^"]+) gives ("[^"]+%") off every product (classified as "[^"]+")$/ |
||
289 | */ |
||
290 | public function itGivesPercentageOffEveryProductClassifiedAs( |
||
297 | |||
298 | /** |
||
299 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product (classified as "[^"]+")$/ |
||
300 | */ |
||
301 | public function itGivesFixedOffEveryProductClassifiedAs( |
||
308 | |||
309 | /** |
||
310 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product with minimum price at ("(?:€|£|\$)[^"]+")$/ |
||
311 | */ |
||
312 | public function thisPromotionGivesOffOnEveryProductWithMinimumPriceAt( |
||
319 | |||
320 | /** |
||
321 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product priced between ("(?:€|£|\$)[^"]+") and ("(?:€|£|\$)[^"]+")$/ |
||
322 | */ |
||
323 | public function thisPromotionGivesOffOnEveryProductPricedBetween( |
||
335 | |||
336 | /** |
||
337 | * @Given /^([^"]+) gives ("[^"]+%") off on every product with minimum price at ("(?:€|£|\$)[^"]+")$/ |
||
338 | */ |
||
339 | public function thisPromotionPercentageGivesOffOnEveryProductWithMinimumPriceAt( |
||
346 | |||
347 | /** |
||
348 | * @Given /^([^"]+) gives ("[^"]+%") off on every product priced between ("(?:€|£|\$)[^"]+") and ("(?:€|£|\$)[^"]+")$/ |
||
349 | */ |
||
350 | public function thisPromotionPercentageGivesOffOnEveryProductPricedBetween( |
||
362 | |||
363 | /** |
||
364 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+")$/ |
||
365 | */ |
||
366 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAs( |
||
375 | |||
376 | /** |
||
377 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+" or "[^"]+")$/ |
||
378 | */ |
||
379 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAsOr( |
||
388 | |||
389 | /** |
||
390 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+") with a minimum value of ("(?:€|£|\$)[^"]+")$/ |
||
391 | */ |
||
392 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAsAndPricedAt( |
||
402 | |||
403 | /** |
||
404 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains (\d+) products (classified as "[^"]+")$/ |
||
405 | */ |
||
406 | public function thePromotionGivesOffIfOrderContainsNumberOfProductsClassifiedAs( |
||
416 | |||
417 | /** |
||
418 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off customer's (\d)(?:st|nd|rd|th) order$/ |
||
419 | */ |
||
420 | public function itGivesFixedOffCustomersNthOrder(PromotionInterface $promotion, $discount, $nth) |
||
426 | |||
427 | /** |
||
428 | * @Given /^([^"]+) gives ("[^"]+%") off on the customer's (\d)(?:st|nd|rd|th) order$/ |
||
429 | */ |
||
430 | public function itGivesPercentageOffCustomersNthOrder(PromotionInterface $promotion, $discount, $nth) |
||
436 | |||
437 | /** |
||
438 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") if an order contains any product (classified as "[^"]+")$/ |
||
439 | */ |
||
440 | public function itGivesPercentageOffOnEveryProductClassifiedAsIfAnOrderContainsAnyProductClassifiedAs( |
||
450 | |||
451 | /** |
||
452 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") and ("(?:€|£|\$)[^"]+") discount on every order$/ |
||
453 | */ |
||
454 | public function itGivesPercentageOffOnEveryProductClassifiedAsAndAmountDiscountOnOrder( |
||
463 | |||
464 | /** |
||
465 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product (classified as "[^"]+") and a free shipping to every order with items total equal at least ("[^"]+")$/ |
||
466 | */ |
||
467 | public function itGivesOffOnEveryProductClassifiedAsAndAFreeShippingToEveryOrderWithItemsTotalEqualAtLeast( |
||
480 | |||
481 | /** |
||
482 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") and a ("(?:€|£|\$)[^"]+") discount to every order with items total equal at least ("(?:€|£|\$)[^"]+")$/ |
||
483 | */ |
||
484 | public function itGivesOffOnEveryProductClassifiedAsAndAFixedDiscountToEveryOrderWithItemsTotalEqualAtLeast( |
||
503 | |||
504 | /** |
||
505 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+" or "[^"]+") if order contains any product (classified as "[^"]+" or "[^"]+")$/ |
||
506 | */ |
||
507 | public function itGivesOffOnEveryProductClassifiedAsOrIfOrderContainsAnyProductClassifiedAsOr( |
||
525 | |||
526 | /** |
||
527 | * @Given /^(it) is coupon based promotion$/ |
||
528 | */ |
||
529 | public function itIsCouponBasedPromotion(PromotionInterface $promotion) |
||
535 | |||
536 | /** |
||
537 | * @Given /^(the promotion) was disabled for the (channel "[^"]+")$/ |
||
538 | */ |
||
539 | public function thePromotionWasDisabledForTheChannel(PromotionInterface $promotion, ChannelInterface $channel) |
||
545 | |||
546 | /** |
||
547 | * @Given /^the (coupon "[^"]+") was used up to its usage limit$/ |
||
548 | */ |
||
549 | public function theCouponWasUsed(PromotionCouponInterface $coupon) |
||
555 | |||
556 | /** |
||
557 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains (?:a|an) ("[^"]+" product)$/ |
||
558 | */ |
||
559 | public function thePromotionGivesOffIfOrderContainsProducts(PromotionInterface $promotion, $discount, ProductInterface $product) |
||
565 | |||
566 | /** |
||
567 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on a ("[^"]*" product)$/ |
||
568 | */ |
||
569 | public function itGivesFixedDiscountOffOnAProduct(PromotionInterface $promotion, $discount, ProductInterface $product) |
||
573 | |||
574 | /** |
||
575 | * @Given /^([^"]+) gives ("[^"]+%") off on a ("[^"]*" product)$/ |
||
576 | */ |
||
577 | public function itGivesPercentageDiscountOffOnAProduct(PromotionInterface $promotion, $discount, ProductInterface $product) |
||
581 | |||
582 | /** |
||
583 | * @param array $taxonCodes |
||
584 | * |
||
585 | * @return array |
||
586 | */ |
||
587 | private function getTaxonFilterConfiguration(array $taxonCodes) |
||
591 | |||
592 | /** |
||
593 | * @param array $productCodes |
||
594 | * |
||
595 | * @return array |
||
596 | */ |
||
597 | private function getProductsFilterConfiguration(array $productCodes) |
||
601 | |||
602 | /** |
||
603 | * @param int $minAmount |
||
604 | * @param int $maxAmount |
||
605 | * |
||
606 | * @return array |
||
607 | */ |
||
608 | private function getPriceRangeFilterConfiguration($minAmount, $maxAmount = null) |
||
617 | |||
618 | /** |
||
619 | * @param PromotionInterface $promotion |
||
620 | * @param int $discount |
||
621 | * @param array $configuration |
||
622 | */ |
||
623 | private function createUnitFixedPromotion(PromotionInterface $promotion, $discount, array $configuration = [], $rule = null) |
||
627 | |||
628 | /** |
||
629 | * @param PromotionInterface $promotion |
||
630 | * @param int $discount |
||
631 | * @param array $configuration |
||
632 | */ |
||
633 | private function createUnitPercentagePromotion(PromotionInterface $promotion, $discount, array $configuration = [], $rule = null) |
||
637 | |||
638 | /** |
||
639 | * @param PromotionInterface $promotion |
||
640 | * @param int $discount |
||
641 | * @param array $configuration |
||
642 | * @param PromotionRuleInterface $rule |
||
643 | */ |
||
644 | private function createFixedPromotion(PromotionInterface $promotion, $discount, array $configuration = [], PromotionRuleInterface $rule = null) |
||
648 | |||
649 | /** |
||
650 | * @param PromotionInterface $promotion |
||
651 | * @param float $discount |
||
652 | * @param array $configuration |
||
653 | * @param PromotionRuleInterface $rule |
||
654 | */ |
||
655 | private function createPercentagePromotion(PromotionInterface $promotion, $discount, array $configuration = [], PromotionRuleInterface $rule = null) |
||
659 | |||
660 | /** |
||
661 | * @param PromotionInterface $promotion |
||
662 | * @param PromotionActionInterface $action |
||
663 | * @param array $configuration |
||
664 | * @param PromotionRuleInterface|null $rule |
||
665 | */ |
||
666 | private function persistPromotion(PromotionInterface $promotion, PromotionActionInterface $action, array $configuration, PromotionRuleInterface $rule = null) |
||
678 | } |
||
679 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.