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 | * Set the order to apply this condition. If no argument order is applied we return 0 as |
||
98 | * indicator that no assignment has been made |
||
99 | * @param int $order |
||
100 | * @return Integer |
||
101 | */ |
||
102 | public function setOrder($order = 1) |
||
106 | |||
107 | /** |
||
108 | * the order to apply this condition. If no argument order is applied we return 0 as |
||
109 | * indicator that no assignment has been made |
||
110 | * |
||
111 | * @return Integer |
||
112 | */ |
||
113 | public function getOrder() |
||
117 | |||
118 | /** |
||
119 | * apply condition to total or subtotal |
||
120 | * |
||
121 | * @param $totalOrSubTotalOrPrice |
||
122 | * @return float |
||
123 | */ |
||
124 | public function applyCondition($totalOrSubTotalOrPrice) |
||
128 | |||
129 | /** |
||
130 | * get the calculated value of this condition supplied by the subtotal|price |
||
131 | * |
||
132 | * @param $totalOrSubTotalOrPrice |
||
133 | * @return mixed |
||
134 | */ |
||
135 | public function getCalculatedValue($totalOrSubTotalOrPrice) |
||
141 | |||
142 | /** |
||
143 | * apply condition |
||
144 | * |
||
145 | * @param $totalOrSubTotalOrPrice |
||
146 | * @param $conditionValue |
||
147 | * @return float |
||
148 | */ |
||
149 | protected function apply($totalOrSubTotalOrPrice, $conditionValue) |
||
211 | |||
212 | /** |
||
213 | * check if value is a percentage |
||
214 | * |
||
215 | * @param $value |
||
216 | * @return bool |
||
217 | */ |
||
218 | protected function valueIsPercentage($value) |
||
222 | |||
223 | /** |
||
224 | * check if value is a subtract |
||
225 | * |
||
226 | * @param $value |
||
227 | * @return bool |
||
228 | */ |
||
229 | protected function valueIsToBeSubtracted($value) |
||
233 | |||
234 | /** |
||
235 | * check if value is to be added |
||
236 | * |
||
237 | * @param $value |
||
238 | * @return bool |
||
239 | */ |
||
240 | protected function valueIsToBeAdded($value) |
||
244 | |||
245 | /** |
||
246 | * removes some arithmetic signs (%,+,-) only |
||
247 | * |
||
248 | * @param $value |
||
249 | * @return mixed |
||
250 | */ |
||
251 | protected function cleanValue($value) |
||
255 | |||
256 | /** |
||
257 | * validates condition arguments |
||
258 | * |
||
259 | * @param $args |
||
260 | * @throws InvalidConditionException |
||
261 | */ |
||
262 | View Code Duplication | protected function validate($args) |
|
277 | } |
||
278 |