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) |
||
| 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 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.