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 | 29 | 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 | 10 | 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 | 6 | 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 | 29 | public function doHash() |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Set start at. |
||
| 270 | * |
||
| 271 | * @param Carbon $datetime |
||
| 272 | */ |
||
| 273 | 29 | public function setStartAtAttribute(Carbon $datetime) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Set finish_at attribute. |
||
| 280 | * |
||
| 281 | * @param Carbon $datetime |
||
| 282 | */ |
||
| 283 | 10 | public function setFinishAtAttribute(Carbon $datetime) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Set Comments. |
||
| 290 | * |
||
| 291 | * @param string $comments |
||
| 292 | */ |
||
| 293 | 29 | 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 | * Scope to Business. |
||
| 381 | * |
||
| 382 | * @param Illuminate\Database\Query $query |
||
| 383 | * |
||
| 384 | * @return Illuminate\Database\Query |
||
| 385 | */ |
||
| 386 | 1 | public function scopeOfBusiness($query, $businessId) |
|
| 390 | |||
| 391 | ///////////////////////// |
||
| 392 | // Hard Status Scoping // |
||
| 393 | ///////////////////////// |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Scope to Contacts Collection. |
||
| 397 | * |
||
| 398 | * @param Illuminate\Database\Query $query |
||
| 399 | * |
||
| 400 | * @return Illuminate\Database\Query |
||
| 401 | */ |
||
| 402 | public function scopeForContacts($query, $contacts) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Scope to Unarchived Appointments. |
||
| 409 | * |
||
| 410 | * @param Illuminate\Database\Query $query |
||
| 411 | * |
||
| 412 | * @return Illuminate\Database\Query |
||
| 413 | */ |
||
| 414 | public function scopeUnarchived($query) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Scope to Served Appointments. |
||
| 428 | * |
||
| 429 | * @param Illuminate\Database\Query $query |
||
| 430 | * |
||
| 431 | * @return Illuminate\Database\Query |
||
| 432 | */ |
||
| 433 | public function scopeServed($query) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Scope to Annulated Appointments. |
||
| 440 | * |
||
| 441 | * @param Illuminate\Database\Query $query |
||
| 442 | * |
||
| 443 | * @return Illuminate\Database\Query |
||
| 444 | */ |
||
| 445 | public function scopeAnnulated($query) |
||
| 449 | |||
| 450 | ///////////////////////// |
||
| 451 | // Soft Status Scoping // |
||
| 452 | ///////////////////////// |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Scope to not Served Appointments. |
||
| 456 | * |
||
| 457 | * @param Illuminate\Database\Query $query |
||
| 458 | * |
||
| 459 | * @return Illuminate\Database\Query |
||
| 460 | */ |
||
| 461 | public function scopeUnServed($query) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Scope to Active Appointments. |
||
| 468 | * |
||
| 469 | * @param Illuminate\Database\Query $query |
||
| 470 | * |
||
| 471 | * @return Illuminate\Database\Query |
||
| 472 | */ |
||
| 473 | 18 | public function scopeActive($query) |
|
| 477 | |||
| 478 | /** |
||
| 479 | * Scope of date. |
||
| 480 | * |
||
| 481 | * @param Illuminate\Database\Query $query |
||
| 482 | * @param Carbon $date |
||
| 483 | * |
||
| 484 | * @return Illuminate\Database\Query |
||
| 485 | */ |
||
| 486 | public function scopeOfDate($query, Carbon $date) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Between Dates. |
||
| 493 | * |
||
| 494 | * @param Illuminate\Database\Query $query |
||
| 495 | * @param Carbon $startAt |
||
| 496 | * @param Carbon $finishAt |
||
| 497 | * |
||
| 498 | * @return Illuminate\Database\Query |
||
| 499 | */ |
||
| 500 | 12 | public function scopeAffectingInterval($query, Carbon $startAt, Carbon $finishAt) |
|
| 524 | |||
| 525 | ////////////////////////// |
||
| 526 | // Soft Status Checkers // |
||
| 527 | ////////////////////////// |
||
| 528 | |||
| 529 | /** |
||
| 530 | * User is target contact of the appointment. |
||
| 531 | * |
||
| 532 | * @param int $userId |
||
| 533 | * |
||
| 534 | * @return bool |
||
| 535 | */ |
||
| 536 | public function isTarget($userId) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * User is issuer of the appointment. |
||
| 543 | * |
||
| 544 | * @param int $userId |
||
| 545 | * |
||
| 546 | * @return bool |
||
| 547 | */ |
||
| 548 | public function isIssuer($userId) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * User is owner of business. |
||
| 555 | * |
||
| 556 | * @param int $userId |
||
| 557 | * |
||
| 558 | * @return bool |
||
| 559 | */ |
||
| 560 | public function isOwner($userId) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * can be annulated by user. |
||
| 567 | * |
||
| 568 | * @param int $userId |
||
| 569 | * |
||
| 570 | * @return bool |
||
| 571 | */ |
||
| 572 | public function canAnnulate($userId) |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Determine if it is still possible to annulate according business policy. |
||
| 581 | * |
||
| 582 | * @return bool |
||
| 583 | */ |
||
| 584 | public function isOnTimeToAnnulate() |
||
| 592 | |||
| 593 | /** |
||
| 594 | * can Serve. |
||
| 595 | * |
||
| 596 | * @param int $userId |
||
| 597 | * |
||
| 598 | * @return bool |
||
| 599 | */ |
||
| 600 | public function canServe($userId) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * can confirm. |
||
| 607 | * |
||
| 608 | * @param int $userId |
||
| 609 | * |
||
| 610 | * @return bool |
||
| 611 | */ |
||
| 612 | public function canConfirm($userId) |
||
| 616 | |||
| 617 | /** |
||
| 618 | * is Serveable by user. |
||
| 619 | * |
||
| 620 | * @param int $userId |
||
| 621 | * |
||
| 622 | * @return bool |
||
| 623 | */ |
||
| 624 | public function isServeableBy($userId) |
||
| 628 | |||
| 629 | /** |
||
| 630 | * is Confirmable By user. |
||
| 631 | * |
||
| 632 | * @param int $userId |
||
| 633 | * |
||
| 634 | * @return bool |
||
| 635 | */ |
||
| 636 | public function isConfirmableBy($userId) |
||
| 643 | |||
| 644 | /** |
||
| 645 | * is Annulable By user. |
||
| 646 | * |
||
| 647 | * @param int $userId |
||
| 648 | * |
||
| 649 | * @return bool |
||
| 650 | */ |
||
| 651 | public function isAnnulableBy($userId) |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Determine if the queried userId may confirm the appointment or not. |
||
| 658 | * |
||
| 659 | * @param int $userId |
||
| 660 | * |
||
| 661 | * @return bool |
||
| 662 | */ |
||
| 663 | public function shouldConfirmBy($userId) |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Determine if the target Contact's User is the same of the Appointment |
||
| 671 | * issuer User. |
||
| 672 | * |
||
| 673 | * @return bool |
||
| 674 | */ |
||
| 675 | public function isSelfIssued() |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Determine if the Serve action can be performed. |
||
| 692 | * |
||
| 693 | * @return bool |
||
| 694 | */ |
||
| 695 | 1 | public function isServeable() |
|
| 699 | |||
| 700 | /** |
||
| 701 | * Determine if the Confirm action can be performed. |
||
| 702 | * |
||
| 703 | * @return bool |
||
| 704 | */ |
||
| 705 | 1 | public function isConfirmable() |
|
| 709 | |||
| 710 | /** |
||
| 711 | * Determine if the Annulate action can be performed. |
||
| 712 | * |
||
| 713 | * @return bool |
||
| 714 | */ |
||
| 715 | 1 | public function isAnnulable() |
|
| 719 | |||
| 720 | ///////////////////////// |
||
| 721 | // Hard Status Actions // |
||
| 722 | ///////////////////////// |
||
| 723 | |||
| 724 | /** |
||
| 725 | * Check and perform Confirm action. |
||
| 726 | * |
||
| 727 | * @return $this |
||
| 728 | */ |
||
| 729 | 5 | public function doReserve() |
|
| 737 | |||
| 738 | /** |
||
| 739 | * Check and perform Confirm action. |
||
| 740 | * |
||
| 741 | * @return $this |
||
| 742 | */ |
||
| 743 | 2 | public function doConfirm() |
|
| 753 | |||
| 754 | /** |
||
| 755 | * Check and perform Annulate action. |
||
| 756 | * |
||
| 757 | * @return $this |
||
| 758 | */ |
||
| 759 | 2 | public function doAnnulate() |
|
| 769 | |||
| 770 | /** |
||
| 771 | * Check and perform Serve action. |
||
| 772 | * |
||
| 773 | * @return $this |
||
| 774 | */ |
||
| 775 | 3 | public function doServe() |
|
| 785 | } |
||
| 786 |
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@propertyannotation 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.