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 |
||
| 21 | class EE_Price extends EE_Soft_Delete_Base_Class implements UsesMoneyInterface |
||
| 22 | { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param array $props_n_values incoming values |
||
| 26 | * @param string $timezone incoming timezone (if not set the timezone set for the website will be |
||
| 27 | * used.) |
||
| 28 | * @param array $date_formats incoming date_formats in an array where the first value is the |
||
| 29 | * date_format and the second value is the time format |
||
| 30 | * @param MoneyFactory $money_factory |
||
| 31 | * @return EE_Price |
||
| 32 | * @throws InvalidArgumentException |
||
| 33 | * @throws InvalidInterfaceException |
||
| 34 | * @throws InvalidDataTypeException |
||
| 35 | * @throws EE_Error |
||
| 36 | */ |
||
| 37 | View Code Duplication | public static function new_instance( |
|
| 59 | |||
| 60 | |||
| 61 | /** |
||
| 62 | * @param array $props_n_values incoming values from the database |
||
| 63 | * @param string $timezone incoming timezone as set by the model. If not set the timezone for |
||
| 64 | * the website will be used. |
||
| 65 | * @param MoneyFactory $money_factory |
||
| 66 | * @return EE_Price |
||
| 67 | * @throws InvalidArgumentException |
||
| 68 | * @throws InvalidInterfaceException |
||
| 69 | * @throws InvalidDataTypeException |
||
| 70 | * @throws EE_Error |
||
| 71 | */ |
||
| 72 | public static function new_instance_from_db( |
||
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children |
||
| 89 | * play nice |
||
| 90 | * |
||
| 91 | * @param array $fieldValues where each key is a field (ie, array key in the 2nd layer of the model's |
||
| 92 | * _fields array, (eg, EVT_ID, TXN_amount, QST_name, etc) and values are their |
||
| 93 | * values |
||
| 94 | * @param boolean $bydb a flag for setting if the class is instantiated by the corresponding db model |
||
| 95 | * or not. |
||
| 96 | * @param string $timezone indicate what timezone you want any datetime fields to be in when |
||
| 97 | * instantiating |
||
| 98 | * a EE_Base_Class object. |
||
| 99 | * @param array $date_formats An array of date formats to set on construct where first value is the |
||
| 100 | * date_format and second value is the time format. |
||
| 101 | * @param MoneyFactory $money_factory |
||
| 102 | * @throws InvalidArgumentException |
||
| 103 | * @throws InvalidInterfaceException |
||
| 104 | * @throws InvalidDataTypeException |
||
| 105 | * @throws EE_Error |
||
| 106 | */ |
||
| 107 | View Code Duplication | protected function __construct( |
|
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * Set Price type ID |
||
| 124 | * |
||
| 125 | * @access public |
||
| 126 | * @param int $PRT_ID |
||
| 127 | */ |
||
| 128 | public function set_type( $PRT_ID = 0 ) { |
||
| 131 | |||
| 132 | |||
| 133 | |||
| 134 | /** |
||
| 135 | * Set Price Amount |
||
| 136 | * |
||
| 137 | * @access public |
||
| 138 | * @param float $PRC_amount |
||
| 139 | */ |
||
| 140 | public function set_amount( $PRC_amount = 0.00 ) { |
||
| 143 | |||
| 144 | |||
| 145 | |||
| 146 | /** |
||
| 147 | * Set Price Name |
||
| 148 | * |
||
| 149 | * @access public |
||
| 150 | * @param string $PRC_name |
||
| 151 | */ |
||
| 152 | public function set_name( $PRC_name = '' ) { |
||
| 155 | |||
| 156 | |||
| 157 | |||
| 158 | /** |
||
| 159 | * Set Price Description |
||
| 160 | * |
||
| 161 | * @access public |
||
| 162 | * @param string $PRC_desc |
||
| 163 | */ |
||
| 164 | public function set_description( $PRC_desc = '' ) { |
||
| 167 | |||
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * set is_default |
||
| 172 | * |
||
| 173 | * @access public |
||
| 174 | * @param bool $PRC_is_default |
||
| 175 | */ |
||
| 176 | public function set_is_default( $PRC_is_default = FALSE ) { |
||
| 179 | |||
| 180 | |||
| 181 | |||
| 182 | /** |
||
| 183 | * set deleted |
||
| 184 | * |
||
| 185 | * @access public |
||
| 186 | * @param bool $PRC_deleted |
||
| 187 | */ |
||
| 188 | public function set_deleted( $PRC_deleted = NULL ) { |
||
| 191 | |||
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * get Price type |
||
| 196 | * @access public |
||
| 197 | * @return int |
||
| 198 | */ |
||
| 199 | public function type() { |
||
| 202 | |||
| 203 | |||
| 204 | |||
| 205 | /** |
||
| 206 | * get Price Amount |
||
| 207 | * @access public |
||
| 208 | * @return float |
||
| 209 | */ |
||
| 210 | public function amount() { |
||
| 213 | |||
| 214 | |||
| 215 | |||
| 216 | /** |
||
| 217 | * get Price Name |
||
| 218 | * @access public |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | public function name() { |
||
| 224 | |||
| 225 | |||
| 226 | |||
| 227 | /** |
||
| 228 | * get Price description |
||
| 229 | * @access public |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | public function desc() { |
||
| 235 | |||
| 236 | |||
| 237 | |||
| 238 | /** |
||
| 239 | * get overrides |
||
| 240 | * @access public |
||
| 241 | * @return int |
||
| 242 | */ |
||
| 243 | public function overrides() { |
||
| 246 | |||
| 247 | |||
| 248 | |||
| 249 | /** |
||
| 250 | * get order |
||
| 251 | * @access public |
||
| 252 | * @return int |
||
| 253 | */ |
||
| 254 | public function order() { |
||
| 257 | |||
| 258 | |||
| 259 | |||
| 260 | /** |
||
| 261 | * get the author of the price |
||
| 262 | * |
||
| 263 | * @since 4.5.0 |
||
| 264 | * |
||
| 265 | * @return int |
||
| 266 | */ |
||
| 267 | public function wp_user() { |
||
| 270 | |||
| 271 | |||
| 272 | |||
| 273 | /** |
||
| 274 | * get is_default |
||
| 275 | * @access public |
||
| 276 | * @return bool |
||
| 277 | */ |
||
| 278 | public function is_default() { |
||
| 281 | |||
| 282 | |||
| 283 | |||
| 284 | /** |
||
| 285 | * get deleted |
||
| 286 | * @access public |
||
| 287 | * @return bool |
||
| 288 | */ |
||
| 289 | public function deleted() { |
||
| 292 | |||
| 293 | |||
| 294 | |||
| 295 | /** |
||
| 296 | * @return bool |
||
| 297 | */ |
||
| 298 | public function parent() { |
||
| 301 | |||
| 302 | |||
| 303 | //some helper methods for getting info on the price_type for this price |
||
| 304 | |||
| 305 | /** |
||
| 306 | * return whether the price is a base price or not |
||
| 307 | * @return boolean |
||
| 308 | */ |
||
| 309 | public function is_base_price() { |
||
| 313 | |||
| 314 | |||
| 315 | |||
| 316 | /** |
||
| 317 | * |
||
| 318 | * @return EE_Price_Type |
||
| 319 | */ |
||
| 320 | public function type_obj() { |
||
| 323 | |||
| 324 | |||
| 325 | |||
| 326 | /** |
||
| 327 | * Simply indicates whether this price increases or decreases the total |
||
| 328 | * @return boolean true = discount, otherwise adds to the total |
||
| 329 | */ |
||
| 330 | public function is_discount() { |
||
| 334 | |||
| 335 | |||
| 336 | |||
| 337 | /** |
||
| 338 | * whether the price is a percentage or not |
||
| 339 | * @return boolean |
||
| 340 | */ |
||
| 341 | public function is_percent() { |
||
| 345 | |||
| 346 | |||
| 347 | /** |
||
| 348 | * return pretty price dependant on whether its a dollar or percent. |
||
| 349 | * |
||
| 350 | * @since 4.4.0 |
||
| 351 | * |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | public function pretty_price() { |
||
| 357 | |||
| 358 | |||
| 359 | |||
| 360 | /** |
||
| 361 | * @return mixed |
||
| 362 | */ |
||
| 363 | public function get_price_without_currency_symbol() { |
||
| 366 | |||
| 367 | |||
| 368 | /** |
||
| 369 | * Returns the payment's amount in subunits (if the currency has subunits; otherwise this will actually be |
||
| 370 | * in the currency's main units) |
||
| 371 | * |
||
| 372 | * @return int |
||
| 373 | * @throws EE_Error |
||
| 374 | * @throws InvalidEntityException |
||
| 375 | * @throws DomainException |
||
| 376 | */ |
||
| 377 | public function amountInSubunits() |
||
| 381 | |||
| 382 | |||
| 383 | /** |
||
| 384 | * Sets the payment's amount based on the incoming monetary subunits (eg pennies). If the currency has no subunits, |
||
| 385 | * the amount is actually assumed to be in the currency's main units |
||
| 386 | * |
||
| 387 | * @param int $amount_in_subunits |
||
| 388 | * @return void |
||
| 389 | * @throws EE_Error |
||
| 390 | * @throws InvalidArgumentException |
||
| 391 | * @throws InvalidInterfaceException |
||
| 392 | * @throws InvalidIdentifierException |
||
| 393 | * @throws InvalidDataTypeException |
||
| 394 | * @throws DomainException |
||
| 395 | */ |
||
| 396 | public function setAmountInSubunits($amount_in_subunits) |
||
| 400 | } |
||
| 401 | /* End of file EE_Price.class.php */ |
||
| 403 |