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 EntryModel 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 EntryModel, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Anomaly\Streams\Platform\Entry; |
||
| 19 | class EntryModel extends EloquentModel implements EntryInterface, PresentableInterface |
||
| 20 | { |
||
| 21 | |||
| 22 | use Searchable; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The foreign key for translations. |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $translationForeignKey = 'entry_id'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * By default nothing is searchable. |
||
| 33 | * |
||
| 34 | * @var boolean |
||
| 35 | */ |
||
| 36 | protected $searchable = false; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The validation rules. These are |
||
| 40 | * overridden on the compiled models. |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $rules = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The field slugs. These are |
||
| 48 | * overridden on compiled models. |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | protected $fields = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The entry relationships by field slug. |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $relationships = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The compiled stream data. |
||
| 63 | * |
||
| 64 | * @var array|StreamInterface |
||
| 65 | */ |
||
| 66 | protected $stream = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var bool |
||
| 70 | */ |
||
| 71 | protected $raw = false; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Boot the model |
||
| 75 | */ |
||
| 76 | protected static function boot() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Sort the query. |
||
| 102 | * |
||
| 103 | * @param Builder $builder |
||
| 104 | * @param string $direction |
||
| 105 | */ |
||
| 106 | public function scopeSorted(Builder $builder, $direction = 'asc') |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Get the raw flag property. |
||
| 113 | * |
||
| 114 | * @return bool |
||
| 115 | */ |
||
| 116 | public function getRaw() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Set the raw flag property. |
||
| 123 | * |
||
| 124 | * @param bool $raw |
||
| 125 | */ |
||
| 126 | public function setRaw($raw) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Get the ID. |
||
| 133 | * |
||
| 134 | * @return mixed |
||
| 135 | */ |
||
| 136 | public function getId() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get the entry ID. |
||
| 143 | * |
||
| 144 | * @return mixed |
||
| 145 | */ |
||
| 146 | public function getEntryId() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Get the entry title. |
||
| 153 | * |
||
| 154 | * @return mixed |
||
| 155 | */ |
||
| 156 | public function getEntryTitle() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Get the model's bound name. |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | public function getBoundModelName() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get the model's bound namespace. |
||
| 173 | * |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | public function getBoundModelNamespace() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Get the sort order. |
||
| 187 | * |
||
| 188 | * @return int |
||
| 189 | */ |
||
| 190 | public function getSortOrder() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Get the entries title. |
||
| 197 | * |
||
| 198 | * @return mixed |
||
| 199 | */ |
||
| 200 | public function getTitle() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Get a field value. |
||
| 207 | * |
||
| 208 | * @param $fieldSlug |
||
| 209 | * @param null $locale |
||
| 210 | * @return mixed |
||
| 211 | */ |
||
| 212 | public function getFieldValue($fieldSlug, $locale = null) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Set a field value. |
||
| 253 | * |
||
| 254 | * @param $fieldSlug |
||
| 255 | * @param $value |
||
| 256 | * @param null $locale |
||
| 257 | * @return $this |
||
| 258 | */ |
||
| 259 | public function setFieldValue($fieldSlug, $value, $locale = null) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Get an entry field. |
||
| 289 | * |
||
| 290 | * @param $slug |
||
| 291 | * @return FieldInterface|null |
||
| 292 | */ |
||
| 293 | public function getField($slug) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Return whether an entry has |
||
| 306 | * a field with a given slug. |
||
| 307 | * |
||
| 308 | * @param $slug |
||
| 309 | * @return bool |
||
| 310 | */ |
||
| 311 | public function hasField($slug) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Get the field type from a field slug. |
||
| 318 | * |
||
| 319 | * @param $fieldSlug |
||
| 320 | * @return null|FieldType |
||
| 321 | */ |
||
| 322 | View Code Duplication | public function getFieldType($fieldSlug) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Get the field type query. |
||
| 352 | * |
||
| 353 | * @param $fieldSlug |
||
| 354 | * @return FieldTypeQuery |
||
| 355 | */ |
||
| 356 | public function getFieldTypeQuery($fieldSlug) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Get the field type presenter. |
||
| 367 | * |
||
| 368 | * @param $fieldSlug |
||
| 369 | * @return FieldTypePresenter |
||
| 370 | */ |
||
| 371 | public function getFieldTypePresenter($fieldSlug) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Set a given attribute on the model. |
||
| 382 | * Override the behavior here to give |
||
| 383 | * the field types a chance to modify things. |
||
| 384 | * |
||
| 385 | * @param string $key |
||
| 386 | * @param mixed $value |
||
| 387 | * @return $this |
||
| 388 | */ |
||
| 389 | public function setAttribute($key, $value) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Get a given attribute on the model. |
||
| 402 | * Override the behavior here to give |
||
| 403 | * the field types a chance to modify things. |
||
| 404 | * |
||
| 405 | * @param string $key |
||
| 406 | * @return mixed |
||
| 407 | */ |
||
| 408 | public function getAttribute($key) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Get a raw unmodified attribute. |
||
| 428 | * |
||
| 429 | * @param $key |
||
| 430 | * @param bool $process |
||
| 431 | * @return mixed|null |
||
| 432 | */ |
||
| 433 | public function getRawAttribute($key, $process = true) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Set a raw unmodified attribute. |
||
| 444 | * |
||
| 445 | * @param $key |
||
| 446 | * @param $value |
||
| 447 | * @return $this |
||
| 448 | */ |
||
| 449 | public function setRawAttribute($key, $value) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Get the stream. |
||
| 458 | * |
||
| 459 | * @return StreamInterface |
||
| 460 | */ |
||
| 461 | public function getStream() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Get the stream namespace. |
||
| 468 | * |
||
| 469 | * @return string |
||
| 470 | */ |
||
| 471 | public function getStreamNamespace() |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Get the stream slug. |
||
| 480 | * |
||
| 481 | * @return string |
||
| 482 | */ |
||
| 483 | public function getStreamSlug() |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Get the entry's stream name. |
||
| 492 | * |
||
| 493 | * @return string |
||
| 494 | */ |
||
| 495 | public function getStreamName() |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Get the stream prefix. |
||
| 504 | * |
||
| 505 | * @return string |
||
| 506 | */ |
||
| 507 | public function getStreamPrefix() |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Get the table name. |
||
| 516 | * |
||
| 517 | * @return string |
||
| 518 | */ |
||
| 519 | public function getTableName() |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Get the translations table name. |
||
| 528 | * |
||
| 529 | * @return string |
||
| 530 | */ |
||
| 531 | public function getTranslationsTableName() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Get all assignments. |
||
| 540 | * |
||
| 541 | * @return AssignmentCollection |
||
| 542 | */ |
||
| 543 | public function getAssignments() |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Get the field slugs for assigned fields. |
||
| 552 | * |
||
| 553 | * @param null $prefix |
||
| 554 | * @return array |
||
| 555 | */ |
||
| 556 | public function getAssignmentFieldSlugs($prefix = null) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Get all assignments of the |
||
| 565 | * provided field type namespace. |
||
| 566 | * |
||
| 567 | * @param $fieldType |
||
| 568 | * @return AssignmentCollection |
||
| 569 | */ |
||
| 570 | public function getAssignmentsByFieldType($fieldType) |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Get an assignment by field slug. |
||
| 579 | * |
||
| 580 | * @param $fieldSlug |
||
| 581 | * @return AssignmentInterface |
||
| 582 | */ |
||
| 583 | public function getAssignment($fieldSlug) |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Return translated assignments. |
||
| 592 | * |
||
| 593 | * @return AssignmentCollection |
||
| 594 | */ |
||
| 595 | public function getTranslatableAssignments() |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Return relation assignments. |
||
| 605 | * |
||
| 606 | * @return AssignmentCollection |
||
| 607 | */ |
||
| 608 | public function getRelationshipAssignments() |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Get the translatable flag. |
||
| 618 | * |
||
| 619 | * @return bool |
||
| 620 | */ |
||
| 621 | public function isTranslatable() |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Return whether the entry is trashable or not. |
||
| 630 | * |
||
| 631 | * @return bool |
||
| 632 | */ |
||
| 633 | public function isTrashable() |
||
| 639 | |||
| 640 | /** |
||
| 641 | * Return the last modified datetime. |
||
| 642 | * |
||
| 643 | * @return Carbon |
||
| 644 | */ |
||
| 645 | public function lastModified() |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Return the related creator. |
||
| 652 | * |
||
| 653 | * @return Authenticatable |
||
| 654 | */ |
||
| 655 | public function getCreatedBy() |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Return the creator relation. |
||
| 662 | * |
||
| 663 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 664 | */ |
||
| 665 | public function createdBy() |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Return the related updater. |
||
| 672 | * |
||
| 673 | * @return Authenticatable |
||
| 674 | */ |
||
| 675 | public function getUpdatedBy() |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Return the updater relation. |
||
| 682 | * |
||
| 683 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 684 | */ |
||
| 685 | public function updatedBy() |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Return whether the title column is |
||
| 692 | * translatable or not. |
||
| 693 | * |
||
| 694 | * @return bool |
||
| 695 | */ |
||
| 696 | public function titleColumnIsTranslatable() |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Return whether or not the assignment for |
||
| 703 | * the given field slug is translatable. |
||
| 704 | * |
||
| 705 | * @param $fieldSlug |
||
| 706 | * @return bool |
||
| 707 | */ |
||
| 708 | public function assignmentIsTranslatable($fieldSlug) |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Return whether or not the assignment for |
||
| 715 | * the given field slug is a relationship. |
||
| 716 | * |
||
| 717 | * @param $fieldSlug |
||
| 718 | * @return bool |
||
| 719 | */ |
||
| 720 | public function assignmentIsRelationship($fieldSlug) |
||
| 726 | |||
| 727 | /** |
||
| 728 | * Fire field type events. |
||
| 729 | * |
||
| 730 | * @param $trigger |
||
| 731 | * @param array $payload |
||
| 732 | */ |
||
| 733 | public function fireFieldTypeEvents($trigger, $payload = []) |
||
| 751 | |||
| 752 | /** |
||
| 753 | * Return the related stream. |
||
| 754 | * |
||
| 755 | * @return StreamInterface|array |
||
| 756 | */ |
||
| 757 | public function stream() |
||
| 765 | |||
| 766 | /** |
||
| 767 | * @param array $items |
||
| 768 | * @return EntryCollection |
||
| 769 | */ |
||
| 770 | View Code Duplication | public function newCollection(array $items = []) |
|
| 780 | |||
| 781 | /** |
||
| 782 | * Return the entry presenter. |
||
| 783 | * |
||
| 784 | * This is against standards but required |
||
| 785 | * by the presentable interface. |
||
| 786 | * |
||
| 787 | * @return EntryPresenter |
||
| 788 | */ |
||
| 789 | View Code Duplication | public function getPresenter() |
|
| 799 | |||
| 800 | /** |
||
| 801 | * Return a new presenter instance. |
||
| 802 | * |
||
| 803 | * @return EntryPresenter |
||
| 804 | */ |
||
| 805 | public function newPresenter() |
||
| 809 | |||
| 810 | /** |
||
| 811 | * Return a model route. |
||
| 812 | * |
||
| 813 | * @return string |
||
| 814 | */ |
||
| 815 | public function route($route, array $parameters = []) |
||
| 821 | |||
| 822 | /** |
||
| 823 | * Return a new router instance. |
||
| 824 | * |
||
| 825 | * @return EntryRouter |
||
| 826 | */ |
||
| 827 | public function newRouter() |
||
| 831 | |||
| 832 | /** |
||
| 833 | * Get the router. |
||
| 834 | * |
||
| 835 | * @return EntryRouter |
||
| 836 | */ |
||
| 837 | public function getRouter() |
||
| 845 | |||
| 846 | /** |
||
| 847 | * Get the router name. |
||
| 848 | * |
||
| 849 | * @return string |
||
| 850 | */ |
||
| 851 | public function getRouterName() |
||
| 857 | |||
| 858 | /** |
||
| 859 | * Create a new Eloquent query builder for the model. |
||
| 860 | * |
||
| 861 | * @param \Illuminate\Database\Query\Builder $query |
||
| 862 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 863 | */ |
||
| 864 | public function newEloquentBuilder($query) |
||
| 868 | |||
| 869 | /** |
||
| 870 | * Get the criteria class. |
||
| 871 | * |
||
| 872 | * @return string |
||
| 873 | */ |
||
| 874 | public function getCriteriaName() |
||
| 880 | |||
| 881 | /** |
||
| 882 | * Return whether the model is searchable or not. |
||
| 883 | * |
||
| 884 | * @return boolean |
||
| 885 | */ |
||
| 886 | public function isSearchable() |
||
| 890 | |||
| 891 | /** |
||
| 892 | * Return a searchable array. |
||
| 893 | * |
||
| 894 | * @return array |
||
| 895 | */ |
||
| 896 | public function toSearchableArray() |
||
| 909 | |||
| 910 | /** |
||
| 911 | * Override the __get method. |
||
| 912 | * |
||
| 913 | * @param string $key |
||
| 914 | * @return EntryPresenter|mixed |
||
| 915 | */ |
||
| 916 | public function __get($key) |
||
| 924 | } |
||
| 925 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.