Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php namespace Darryldecode\Cart; |
||
13 | class CartCondition { |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | private $args; |
||
19 | |||
20 | /** |
||
21 | * the parsed raw value of the condition |
||
22 | * |
||
23 | * @var |
||
24 | */ |
||
25 | public $parsedRawValue; |
||
26 | |||
27 | /** |
||
28 | * @param array $args (name, type, target, value) |
||
29 | * @throws InvalidConditionException |
||
30 | */ |
||
31 | public function __construct(array $args) |
||
44 | |||
45 | /** |
||
46 | * the target of where the condition is applied. |
||
47 | * NOTE: On conditions added to per item bases, target is not needed. |
||
48 | * |
||
49 | * @return mixed |
||
50 | */ |
||
51 | public function getTarget() |
||
55 | |||
56 | /** |
||
57 | * the name of the condition |
||
58 | * |
||
59 | * @return mixed |
||
60 | */ |
||
61 | public function getName() |
||
65 | |||
66 | /** |
||
67 | * the type of the condition |
||
68 | * |
||
69 | * @return mixed |
||
70 | */ |
||
71 | public function getType() |
||
75 | |||
76 | /** |
||
77 | * get the additional attributes of a condition |
||
78 | * |
||
79 | * @return array |
||
80 | */ |
||
81 | public function getAttributes() |
||
85 | |||
86 | /** |
||
87 | * the value of this the condition |
||
88 | * |
||
89 | * @return mixed |
||
90 | */ |
||
91 | public function getValue() |
||
95 | |||
96 | /** |
||
97 | * the quantity of this the condition |
||
98 | * |
||
99 | * @return mixed |
||
100 | */ |
||
101 | public function getQuantity() |
||
105 | |||
106 | /** |
||
107 | * Set the order to apply this condition. If no argument order is applied we return 0 as |
||
108 | * indicator that no assignment has been made |
||
109 | * @param int $order |
||
110 | * @return Integer |
||
111 | */ |
||
112 | public function setOrder($order = 1) |
||
116 | |||
117 | /** |
||
118 | * the order to apply this condition. If no argument order is applied we return 0 as |
||
119 | * indicator that no assignment has been made |
||
120 | * |
||
121 | * @return Integer |
||
122 | */ |
||
123 | public function getOrder() |
||
127 | |||
128 | /** |
||
129 | * apply condition to total or subtotal |
||
130 | * |
||
131 | * @param $totalOrSubTotalOrPrice |
||
132 | * @return float |
||
133 | */ |
||
134 | public function applyCondition($totalOrSubTotalOrPrice) |
||
138 | |||
139 | /** |
||
140 | * get the calculated value of this condition supplied by the subtotal|price |
||
141 | * |
||
142 | * @param $totalOrSubTotalOrPrice |
||
143 | * @return mixed |
||
144 | */ |
||
145 | public function getCalculatedValue($totalOrSubTotalOrPrice) |
||
151 | |||
152 | /** |
||
153 | * apply condition |
||
154 | * |
||
155 | * @param $totalOrSubTotalOrPrice |
||
156 | * @param $conditionValue |
||
157 | * @return float |
||
158 | */ |
||
159 | protected function apply($totalOrSubTotalOrPrice, $conditionValue) |
||
160 | { |
||
161 | // if type of condition is multiply we make count it or |
||
162 | // if value has a percentage sign on it, we will get first |
||
163 | // its percentage then we will evaluate again if the value |
||
164 | // has a minus or plus sign so we can decide what to do with the |
||
165 | // percentage, whether to add or subtract it to the total/subtotal/price |
||
166 | // if we can't find any plus/minus sign, we will assume it as plus sign |
||
167 | if( $this->getType() == 'multiply' ){ |
||
168 | $result = $totalOrSubTotalOrPrice + ( $this->getValue() * $this->getQuantity() ); |
||
169 | } |
||
170 | else if( $this->valueIsPercentage($conditionValue) ) |
||
171 | { |
||
172 | if( $this->valueIsToBeSubtracted($conditionValue) ) |
||
173 | { |
||
174 | $value = Helpers::normalizePrice( $this->cleanValue($conditionValue) ); |
||
175 | |||
176 | $this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100); |
||
177 | |||
178 | $result = floatval($totalOrSubTotalOrPrice - $this->parsedRawValue); |
||
179 | } |
||
180 | else if ( $this->valueIsToBeAdded($conditionValue) ) |
||
181 | { |
||
182 | $value = Helpers::normalizePrice( $this->cleanValue($conditionValue) ); |
||
183 | |||
184 | $this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100); |
||
185 | |||
186 | $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); |
||
187 | } |
||
188 | else |
||
189 | { |
||
190 | $value = Helpers::normalizePrice($conditionValue); |
||
191 | |||
192 | $this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100); |
||
193 | |||
194 | $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); |
||
195 | } |
||
196 | } |
||
197 | |||
198 | // if the value has no percent sign on it, the operation will not be a percentage |
||
199 | // next is we will check if it has a minus/plus sign so then we can just deduct it to total/subtotal/price |
||
200 | else |
||
201 | { |
||
202 | if( $this->valueIsToBeSubtracted($conditionValue) ) |
||
203 | { |
||
204 | $this->parsedRawValue = Helpers::normalizePrice( $this->cleanValue($conditionValue) ); |
||
205 | |||
206 | $result = floatval($totalOrSubTotalOrPrice - $this->parsedRawValue); |
||
207 | } |
||
208 | else if ( $this->valueIsToBeAdded($conditionValue) ) |
||
209 | { |
||
210 | $this->parsedRawValue = Helpers::normalizePrice( $this->cleanValue($conditionValue) ); |
||
211 | |||
212 | $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); |
||
213 | } |
||
214 | else |
||
215 | { |
||
216 | $this->parsedRawValue = Helpers::normalizePrice($conditionValue); |
||
217 | |||
218 | $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); |
||
219 | } |
||
220 | } |
||
221 | |||
222 | // Do not allow items with negative prices. |
||
223 | return $result < 0 ? 0.00 : $result; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * check if value is a percentage |
||
228 | * |
||
229 | * @param $value |
||
230 | * @return bool |
||
231 | */ |
||
232 | protected function valueIsPercentage($value) |
||
236 | |||
237 | /** |
||
238 | * check if value is a subtract |
||
239 | * |
||
240 | * @param $value |
||
241 | * @return bool |
||
242 | */ |
||
243 | protected function valueIsToBeSubtracted($value) |
||
247 | |||
248 | /** |
||
249 | * check if value is to be added |
||
250 | * |
||
251 | * @param $value |
||
252 | * @return bool |
||
253 | */ |
||
254 | protected function valueIsToBeAdded($value) |
||
258 | |||
259 | /** |
||
260 | * removes some arithmetic signs (%,+,-) only |
||
261 | * |
||
262 | * @param $value |
||
263 | * @return mixed |
||
264 | */ |
||
265 | protected function cleanValue($value) |
||
269 | |||
270 | /** |
||
271 | * validates condition arguments |
||
272 | * |
||
273 | * @param $args |
||
274 | * @throws InvalidConditionException |
||
275 | */ |
||
276 | View Code Duplication | protected function validate($args) |
|
291 | } |
||
292 |