Conditions | 6 |
Paths | 12 |
Total Lines | 61 |
Code Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php declare(strict_types=1); |
||
83 | private function load(SalesChannelContext $context, Cart $cart, CartBehavior $behaviorContext): RuleLoaderResult |
||
84 | { |
||
85 | $rules = $this->loadRules($context->getContext()); |
||
86 | |||
87 | // save all rules for later usage |
||
88 | $all = $rules; |
||
89 | |||
90 | // update rules in current context |
||
91 | $context->setRuleIds($rules->getIds()); |
||
92 | |||
93 | $iteration = 1; |
||
94 | |||
95 | // start first cart calculation to have all objects enriched |
||
96 | $cart = $this->processor->process($cart, $context, $behaviorContext); |
||
97 | |||
98 | do { |
||
99 | $compare = $cart; |
||
100 | |||
101 | if ($iteration > self::MAX_ITERATION) { |
||
102 | break; |
||
103 | } |
||
104 | |||
105 | // filter rules which matches to current scope |
||
106 | $rules = $rules->filterMatchingRules($cart, $context); |
||
107 | |||
108 | // update matching rules in context |
||
109 | $context->setRuleIds($rules->getIds()); |
||
110 | |||
111 | // calculate cart again |
||
112 | $cart = $this->processor->process($cart, $context, $behaviorContext); |
||
113 | |||
114 | // check if the cart changed, in this case we have to recalculate the cart again |
||
115 | $recalculate = $this->cartChanged($cart, $compare); |
||
116 | |||
117 | // check if rules changed for the last calculated cart, in this case we have to recalculate |
||
118 | $ruleCompare = $all->filterMatchingRules($cart, $context); |
||
119 | |||
120 | if (!$rules->equals($ruleCompare)) { |
||
121 | $recalculate = true; |
||
122 | $rules = $ruleCompare; |
||
123 | } |
||
124 | |||
125 | ++$iteration; |
||
126 | } while ($recalculate); |
||
127 | |||
128 | $index = 0; |
||
129 | foreach ($rules as $rule) { |
||
130 | ++$index; |
||
131 | $this->logger->info( |
||
132 | sprintf('#%s Rule detection: %s with priority %s (id: %s)', $index, $rule->getName(), $rule->getPriority(), $rule->getId()) |
||
133 | ); |
||
134 | } |
||
135 | |||
136 | $context->setRuleIds($rules->getIds()); |
||
137 | |||
138 | // save the cart if errors exist, so the errors get persisted |
||
139 | if ($cart->getErrors()->count() > 0) { |
||
140 | $this->cartPersister->save($cart, $context); |
||
141 | } |
||
142 | |||
143 | return new RuleLoaderResult($cart, $rules); |
||
144 | } |
||
180 |