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; |
||
30 | class Notification extends Model |
||
31 | { |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $fillable = [ |
||
37 | 'to_id','to_type','from_id','from_type', |
||
38 | 'category_id','read','url','extra', 'expire_time', |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * Notification constructor. |
||
43 | * |
||
44 | * @param array $attributes |
||
45 | */ |
||
46 | public function __construct(array $attributes = []) |
||
53 | |||
54 | /** |
||
55 | * Custom Collection |
||
56 | * |
||
57 | * @param array $models |
||
58 | * @return NotifynderCollection|\Illuminate\Database\Eloquent\Collection |
||
59 | */ |
||
60 | public function newCollection(array $models = array()) |
||
64 | |||
65 | /** |
||
66 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
67 | */ |
||
68 | public function body() |
||
72 | |||
73 | /** |
||
74 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\MorphTo |
||
75 | */ |
||
76 | View Code Duplication | public function from() |
|
87 | |||
88 | /** |
||
89 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\MorphTo |
||
90 | */ |
||
91 | View Code Duplication | public function to() |
|
102 | |||
103 | /** |
||
104 | * Not read scope |
||
105 | * |
||
106 | * @param $query |
||
107 | * @return mixed |
||
108 | */ |
||
109 | public function scopeWithNotRead($query) |
||
113 | |||
114 | /** |
||
115 | * Only Expired Notification scope |
||
116 | * |
||
117 | * @param $query |
||
118 | * @return mixed |
||
119 | */ |
||
120 | public function scopeOnlyExpired($query) |
||
124 | |||
125 | /** |
||
126 | * Where Polymorphic |
||
127 | * |
||
128 | * @param $query |
||
129 | * @param $id |
||
130 | * @param $type |
||
131 | * @return mixed |
||
132 | */ |
||
133 | public function scopeWherePolymorphic($query, $id, $type) |
||
142 | |||
143 | /** |
||
144 | * Get parsed body attributes |
||
145 | * |
||
146 | * @return mixed |
||
147 | */ |
||
148 | public function getNotifyBodyAttribute() |
||
154 | |||
155 | /** |
||
156 | * @param $value |
||
157 | * @return mixed|string |
||
158 | */ |
||
159 | public function getExtraAttribute($value) |
||
163 | |||
164 | /** |
||
165 | * Filter Scope by category |
||
166 | * |
||
167 | * @param $query |
||
168 | * @param $category |
||
169 | * @return mixed |
||
170 | */ |
||
171 | public function scopeByCategory($query,$category) |
||
182 | |||
183 | /** |
||
184 | * Set configuration object |
||
185 | * |
||
186 | * @param Repository $config |
||
187 | */ |
||
188 | public function setConfig(Repository $config) |
||
192 | |||
193 | /** |
||
194 | * Get custom required fields from the configs |
||
195 | * so that we can automatically bind them to the model |
||
196 | * fillable property |
||
197 | * |
||
198 | * @return mixed |
||
199 | */ |
||
200 | public function getCustomFillableFields() |
||
207 | |||
208 | /** |
||
209 | * @return array |
||
210 | */ |
||
211 | protected function mergeFillable() |
||
217 | } |
||
218 |
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.