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 Bedard\Shop\Models; |
||
| 8 | class Promotion extends Model |
||
| 9 | { |
||
| 10 | use \Bedard\Shop\Traits\StartEndable, |
||
| 11 | \Bedard\Shop\Traits\Timeable, |
||
| 12 | \October\Rain\Database\Traits\Purgeable, |
||
| 13 | \October\Rain\Database\Traits\Validation; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var string The database table used by the model. |
||
| 17 | */ |
||
| 18 | public $table = 'bedard_shop_promotions'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var array Default attributes |
||
| 22 | */ |
||
| 23 | public $attributes = [ |
||
| 24 | 'amount' => 0, |
||
| 25 | 'amount_exact' => 0, |
||
| 26 | 'amount_percentage' => 0, |
||
| 27 | 'is_percentage' => true, |
||
| 28 | ]; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var array Guarded fields |
||
| 32 | */ |
||
| 33 | protected $guarded = ['*']; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array Fillable fields |
||
| 37 | */ |
||
| 38 | protected $fillable = [ |
||
| 39 | 'amount_exact', |
||
| 40 | 'amount_percentage', |
||
| 41 | 'amount', |
||
| 42 | 'is_percentage', |
||
| 43 | 'message', |
||
| 44 | 'minimum_cart_value', |
||
| 45 | 'name', |
||
| 46 | ]; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var array Purgeable vields |
||
| 50 | */ |
||
| 51 | protected $purgeable = [ |
||
| 52 | 'amount_exact', |
||
| 53 | 'amount_percentage', |
||
| 54 | ]; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array Validation rules |
||
| 58 | */ |
||
| 59 | public $rules = [ |
||
| 60 | 'end_at' => 'date', |
||
| 61 | 'name' => 'required', |
||
| 62 | 'start_at' => 'date', |
||
| 63 | 'amount_exact' => 'numeric|min:0', |
||
| 64 | 'amount_percentage' => 'numeric|min:0|max:100', |
||
| 65 | ]; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Before save. |
||
| 69 | * |
||
| 70 | * @return void |
||
| 71 | */ |
||
| 72 | public function beforeSave() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Filter form fields. |
||
| 79 | * |
||
| 80 | * @param object $fields |
||
| 81 | * @return void |
||
| 82 | */ |
||
| 83 | public function filterFields($fields) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Set the promotion amount. |
||
| 91 | * |
||
| 92 | * @return void |
||
| 93 | */ |
||
| 94 | View Code Duplication | public function setAmount() |
|
| 103 | } |
||
| 104 |