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:
Complex classes like Appointment often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Appointment, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Appointment extends EloquentModel implements HasPresenter |
||
18 | { |
||
19 | /** |
||
20 | * The attributes that are mass assignable. |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $fillable = ['issuer_id', 'contact_id', 'business_id', |
||
25 | 'service_id', 'start_at', 'finish_at', 'duration', 'comments', ]; |
||
26 | |||
27 | /** |
||
28 | * The attributes that aren't mass assignable. |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $guarded = ['id', 'hash', 'status', 'vacancy_id']; |
||
33 | |||
34 | /** |
||
35 | * The attributes that should be mutated to dates. |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $dates = ['start_at', 'finish_at']; |
||
40 | |||
41 | /** |
||
42 | * Appointment Hard Status Constants. |
||
43 | */ |
||
44 | const STATUS_RESERVED = 'R'; |
||
45 | const STATUS_CONFIRMED = 'C'; |
||
46 | const STATUS_ANNULATED = 'A'; |
||
47 | const STATUS_SERVED = 'S'; |
||
48 | |||
49 | /////////////// |
||
50 | // PRESENTER // |
||
51 | /////////////// |
||
52 | |||
53 | /** |
||
54 | * Get Presenter Class. |
||
55 | * |
||
56 | * @return App\Presenters\AppointmentPresenter |
||
57 | */ |
||
58 | 1 | public function getPresenterClass() |
|
59 | { |
||
60 | 1 | return AppointmentPresenter::class; |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * Generate hash and save the model to the database. |
||
65 | * |
||
66 | * @param array $options |
||
67 | * |
||
68 | * @return bool |
||
69 | */ |
||
70 | 24 | public function save(array $options = []) |
|
76 | |||
77 | /////////////////// |
||
78 | // Relationships // |
||
79 | /////////////////// |
||
80 | |||
81 | /** |
||
82 | * Get the issuer (the User that generated the Appointment). |
||
83 | * |
||
84 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
85 | */ |
||
86 | 4 | public function issuer() |
|
90 | |||
91 | /** |
||
92 | * Get the target Contact (for whom is reserved the Appointment). |
||
93 | * |
||
94 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
95 | */ |
||
96 | 4 | public function contact() |
|
100 | |||
101 | /** |
||
102 | * Get the holding Business (that has taken the reservation). |
||
103 | * |
||
104 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
105 | */ |
||
106 | 8 | public function business() |
|
110 | |||
111 | /** |
||
112 | * Get the reserved Service. |
||
113 | * |
||
114 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
115 | */ |
||
116 | 8 | public function service() |
|
120 | |||
121 | /** |
||
122 | * Get the Vacancy (that justifies the availability of resources for the |
||
123 | * Appointment generation). |
||
124 | * |
||
125 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
126 | */ |
||
127 | 5 | public function vacancy() |
|
131 | |||
132 | /////////// |
||
133 | // Other // |
||
134 | /////////// |
||
135 | |||
136 | /** |
||
137 | * Get the User through Contact. |
||
138 | * |
||
139 | * @return User |
||
140 | */ |
||
141 | 2 | public function user() |
|
145 | |||
146 | /** |
||
147 | * Determine if the new Appointment will hash-crash with another existing |
||
148 | * Appointment. |
||
149 | * |
||
150 | * @return bool |
||
151 | */ |
||
152 | 5 | public function duplicates() |
|
156 | |||
157 | /////////////// |
||
158 | // Accessors // |
||
159 | /////////////// |
||
160 | |||
161 | /** |
||
162 | * Get Hash. |
||
163 | * |
||
164 | * @return string |
||
165 | */ |
||
166 | 5 | public function getHashAttribute() |
|
172 | |||
173 | /** |
||
174 | * Get Finish At: |
||
175 | * Calculates the start_at time plus duration in minutes. |
||
176 | * |
||
177 | * @return Carbon |
||
178 | */ |
||
179 | 1 | public function getFinishAtAttribute() |
|
191 | |||
192 | /** |
||
193 | * Get the date of the Appointment. |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
197 | 3 | public function getDateAttribute() |
|
203 | |||
204 | ////////////// |
||
205 | // Mutators // |
||
206 | ////////////// |
||
207 | |||
208 | /** |
||
209 | * Generate Appointment hash. |
||
210 | * |
||
211 | * @return string |
||
212 | */ |
||
213 | 24 | public function doHash() |
|
222 | |||
223 | /** |
||
224 | * Set start at. |
||
225 | * |
||
226 | * @param Carbon $datetime |
||
227 | */ |
||
228 | 24 | public function setStartAtAttribute(Carbon $datetime) |
|
232 | |||
233 | /** |
||
234 | * Set finish_at attribute. |
||
235 | * |
||
236 | * @param Carbon $datetime |
||
237 | */ |
||
238 | 8 | public function setFinishAtAttribute(Carbon $datetime) |
|
242 | |||
243 | /** |
||
244 | * Set Comments. |
||
245 | * |
||
246 | * @param string $comments |
||
247 | */ |
||
248 | 24 | public function setCommentsAttribute($comments) |
|
252 | |||
253 | ///////////////// |
||
254 | // HARD STATUS // |
||
255 | ///////////////// |
||
256 | |||
257 | /** |
||
258 | * Determine if is Reserved. |
||
259 | * |
||
260 | * @return bool |
||
261 | */ |
||
262 | 1 | public function isReserved() |
|
266 | |||
267 | /////////////////////////// |
||
268 | // Calculated attributes // |
||
269 | /////////////////////////// |
||
270 | |||
271 | /** |
||
272 | * Appointment Status Workflow. |
||
273 | * |
||
274 | * Hard Status: Those concrete values stored in DB |
||
275 | * Soft Status: Those values calculated from stored values in DB |
||
276 | * |
||
277 | * Suggested transitions (Binding is not mandatory) |
||
278 | * Reserved -> Confirmed -> Served |
||
279 | * Reserved -> Served |
||
280 | * Reserved -> Annulated |
||
281 | * Reserved -> Confirmed -> Annulated |
||
282 | * |
||
283 | * Soft Status |
||
284 | * (Active) [ Reserved | Confirmed ] |
||
285 | * (InActive) [ Annulated | Served ] |
||
286 | */ |
||
287 | |||
288 | /** |
||
289 | * Determine if is Active. |
||
290 | * |
||
291 | * @return bool |
||
292 | */ |
||
293 | 3 | public function isActive() |
|
294 | { |
||
295 | return |
||
296 | 3 | $this->status == Self::STATUS_CONFIRMED || |
|
297 | 3 | $this->status == Self::STATUS_RESERVED; |
|
298 | } |
||
299 | |||
300 | /** |
||
301 | * Determine if is Pending. |
||
302 | * |
||
303 | * @return bool |
||
304 | */ |
||
305 | 3 | public function isPending() |
|
309 | |||
310 | /** |
||
311 | * Determine if is Future. |
||
312 | * |
||
313 | * @return bool |
||
314 | */ |
||
315 | 3 | public function isFuture() |
|
316 | { |
||
317 | 3 | return !$this->isDue(); |
|
318 | } |
||
319 | |||
320 | /** |
||
321 | * Determine if is due. |
||
322 | * |
||
323 | * @return bool |
||
324 | */ |
||
325 | 3 | public function isDue() |
|
326 | { |
||
327 | 3 | return $this->start_at->isPast(); |
|
328 | } |
||
329 | |||
330 | //////////// |
||
331 | // Scopes // |
||
332 | //////////// |
||
333 | |||
334 | ///////////////////////// |
||
335 | // Soft Status Scoping // |
||
336 | ///////////////////////// |
||
337 | |||
338 | /** |
||
339 | * Scope to not Served Appointments. |
||
340 | * |
||
341 | * @param Illuminate\Database\Query $query |
||
342 | * |
||
343 | * @return Illuminate\Database\Query |
||
344 | */ |
||
345 | public function scopeUnServed($query) |
||
349 | |||
350 | /** |
||
351 | * Scope to Active Appointments. |
||
352 | * |
||
353 | * @param Illuminate\Database\Query $query |
||
354 | * |
||
355 | * @return Illuminate\Database\Query |
||
356 | */ |
||
357 | 12 | public function scopeActive($query) |
|
361 | |||
362 | /** |
||
363 | * Between Dates. |
||
364 | * |
||
365 | * @param Illuminate\Database\Query $query |
||
366 | * @param Carbon $startAt |
||
367 | * @param Carbon $finishAt |
||
368 | * |
||
369 | * @return Illuminate\Database\Query |
||
370 | */ |
||
371 | 8 | public function scopeAffectingInterval($query, Carbon $startAt, Carbon $finishAt) |
|
395 | |||
396 | /** |
||
397 | * Determine if the Serve action can be performed. |
||
398 | * |
||
399 | * @return bool |
||
400 | */ |
||
401 | public function isServeable() |
||
405 | |||
406 | /** |
||
407 | * Determine if the Confirm action can be performed. |
||
408 | * |
||
409 | * @return bool |
||
410 | */ |
||
411 | public function isConfirmable() |
||
415 | |||
416 | /** |
||
417 | * Determine if the Annulate action can be performed. |
||
418 | * |
||
419 | * @return bool |
||
420 | */ |
||
421 | public function isAnnulable() |
||
425 | |||
426 | ///////////////////////// |
||
427 | // Hard Status Actions // |
||
428 | ///////////////////////// |
||
429 | |||
430 | /** |
||
431 | * Check and perform Confirm action. |
||
432 | * |
||
433 | * @return $this |
||
434 | */ |
||
435 | 5 | public function doReserve() |
|
443 | |||
444 | /** |
||
445 | * Check and perform Confirm action. |
||
446 | * |
||
447 | * @return $this |
||
448 | */ |
||
449 | 1 | public function doConfirm() |
|
459 | |||
460 | /** |
||
461 | * Check and perform Annulate action. |
||
462 | * |
||
463 | * @return $this |
||
464 | */ |
||
465 | 1 | public function doAnnulate() |
|
475 | |||
476 | /** |
||
477 | * Check and perform Serve action. |
||
478 | * |
||
479 | * @return $this |
||
480 | */ |
||
481 | 2 | public function doServe() |
|
491 | } |
||
492 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.