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 | private $parsedRawValue; |
||
26 | |||
27 | private $config; |
||
28 | |||
29 | /** |
||
30 | * @param array $args (name, type, target, value) |
||
31 | * @throws InvalidConditionException |
||
32 | */ |
||
33 | public function __construct(array $args) |
||
48 | |||
49 | /** |
||
50 | * the target of where the condition is applied |
||
51 | * |
||
52 | * @return mixed |
||
53 | */ |
||
54 | public function getTarget() |
||
58 | |||
59 | /** |
||
60 | * the name of the condition |
||
61 | * |
||
62 | * @return mixed |
||
63 | */ |
||
64 | public function getName() |
||
68 | |||
69 | /** |
||
70 | * the type of the condition |
||
71 | * |
||
72 | * @return mixed |
||
73 | */ |
||
74 | public function getType() |
||
78 | |||
79 | /** |
||
80 | * get the additional attributes of a condition |
||
81 | * |
||
82 | * @return array |
||
83 | */ |
||
84 | public function getAttributes() |
||
88 | |||
89 | /** |
||
90 | * the value of this the condition |
||
91 | * |
||
92 | * @return mixed |
||
93 | */ |
||
94 | public function getValue() |
||
98 | |||
99 | /** |
||
100 | * Set the order to apply this condition. If no argument order is applied we return 0 as |
||
101 | * indicator that no assignment has been made |
||
102 | * @param int $order |
||
103 | * @return Integer |
||
104 | */ |
||
105 | public function setOrder($order = 1) |
||
109 | |||
110 | /** |
||
111 | * the order to apply this condition. If no argument order is applied we return 0 as |
||
112 | * indicator that no assignment has been made |
||
113 | * |
||
114 | * @return Integer |
||
115 | */ |
||
116 | public function getOrder() |
||
120 | |||
121 | /** |
||
122 | * apply condition to total or subtotal |
||
123 | * |
||
124 | * @param $totalOrSubTotalOrPrice |
||
125 | * @return float |
||
126 | */ |
||
127 | public function applyCondition($totalOrSubTotalOrPrice) |
||
131 | |||
132 | /** |
||
133 | * get the calculated value of this condition supplied by the subtotal|price |
||
134 | * |
||
135 | * @param $totalOrSubTotalOrPrice |
||
136 | * @return mixed |
||
137 | */ |
||
138 | public function getCalculatedValue($totalOrSubTotalOrPrice) |
||
144 | |||
145 | /** |
||
146 | * get the calculated value of this condition supplied by the subtotal|price |
||
147 | * |
||
148 | * @param $totalOrSubTotalOrPrice |
||
149 | * @return mixed |
||
150 | */ |
||
151 | public function getCalculatedValueFormatted($totalOrSubTotalOrPrice) |
||
156 | |||
157 | /** |
||
158 | * apply condition |
||
159 | * |
||
160 | * @param $totalOrSubTotalOrPrice |
||
161 | * @param $conditionValue |
||
162 | * @return float |
||
163 | */ |
||
164 | protected function apply($totalOrSubTotalOrPrice, $conditionValue) |
||
227 | |||
228 | /** |
||
229 | * check if value is a percentage |
||
230 | * |
||
231 | * @param $value |
||
232 | * @return bool |
||
233 | */ |
||
234 | protected function valueIsPercentage($value) |
||
238 | |||
239 | /** |
||
240 | * check if value is a subtract |
||
241 | * |
||
242 | * @param $value |
||
243 | * @return bool |
||
244 | */ |
||
245 | protected function valueIsToBeSubtracted($value) |
||
249 | |||
250 | /** |
||
251 | * check if value is to be added |
||
252 | * |
||
253 | * @param $value |
||
254 | * @return bool |
||
255 | */ |
||
256 | protected function valueIsToBeAdded($value) |
||
260 | |||
261 | /** |
||
262 | * removes some arithmetic signs (%,+,-) only |
||
263 | * |
||
264 | * @param $value |
||
265 | * @return mixed |
||
266 | */ |
||
267 | protected function cleanValue($value) |
||
271 | |||
272 | /** |
||
273 | * validates condition arguments |
||
274 | * |
||
275 | * @param $args |
||
276 | * @throws InvalidConditionException |
||
277 | */ |
||
278 | View Code Duplication | protected function validate($args) |
|
294 | |||
295 | protected function getConfig() |
||
303 | } |
||
304 |