Conditions | 7 |
Paths | 12 |
Total Lines | 62 |
Code Lines | 25 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 2 |
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 namespace Darryldecode\Cart; |
||
148 | protected function apply($totalOrSubTotalOrPrice, $conditionValue) |
||
149 | { |
||
150 | // if value has a percentage sign on it, we will get first |
||
151 | // its percentage then we will evaluate again if the value |
||
152 | // has a minus or plus sign so we can decide what to do with the |
||
153 | // percentage, whether to add or subtract it to the total/subtotal/price |
||
154 | // if we can't find any plus/minus sign, we will assume it as plus sign |
||
155 | if( $this->valueIsPercentage($conditionValue) ) |
||
156 | { |
||
157 | if( $this->valueIsToBeSubtracted($conditionValue) ) |
||
158 | { |
||
159 | $value = Helpers::normalizePrice( $this->cleanValue($conditionValue) ); |
||
160 | |||
161 | $this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100); |
||
162 | |||
163 | $result = floatval($totalOrSubTotalOrPrice - $this->parsedRawValue); |
||
164 | } |
||
165 | else if ( $this->valueIsToBeAdded($conditionValue) ) |
||
166 | { |
||
167 | $value = Helpers::normalizePrice( $this->cleanValue($conditionValue) ); |
||
168 | |||
169 | $this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100); |
||
170 | |||
171 | $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); |
||
172 | } |
||
173 | else |
||
174 | { |
||
175 | $value = Helpers::normalizePrice($conditionValue); |
||
176 | |||
177 | $this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100); |
||
178 | |||
179 | $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | // if the value has no percent sign on it, the operation will not be a percentage |
||
184 | // next is we will check if it has a minus/plus sign so then we can just deduct it to total/subtotal/price |
||
185 | else |
||
186 | { |
||
187 | if( $this->valueIsToBeSubtracted($conditionValue) ) |
||
188 | { |
||
189 | $this->parsedRawValue = Helpers::normalizePrice( $this->cleanValue($conditionValue) ); |
||
190 | |||
191 | $result = floatval($totalOrSubTotalOrPrice - $this->parsedRawValue); |
||
192 | } |
||
193 | else if ( $this->valueIsToBeAdded($conditionValue) ) |
||
194 | { |
||
195 | $this->parsedRawValue = Helpers::normalizePrice( $this->cleanValue($conditionValue) ); |
||
196 | |||
197 | $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); |
||
198 | } |
||
199 | else |
||
200 | { |
||
201 | $this->parsedRawValue = Helpers::normalizePrice($conditionValue); |
||
202 | |||
203 | $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); |
||
204 | } |
||
205 | } |
||
206 | |||
207 | // Do not allow items with negative prices. |
||
208 | return $result < 0 ? 0.00 : $result; |
||
209 | } |
||
210 | |||
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.