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 |
||
34 | class Business extends EloquentModel implements HasPresenter |
||
35 | { |
||
36 | use SoftDeletes, Preferenceable, IsIntoDomain; |
||
37 | |||
38 | /** |
||
39 | * The attributes that are mass assignable. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $fillable = [ |
||
44 | 'name', |
||
45 | 'description', |
||
46 | 'timezone', |
||
47 | 'postal_address', |
||
48 | 'phone', |
||
49 | 'social_facebook', |
||
50 | 'strategy', |
||
51 | 'plan', |
||
52 | 'country_code', |
||
53 | 'locale', |
||
54 | ]; |
||
55 | |||
56 | /** |
||
57 | * The attributes that should be mutated to dates. |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | protected $dates = ['deleted_at']; |
||
62 | |||
63 | /** |
||
64 | * Get the route key for the model. |
||
65 | * |
||
66 | * @return string |
||
67 | */ |
||
68 | public function getRouteKeyName() |
||
72 | |||
73 | /** |
||
74 | * Define model events. |
||
75 | * |
||
76 | * @return void |
||
77 | */ |
||
78 | 98 | public static function boot() |
|
88 | |||
89 | /** |
||
90 | * Make Slug. |
||
91 | * |
||
92 | * @param string $name |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | 96 | protected function makeSlug($name) |
|
100 | |||
101 | /////////////////// |
||
102 | // Relationships // |
||
103 | /////////////////// |
||
104 | |||
105 | /** |
||
106 | * Belongs to a Category. |
||
107 | * |
||
108 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
109 | */ |
||
110 | 2 | public function category() |
|
114 | |||
115 | /** |
||
116 | * Has a Contact addressbook. |
||
117 | * |
||
118 | * @return Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
119 | */ |
||
120 | 10 | public function addressbook() |
|
124 | |||
125 | /** |
||
126 | * Has a Contact addressbook. |
||
127 | * |
||
128 | * @return Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
129 | */ |
||
130 | 12 | public function contacts() |
|
137 | |||
138 | /** |
||
139 | * Provides a catalog of Services. |
||
140 | * |
||
141 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
142 | */ |
||
143 | 27 | public function services() |
|
147 | |||
148 | /** |
||
149 | * Provides Services of Types. |
||
150 | * |
||
151 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
152 | */ |
||
153 | 1 | public function servicetypes() |
|
157 | |||
158 | /** |
||
159 | * Publishes Vacancies. |
||
160 | * |
||
161 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
162 | */ |
||
163 | 42 | public function vacancies() |
|
167 | |||
168 | /** |
||
169 | * Has many human resources. |
||
170 | * |
||
171 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
172 | */ |
||
173 | 2 | public function humanresources() |
|
177 | |||
178 | /** |
||
179 | * Holds booked Appointments. |
||
180 | * |
||
181 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
182 | */ |
||
183 | 17 | public function bookings() |
|
187 | |||
188 | /** |
||
189 | * Is owned by Users. |
||
190 | * |
||
191 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
192 | */ |
||
193 | 33 | public function owners() |
|
197 | |||
198 | /** |
||
199 | * Belongs to a User. |
||
200 | * |
||
201 | * @return User |
||
202 | */ |
||
203 | 1 | public function owner() |
|
207 | |||
208 | /** |
||
209 | * Get the real Users subscriptions count. |
||
210 | * |
||
211 | * @return Illuminate\Database\Query Relationship |
||
212 | */ |
||
213 | public function subscriptionsCount() |
||
220 | |||
221 | /** |
||
222 | * get SubscriptionsCount Attribute. |
||
223 | * |
||
224 | * @return int Count of Contacts with real User held by this Business |
||
225 | */ |
||
226 | View Code Duplication | public function getSubscriptionsCountAttribute() |
|
238 | |||
239 | /////////////// |
||
240 | // Overrides // |
||
241 | /////////////// |
||
242 | |||
243 | // |
||
244 | |||
245 | /////////////// |
||
246 | // Presenter // |
||
247 | /////////////// |
||
248 | |||
249 | /** |
||
250 | * Get presenter. |
||
251 | * |
||
252 | * @return BusinessPresenter Presenter class |
||
253 | */ |
||
254 | 2 | public function getPresenterClass() |
|
258 | |||
259 | /////////////// |
||
260 | // Accessors // |
||
261 | /////////////// |
||
262 | |||
263 | /** |
||
264 | * get route key. |
||
265 | * |
||
266 | * @return string Model slug |
||
267 | */ |
||
268 | public function getRouteKey() |
||
272 | |||
273 | ////////////// |
||
274 | // Mutators // |
||
275 | ////////////// |
||
276 | |||
277 | /** |
||
278 | * Set Slug. |
||
279 | * |
||
280 | * @return string Generated slug |
||
281 | */ |
||
282 | 97 | public function setSlugAttribute() |
|
286 | |||
287 | /** |
||
288 | * Set name of the business. |
||
289 | * |
||
290 | * @param string $name Name of business |
||
291 | */ |
||
292 | 96 | public function setNameAttribute($name) |
|
297 | |||
298 | /** |
||
299 | * Set Phone. |
||
300 | * |
||
301 | * Expected phone number is international format numeric only |
||
302 | * |
||
303 | * @param string $phone Phone number |
||
304 | */ |
||
305 | 97 | public function setPhoneAttribute($phone) |
|
309 | |||
310 | /** |
||
311 | * Set Postal Address. |
||
312 | * |
||
313 | * @param string $postalAddress Postal address |
||
314 | */ |
||
315 | 97 | public function setPostalAddressAttribute($postalAddress) |
|
319 | |||
320 | /** |
||
321 | * Set Social Facebook. |
||
322 | */ |
||
323 | 96 | public function setSocialFacebookAttribute($facebookPageUrl) |
|
327 | } |
||
328 |
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.