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 | public function getPresenterClass() |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Generate hash and save the model to the database. |
||
| 65 | * |
||
| 66 | * @param array $options |
||
| 67 | * |
||
| 68 | * @return bool |
||
| 69 | */ |
||
| 70 | 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\belongsTo |
||
| 85 | */ |
||
| 86 | public function issuer() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Get the target Contact (for whom is reserved the Appointment). |
||
| 93 | * |
||
| 94 | * @return Illuminate\Database\Eloquent\belongsTo |
||
| 95 | */ |
||
| 96 | public function contact() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get the holding Business (that has taken the reservation). |
||
| 103 | * |
||
| 104 | * @return Illuminate\Database\Eloquent\belongsTo |
||
| 105 | */ |
||
| 106 | public function business() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Get the reserved Service. |
||
| 113 | * |
||
| 114 | * @return Illuminate\Database\Eloquent\belongsTo |
||
| 115 | */ |
||
| 116 | 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\belongsTo |
||
| 126 | */ |
||
| 127 | public function vacancy() |
||
| 131 | |||
| 132 | /////////// |
||
| 133 | // Other // |
||
| 134 | /////////// |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Get the User through Contact. |
||
| 138 | * |
||
| 139 | * @return User |
||
| 140 | */ |
||
| 141 | 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 | public function duplicates() |
||
| 156 | |||
| 157 | /////////////// |
||
| 158 | // Accessors // |
||
| 159 | /////////////// |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Get Hash. |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | 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 | public function getFinishAtAttribute() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Get TimeZone (from the Business). |
||
| 194 | * |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | public function getTZAttribute() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get the human readable status name. |
||
| 204 | * |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | public function getStatusLabelAttribute() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Get the date of the Appointment. |
||
| 223 | * |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | public function getDateAttribute() |
||
| 232 | |||
| 233 | ////////////// |
||
| 234 | // Mutators // |
||
| 235 | ////////////// |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Generate Appointment hash. |
||
| 239 | * |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | public function doHash() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Set start at. |
||
| 254 | * |
||
| 255 | * @param Carbon $datetime |
||
| 256 | */ |
||
| 257 | public function setStartAtAttribute(Carbon $datetime) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Set finish_at attribute. |
||
| 264 | * |
||
| 265 | * @param Carbon $datetime |
||
| 266 | */ |
||
| 267 | public function setFinishAtAttribute(Carbon $datetime) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Set Comments. |
||
| 274 | * |
||
| 275 | * @param string $comments |
||
| 276 | */ |
||
| 277 | public function setCommentsAttribute($comments) |
||
| 281 | |||
| 282 | ///////////////// |
||
| 283 | // HARD STATUS // |
||
| 284 | ///////////////// |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Determine if is Reserved. |
||
| 288 | * |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | public function isReserved() |
||
| 295 | |||
| 296 | /////////////////////////// |
||
| 297 | // Calculated attributes // |
||
| 298 | /////////////////////////// |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Appointment Status Workflow. |
||
| 302 | * |
||
| 303 | * Hard Status: Those concrete values stored in DB |
||
| 304 | * Soft Status: Those values calculated from stored values in DB |
||
| 305 | * |
||
| 306 | * Suggested transitions (Binding is not mandatory) |
||
| 307 | * Reserved -> Confirmed -> Served |
||
| 308 | * Reserved -> Served |
||
| 309 | * Reserved -> Annulated |
||
| 310 | * Reserved -> Confirmed -> Annulated |
||
| 311 | * |
||
| 312 | * Soft Status |
||
| 313 | * (Active) [ Reserved | Confirmed ] |
||
| 314 | * (InActive) [ Annulated | Served ] |
||
| 315 | */ |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Determine if is Active. |
||
| 319 | * |
||
| 320 | * @return bool |
||
| 321 | */ |
||
| 322 | public function isActive() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Determine if is Pending. |
||
| 331 | * |
||
| 332 | * @return bool |
||
| 333 | */ |
||
| 334 | public function isPending() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Determine if is Future. |
||
| 341 | * |
||
| 342 | * @return bool |
||
| 343 | */ |
||
| 344 | public function isFuture() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Determine if is due. |
||
| 351 | * |
||
| 352 | * @return bool |
||
| 353 | */ |
||
| 354 | public function isDue() |
||
| 358 | |||
| 359 | //////////// |
||
| 360 | // Scopes // |
||
| 361 | //////////// |
||
| 362 | |||
| 363 | ///////////////////////// |
||
| 364 | // Hard Status Scoping // |
||
| 365 | ///////////////////////// |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Scope to Contacts Collection. |
||
| 369 | * |
||
| 370 | * @param Illuminate\Database\Query $query |
||
| 371 | * |
||
| 372 | * @return Illuminate\Database\Query |
||
| 373 | */ |
||
| 374 | public function scopeForContacts($query, $contacts) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Scope to Unarchived Appointments. |
||
| 381 | * |
||
| 382 | * @param Illuminate\Database\Query $query |
||
| 383 | * |
||
| 384 | * @return Illuminate\Database\Query |
||
| 385 | */ |
||
| 386 | public function scopeUnarchived($query) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Scope to Served Appointments. |
||
| 400 | * |
||
| 401 | * @param Illuminate\Database\Query $query |
||
| 402 | * |
||
| 403 | * @return Illuminate\Database\Query |
||
| 404 | */ |
||
| 405 | public function scopeServed($query) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Scope to Annulated Appointments. |
||
| 412 | * |
||
| 413 | * @param Illuminate\Database\Query $query |
||
| 414 | * |
||
| 415 | * @return Illuminate\Database\Query |
||
| 416 | */ |
||
| 417 | public function scopeAnnulated($query) |
||
| 421 | |||
| 422 | ///////////////////////// |
||
| 423 | // Soft Status Scoping // |
||
| 424 | ///////////////////////// |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Scope to not Served Appointments. |
||
| 428 | * |
||
| 429 | * @param Illuminate\Database\Query $query |
||
| 430 | * |
||
| 431 | * @return Illuminate\Database\Query |
||
| 432 | */ |
||
| 433 | public function scopeUnServed($query) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Scope to Active Appointments. |
||
| 440 | * |
||
| 441 | * @param Illuminate\Database\Query $query |
||
| 442 | * |
||
| 443 | * @return Illuminate\Database\Query |
||
| 444 | */ |
||
| 445 | public function scopeActive($query) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Scope of Business. |
||
| 452 | * |
||
| 453 | * @param Illuminate\Database\Query $query |
||
| 454 | * @param int $businessId |
||
| 455 | * |
||
| 456 | * @return Illuminate\Database\Query |
||
| 457 | */ |
||
| 458 | public function scopeOfBusiness($query, $businessId) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Scope of date. |
||
| 465 | * |
||
| 466 | * @param Illuminate\Database\Query $query |
||
| 467 | * @param Carbon $date |
||
| 468 | * |
||
| 469 | * @return Illuminate\Database\Query |
||
| 470 | */ |
||
| 471 | public function scopeOfDate($query, Carbon $date) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Scope only future appointments. |
||
| 478 | * |
||
| 479 | * @param Illuminate\Database\Query $query |
||
| 480 | * |
||
| 481 | * @return Illuminate\Database\Query |
||
| 482 | */ |
||
| 483 | public function scopeFuture($query) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Scope only till date. |
||
| 492 | * |
||
| 493 | * @param Illuminate\Database\Query $query |
||
| 494 | * @param Carbon $date |
||
| 495 | * |
||
| 496 | * @return Illuminate\Database\Query |
||
| 497 | */ |
||
| 498 | public function scopeTillDate($query, Carbon $date) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Between Dates. |
||
| 505 | * |
||
| 506 | * @param Illuminate\Database\Query $query |
||
| 507 | * @param Carbon $startAt |
||
| 508 | * @param Carbon $finishAt |
||
| 509 | * |
||
| 510 | * @return Illuminate\Database\Query |
||
| 511 | */ |
||
| 512 | public function scopeAffectingInterval($query, Carbon $startAt, Carbon $finishAt) |
||
| 536 | |||
| 537 | ////////////////////////// |
||
| 538 | // Soft Status Checkers // |
||
| 539 | ////////////////////////// |
||
| 540 | |||
| 541 | /** |
||
| 542 | * User is target contact of the appointment. |
||
| 543 | * |
||
| 544 | * @param int $userId |
||
| 545 | * |
||
| 546 | * @return bool |
||
| 547 | */ |
||
| 548 | public function isTarget($userId) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * User is issuer of the appointment. |
||
| 555 | * |
||
| 556 | * @param int $userId |
||
| 557 | * |
||
| 558 | * @return bool |
||
| 559 | */ |
||
| 560 | public function isIssuer($userId) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * User is owner of business. |
||
| 567 | * |
||
| 568 | * @param int $userId |
||
| 569 | * |
||
| 570 | * @return bool |
||
| 571 | */ |
||
| 572 | public function isOwner($userId) |
||
| 576 | |||
| 577 | /** |
||
| 578 | * can be annulated by user. |
||
| 579 | * |
||
| 580 | * @param int $userId |
||
| 581 | * |
||
| 582 | * @return bool |
||
| 583 | */ |
||
| 584 | public function canAnnulate($userId) |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Determine if it is still possible to annulate according business policy. |
||
| 593 | * |
||
| 594 | * @return bool |
||
| 595 | */ |
||
| 596 | public function isOnTimeToAnnulate() |
||
| 604 | |||
| 605 | /** |
||
| 606 | * can Serve. |
||
| 607 | * |
||
| 608 | * @param int $userId |
||
| 609 | * |
||
| 610 | * @return bool |
||
| 611 | */ |
||
| 612 | public function canServe($userId) |
||
| 616 | |||
| 617 | /** |
||
| 618 | * can confirm. |
||
| 619 | * |
||
| 620 | * @param int $userId |
||
| 621 | * |
||
| 622 | * @return bool |
||
| 623 | */ |
||
| 624 | public function canConfirm($userId) |
||
| 628 | |||
| 629 | /** |
||
| 630 | * is Serveable by user. |
||
| 631 | * |
||
| 632 | * @param int $userId |
||
| 633 | * |
||
| 634 | * @return bool |
||
| 635 | */ |
||
| 636 | public function isServeableBy($userId) |
||
| 640 | |||
| 641 | /** |
||
| 642 | * is Confirmable By user. |
||
| 643 | * |
||
| 644 | * @param int $userId |
||
| 645 | * |
||
| 646 | * @return bool |
||
| 647 | */ |
||
| 648 | public function isConfirmableBy($userId) |
||
| 655 | |||
| 656 | /** |
||
| 657 | * is Annulable By user. |
||
| 658 | * |
||
| 659 | * @param int $userId |
||
| 660 | * |
||
| 661 | * @return bool |
||
| 662 | */ |
||
| 663 | public function isAnnulableBy($userId) |
||
| 667 | |||
| 668 | /** |
||
| 669 | * Determine if the queried userId may confirm the appointment or not. |
||
| 670 | * |
||
| 671 | * @param int $userId |
||
| 672 | * |
||
| 673 | * @return bool |
||
| 674 | */ |
||
| 675 | public function shouldConfirmBy($userId) |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Determine if the target Contact's User is the same of the Appointment |
||
| 683 | * issuer User. |
||
| 684 | * |
||
| 685 | * @return bool |
||
| 686 | */ |
||
| 687 | public function isSelfIssued() |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Determine if the Serve action can be performed. |
||
| 704 | * |
||
| 705 | * @return bool |
||
| 706 | */ |
||
| 707 | public function isServeable() |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Determine if the Confirm action can be performed. |
||
| 714 | * |
||
| 715 | * @return bool |
||
| 716 | */ |
||
| 717 | public function isConfirmable() |
||
| 721 | |||
| 722 | /** |
||
| 723 | * Determine if the Annulate action can be performed. |
||
| 724 | * |
||
| 725 | * @return bool |
||
| 726 | */ |
||
| 727 | public function isAnnulable() |
||
| 731 | |||
| 732 | ///////////////////////// |
||
| 733 | // Hard Status Actions // |
||
| 734 | ///////////////////////// |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Check and perform Confirm action. |
||
| 738 | * |
||
| 739 | * @return $this |
||
| 740 | */ |
||
| 741 | public function doReserve() |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Check and perform Confirm action. |
||
| 752 | * |
||
| 753 | * @return $this |
||
| 754 | */ |
||
| 755 | public function doConfirm() |
||
| 765 | |||
| 766 | /** |
||
| 767 | * Check and perform Annulate action. |
||
| 768 | * |
||
| 769 | * @return $this |
||
| 770 | */ |
||
| 771 | public function doAnnulate() |
||
| 781 | |||
| 782 | /** |
||
| 783 | * Check and perform Serve action. |
||
| 784 | * |
||
| 785 | * @return $this |
||
| 786 | */ |
||
| 787 | public function doServe() |
||
| 797 | } |
||
| 798 |
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.