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 | 27 | 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 | 8 | 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 annulation deadline (target date). |
||
194 | * |
||
195 | * @return Carbon\Carbon |
||
196 | */ |
||
197 | public function getAnnulationDeadlineAttribute() |
||
205 | |||
206 | /** |
||
207 | * Get the human readable status name. |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | public function getStatusLabelAttribute() |
||
224 | |||
225 | /** |
||
226 | * Get the date of the Appointment. |
||
227 | * |
||
228 | * @return string |
||
229 | */ |
||
230 | 3 | public function getDateAttribute() |
|
236 | |||
237 | /** |
||
238 | * Get user-friendly unique identification code. |
||
239 | * |
||
240 | * @return string |
||
241 | */ |
||
242 | public function getCodeAttribute() |
||
248 | |||
249 | ////////////// |
||
250 | // Mutators // |
||
251 | ////////////// |
||
252 | |||
253 | /** |
||
254 | * Generate Appointment hash. |
||
255 | * |
||
256 | * @return string |
||
257 | */ |
||
258 | 27 | public function doHash() |
|
267 | |||
268 | /** |
||
269 | * Set start at. |
||
270 | * |
||
271 | * @param Carbon $datetime |
||
272 | */ |
||
273 | 27 | public function setStartAtAttribute(Carbon $datetime) |
|
277 | |||
278 | /** |
||
279 | * Set finish_at attribute. |
||
280 | * |
||
281 | * @param Carbon $datetime |
||
282 | */ |
||
283 | 8 | public function setFinishAtAttribute(Carbon $datetime) |
|
287 | |||
288 | /** |
||
289 | * Set Comments. |
||
290 | * |
||
291 | * @param string $comments |
||
292 | */ |
||
293 | 27 | public function setCommentsAttribute($comments) |
|
297 | |||
298 | ///////////////// |
||
299 | // HARD STATUS // |
||
300 | ///////////////// |
||
301 | |||
302 | /** |
||
303 | * Determine if is Reserved. |
||
304 | * |
||
305 | * @return bool |
||
306 | */ |
||
307 | 1 | public function isReserved() |
|
311 | |||
312 | /////////////////////////// |
||
313 | // Calculated attributes // |
||
314 | /////////////////////////// |
||
315 | |||
316 | /** |
||
317 | * Appointment Status Workflow. |
||
318 | * |
||
319 | * Hard Status: Those concrete values stored in DB |
||
320 | * Soft Status: Those values calculated from stored values in DB |
||
321 | * |
||
322 | * Suggested transitions (Binding is not mandatory) |
||
323 | * Reserved -> Confirmed -> Served |
||
324 | * Reserved -> Served |
||
325 | * Reserved -> Annulated |
||
326 | * Reserved -> Confirmed -> Annulated |
||
327 | * |
||
328 | * Soft Status |
||
329 | * (Active) [ Reserved | Confirmed ] |
||
330 | * (InActive) [ Annulated | Served ] |
||
331 | */ |
||
332 | |||
333 | /** |
||
334 | * Determine if is Active. |
||
335 | * |
||
336 | * @return bool |
||
337 | */ |
||
338 | 5 | public function isActive() |
|
344 | |||
345 | /** |
||
346 | * Determine if is Pending. |
||
347 | * |
||
348 | * @return bool |
||
349 | */ |
||
350 | 3 | public function isPending() |
|
354 | |||
355 | /** |
||
356 | * Determine if is Future. |
||
357 | * |
||
358 | * @return bool |
||
359 | */ |
||
360 | 4 | public function isFuture() |
|
364 | |||
365 | /** |
||
366 | * Determine if is due. |
||
367 | * |
||
368 | * @return bool |
||
369 | */ |
||
370 | 5 | public function isDue() |
|
374 | |||
375 | //////////// |
||
376 | // Scopes // |
||
377 | //////////// |
||
378 | |||
379 | ///////////////////////// |
||
380 | // Hard Status Scoping // |
||
381 | ///////////////////////// |
||
382 | |||
383 | /** |
||
384 | * Scope to Contacts Collection. |
||
385 | * |
||
386 | * @param Illuminate\Database\Query $query |
||
387 | * |
||
388 | * @return Illuminate\Database\Query |
||
389 | */ |
||
390 | public function scopeForContacts($query, $contacts) |
||
394 | |||
395 | /** |
||
396 | * Scope to Unarchived Appointments. |
||
397 | * |
||
398 | * @param Illuminate\Database\Query $query |
||
399 | * |
||
400 | * @return Illuminate\Database\Query |
||
401 | */ |
||
402 | public function scopeUnarchived($query) |
||
413 | |||
414 | /** |
||
415 | * Scope to Served Appointments. |
||
416 | * |
||
417 | * @param Illuminate\Database\Query $query |
||
418 | * |
||
419 | * @return Illuminate\Database\Query |
||
420 | */ |
||
421 | public function scopeServed($query) |
||
425 | |||
426 | /** |
||
427 | * Scope to Annulated Appointments. |
||
428 | * |
||
429 | * @param Illuminate\Database\Query $query |
||
430 | * |
||
431 | * @return Illuminate\Database\Query |
||
432 | */ |
||
433 | public function scopeAnnulated($query) |
||
437 | |||
438 | ///////////////////////// |
||
439 | // Soft Status Scoping // |
||
440 | ///////////////////////// |
||
441 | |||
442 | /** |
||
443 | * Scope to not Served Appointments. |
||
444 | * |
||
445 | * @param Illuminate\Database\Query $query |
||
446 | * |
||
447 | * @return Illuminate\Database\Query |
||
448 | */ |
||
449 | public function scopeUnServed($query) |
||
453 | |||
454 | /** |
||
455 | * Scope to Active Appointments. |
||
456 | * |
||
457 | * @param Illuminate\Database\Query $query |
||
458 | * |
||
459 | * @return Illuminate\Database\Query |
||
460 | */ |
||
461 | 16 | public function scopeActive($query) |
|
465 | |||
466 | /** |
||
467 | * Scope of date. |
||
468 | * |
||
469 | * @param Illuminate\Database\Query $query |
||
470 | * @param Carbon $date |
||
471 | * |
||
472 | * @return Illuminate\Database\Query |
||
473 | */ |
||
474 | public function scopeOfDate($query, Carbon $date) |
||
478 | |||
479 | /** |
||
480 | * Between Dates. |
||
481 | * |
||
482 | * @param Illuminate\Database\Query $query |
||
483 | * @param Carbon $startAt |
||
484 | * @param Carbon $finishAt |
||
485 | * |
||
486 | * @return Illuminate\Database\Query |
||
487 | */ |
||
488 | 12 | public function scopeAffectingInterval($query, Carbon $startAt, Carbon $finishAt) |
|
512 | |||
513 | ////////////////////////// |
||
514 | // Soft Status Checkers // |
||
515 | ////////////////////////// |
||
516 | |||
517 | /** |
||
518 | * User is target contact of the appointment. |
||
519 | * |
||
520 | * @param int $userId |
||
521 | * |
||
522 | * @return bool |
||
523 | */ |
||
524 | public function isTarget($userId) |
||
528 | |||
529 | /** |
||
530 | * User is issuer of the appointment. |
||
531 | * |
||
532 | * @param int $userId |
||
533 | * |
||
534 | * @return bool |
||
535 | */ |
||
536 | public function isIssuer($userId) |
||
540 | |||
541 | /** |
||
542 | * User is owner of business. |
||
543 | * |
||
544 | * @param int $userId |
||
545 | * |
||
546 | * @return bool |
||
547 | */ |
||
548 | public function isOwner($userId) |
||
552 | |||
553 | /** |
||
554 | * can be annulated by user. |
||
555 | * |
||
556 | * @param int $userId |
||
557 | * |
||
558 | * @return bool |
||
559 | */ |
||
560 | public function canAnnulate($userId) |
||
566 | |||
567 | /** |
||
568 | * Determine if it is still possible to annulate according business policy. |
||
569 | * |
||
570 | * @return bool |
||
571 | */ |
||
572 | public function isOnTimeToAnnulate() |
||
580 | |||
581 | /** |
||
582 | * can Serve. |
||
583 | * |
||
584 | * @param int $userId |
||
585 | * |
||
586 | * @return bool |
||
587 | */ |
||
588 | public function canServe($userId) |
||
592 | |||
593 | /** |
||
594 | * can confirm. |
||
595 | * |
||
596 | * @param int $userId |
||
597 | * |
||
598 | * @return bool |
||
599 | */ |
||
600 | public function canConfirm($userId) |
||
604 | |||
605 | /** |
||
606 | * is Serveable by user. |
||
607 | * |
||
608 | * @param int $userId |
||
609 | * |
||
610 | * @return bool |
||
611 | */ |
||
612 | public function isServeableBy($userId) |
||
616 | |||
617 | /** |
||
618 | * is Confirmable By user. |
||
619 | * |
||
620 | * @param int $userId |
||
621 | * |
||
622 | * @return bool |
||
623 | */ |
||
624 | public function isConfirmableBy($userId) |
||
631 | |||
632 | /** |
||
633 | * is Annulable By user. |
||
634 | * |
||
635 | * @param int $userId |
||
636 | * |
||
637 | * @return bool |
||
638 | */ |
||
639 | public function isAnnulableBy($userId) |
||
643 | |||
644 | /** |
||
645 | * Determine if the queried userId may confirm the appointment or not. |
||
646 | * |
||
647 | * @param int $userId |
||
648 | * |
||
649 | * @return bool |
||
650 | */ |
||
651 | public function shouldConfirmBy($userId) |
||
656 | |||
657 | /** |
||
658 | * Determine if the target Contact's User is the same of the Appointment |
||
659 | * issuer User. |
||
660 | * |
||
661 | * @return bool |
||
662 | */ |
||
663 | public function isSelfIssued() |
||
677 | |||
678 | /** |
||
679 | * Determine if the Serve action can be performed. |
||
680 | * |
||
681 | * @return bool |
||
682 | */ |
||
683 | 1 | public function isServeable() |
|
684 | { |
||
685 | 1 | return $this->isActive() && $this->isDue(); |
|
686 | } |
||
687 | |||
688 | /** |
||
689 | * Determine if the Confirm action can be performed. |
||
690 | * |
||
691 | * @return bool |
||
692 | */ |
||
693 | 1 | public function isConfirmable() |
|
694 | { |
||
695 | 1 | return $this->status == self::STATUS_RESERVED && $this->isFuture(); |
|
696 | } |
||
697 | |||
698 | /** |
||
699 | * Determine if the Annulate action can be performed. |
||
700 | * |
||
701 | * @return bool |
||
702 | */ |
||
703 | 1 | public function isAnnulable() |
|
704 | { |
||
705 | 1 | return $this->isActive(); |
|
706 | } |
||
707 | |||
708 | ///////////////////////// |
||
709 | // Hard Status Actions // |
||
710 | ///////////////////////// |
||
711 | |||
712 | /** |
||
713 | * Check and perform Confirm action. |
||
714 | * |
||
715 | * @return $this |
||
716 | */ |
||
717 | 5 | public function doReserve() |
|
725 | |||
726 | /** |
||
727 | * Check and perform Confirm action. |
||
728 | * |
||
729 | * @return $this |
||
730 | */ |
||
731 | 2 | public function doConfirm() |
|
741 | |||
742 | /** |
||
743 | * Check and perform Annulate action. |
||
744 | * |
||
745 | * @return $this |
||
746 | */ |
||
747 | 2 | public function doAnnulate() |
|
757 | |||
758 | /** |
||
759 | * Check and perform Serve action. |
||
760 | * |
||
761 | * @return $this |
||
762 | */ |
||
763 | 3 | public function doServe() |
|
773 | } |
||
774 |
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.