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 | /** |
||
28 | * @param array $args (name, type, target, value) |
||
29 | * @throws InvalidConditionException |
||
30 | */ |
||
31 | public function __construct(array $args) |
||
32 | { |
||
33 | $this->args = $args; |
||
34 | |||
35 | if( Helpers::isMultiArray($args) ) |
||
36 | { |
||
37 | Throw new InvalidConditionException('Multi dimensional array is not supported.'); |
||
38 | } |
||
39 | else |
||
40 | { |
||
41 | $this->validate($this->args); |
||
42 | } |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * the target of where the condition is applied |
||
47 | * |
||
48 | * @return mixed |
||
49 | */ |
||
50 | public function getTarget() |
||
54 | |||
55 | /** |
||
56 | * the name of the condition |
||
57 | * |
||
58 | * @return mixed |
||
59 | */ |
||
60 | public function getName() |
||
64 | |||
65 | /** |
||
66 | * the type of the condition |
||
67 | * |
||
68 | * @return mixed |
||
69 | */ |
||
70 | public function getType() |
||
74 | |||
75 | /** |
||
76 | * get the additional attributes of a condition |
||
77 | * |
||
78 | * @return array |
||
79 | */ |
||
80 | public function getAttributes() |
||
84 | |||
85 | /** |
||
86 | * the value of this the condition |
||
87 | * |
||
88 | * @return mixed |
||
89 | */ |
||
90 | public function getValue() |
||
94 | |||
95 | /** |
||
96 | * Set the order to apply this condition. If no argument order is applied we return 0 as |
||
97 | * indicator that no assignment has been made |
||
98 | * @param int $order |
||
99 | * @return Integer |
||
100 | */ |
||
101 | public function setOrder($order = 1) |
||
105 | |||
106 | /** |
||
107 | * 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 | * |
||
110 | * @return Integer |
||
111 | */ |
||
112 | public function getOrder() |
||
116 | |||
117 | /** |
||
118 | * apply condition to total or subtotal |
||
119 | * |
||
120 | * @param $totalOrSubTotalOrPrice |
||
121 | * @return float |
||
122 | */ |
||
123 | public function applyCondition($totalOrSubTotalOrPrice) |
||
127 | |||
128 | /** |
||
129 | * get the calculated value of this condition supplied by the subtotal|price |
||
130 | * |
||
131 | * @param $totalOrSubTotalOrPrice |
||
132 | * @return mixed |
||
133 | */ |
||
134 | public function getCalculatedValue($totalOrSubTotalOrPrice) |
||
140 | |||
141 | /** |
||
142 | * apply condition |
||
143 | * |
||
144 | * @param $totalOrSubTotalOrPrice |
||
145 | * @param $conditionValue |
||
146 | * @return float |
||
147 | */ |
||
148 | protected function apply($totalOrSubTotalOrPrice, $conditionValue) |
||
210 | |||
211 | /** |
||
212 | * check if value is a percentage |
||
213 | * |
||
214 | * @param $value |
||
215 | * @return bool |
||
216 | */ |
||
217 | protected function valueIsPercentage($value) |
||
221 | |||
222 | /** |
||
223 | * check if value is a subtract |
||
224 | * |
||
225 | * @param $value |
||
226 | * @return bool |
||
227 | */ |
||
228 | protected function valueIsToBeSubtracted($value) |
||
232 | |||
233 | /** |
||
234 | * check if value is to be added |
||
235 | * |
||
236 | * @param $value |
||
237 | * @return bool |
||
238 | */ |
||
239 | protected function valueIsToBeAdded($value) |
||
243 | |||
244 | /** |
||
245 | * removes some arithmetic signs (%,+,-) only |
||
246 | * |
||
247 | * @param $value |
||
248 | * @return mixed |
||
249 | */ |
||
250 | protected function cleanValue($value) |
||
254 | |||
255 | /** |
||
256 | * validates condition arguments |
||
257 | * |
||
258 | * @param $args |
||
259 | * @throws InvalidConditionException |
||
260 | */ |
||
261 | View Code Duplication | protected function validate($args) |
|
277 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.