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 Fenos\Notifynder\Models; |
||
| 28 | class Notification extends Model |
||
| 29 | { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $fillable = [ |
||
| 35 | 'to_id','to_type','from_id','from_type', |
||
| 36 | 'category_id','read','url','extra', 'expire_time', |
||
| 37 | ]; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Custom Collection |
||
| 41 | * |
||
| 42 | * @param array $models |
||
| 43 | * @return NotifynderCollection|\Illuminate\Database\Eloquent\Collection |
||
| 44 | */ |
||
| 45 | public function newCollection(array $models = array()) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 52 | */ |
||
| 53 | public function body() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\MorphTo |
||
| 60 | */ |
||
| 61 | View Code Duplication | public function from() |
|
| 72 | |||
| 73 | /** |
||
| 74 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\MorphTo |
||
| 75 | */ |
||
| 76 | View Code Duplication | public function to() |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Not read scope |
||
| 90 | * |
||
| 91 | * @param $query |
||
| 92 | * @return mixed |
||
| 93 | */ |
||
| 94 | public function scopeWithNotRead($query) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Only Expired Notification scope |
||
| 101 | * |
||
| 102 | * @param $query |
||
| 103 | * @return mixed |
||
| 104 | */ |
||
| 105 | public function scopeOnlyExpired($query) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Where Polymorphic |
||
| 112 | * |
||
| 113 | * @param $query |
||
| 114 | * @param $id |
||
| 115 | * @param $type |
||
| 116 | * @return mixed |
||
| 117 | */ |
||
| 118 | public function scopeWherePolymorphic($query, $id, $type) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param $value |
||
| 130 | * @return mixed|string |
||
| 131 | */ |
||
| 132 | public function getExtraAttribute($value) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Filter Scope by category |
||
| 139 | * |
||
| 140 | * @param $query |
||
| 141 | * @param $category |
||
| 142 | * @return mixed |
||
| 143 | */ |
||
| 144 | public function scopeByCategory($query,$category) |
||
| 155 | } |
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.