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 | * Boot the model |
||
| 70 | */ |
||
| 71 | protected static function boot() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Sort the query. |
||
| 97 | * |
||
| 98 | * @param Builder $builder |
||
| 99 | * @param string $direction |
||
| 100 | */ |
||
| 101 | public function scopeSorted(Builder $builder, $direction = 'asc') |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get the ID. |
||
| 108 | * |
||
| 109 | * @return mixed |
||
| 110 | */ |
||
| 111 | public function getId() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Get the entry ID. |
||
| 118 | * |
||
| 119 | * @return mixed |
||
| 120 | */ |
||
| 121 | public function getEntryId() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get the entry title. |
||
| 128 | * |
||
| 129 | * @return mixed |
||
| 130 | */ |
||
| 131 | public function getEntryTitle() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Get the model's bound name. |
||
| 138 | * |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | public function getBoundModelName() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Get the model's bound namespace. |
||
| 148 | * |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function getBoundModelNamespace() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get the sort order. |
||
| 162 | * |
||
| 163 | * @return int |
||
| 164 | */ |
||
| 165 | public function getSortOrder() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Get the entries title. |
||
| 172 | * |
||
| 173 | * @return mixed |
||
| 174 | */ |
||
| 175 | public function getTitle() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Get a field value. |
||
| 182 | * |
||
| 183 | * @param $fieldSlug |
||
| 184 | * @param null $locale |
||
| 185 | * @return mixed |
||
| 186 | */ |
||
| 187 | public function getFieldValue($fieldSlug, $locale = null) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Set a field value. |
||
| 228 | * |
||
| 229 | * @param $fieldSlug |
||
| 230 | * @param $value |
||
| 231 | * @param null $locale |
||
| 232 | * @return $this |
||
| 233 | */ |
||
| 234 | public function setFieldValue($fieldSlug, $value, $locale = null) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Get an entry field. |
||
| 264 | * |
||
| 265 | * @param $slug |
||
| 266 | * @return FieldInterface|null |
||
| 267 | */ |
||
| 268 | public function getField($slug) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Return whether an entry has |
||
| 281 | * a field with a given slug. |
||
| 282 | * |
||
| 283 | * @param $slug |
||
| 284 | * @return bool |
||
| 285 | */ |
||
| 286 | public function hasField($slug) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Get the field type from a field slug. |
||
| 293 | * |
||
| 294 | * @param $fieldSlug |
||
| 295 | * @return null|FieldType |
||
| 296 | */ |
||
| 297 | View Code Duplication | public function getFieldType($fieldSlug) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Get the field type query. |
||
| 327 | * |
||
| 328 | * @param $fieldSlug |
||
| 329 | * @return FieldTypeQuery |
||
| 330 | */ |
||
| 331 | public function getFieldTypeQuery($fieldSlug) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Get the field type presenter. |
||
| 342 | * |
||
| 343 | * @param $fieldSlug |
||
| 344 | * @return FieldTypePresenter |
||
| 345 | */ |
||
| 346 | public function getFieldTypePresenter($fieldSlug) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Set a given attribute on the model. |
||
| 357 | * Override the behavior here to give |
||
| 358 | * the field types a chance to modify things. |
||
| 359 | * |
||
| 360 | * @param string $key |
||
| 361 | * @param mixed $value |
||
| 362 | * @return $this |
||
| 363 | */ |
||
| 364 | public function setAttribute($key, $value) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Get a given attribute on the model. |
||
| 377 | * Override the behavior here to give |
||
| 378 | * the field types a chance to modify things. |
||
| 379 | * |
||
| 380 | * @param string $key |
||
| 381 | * @return mixed |
||
| 382 | */ |
||
| 383 | public function getAttribute($key) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Get a raw unmodified attribute. |
||
| 402 | * |
||
| 403 | * @param $key |
||
| 404 | * @param bool $process |
||
| 405 | * @return mixed|null |
||
| 406 | */ |
||
| 407 | public function getRawAttribute($key, $process = true) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Set a raw unmodified attribute. |
||
| 418 | * |
||
| 419 | * @param $key |
||
| 420 | * @param $value |
||
| 421 | * @return $this |
||
| 422 | */ |
||
| 423 | public function setRawAttribute($key, $value) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Get the stream. |
||
| 432 | * |
||
| 433 | * @return StreamInterface |
||
| 434 | */ |
||
| 435 | public function getStream() |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Get the stream namespace. |
||
| 442 | * |
||
| 443 | * @return string |
||
| 444 | */ |
||
| 445 | public function getStreamNamespace() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Get the stream slug. |
||
| 454 | * |
||
| 455 | * @return string |
||
| 456 | */ |
||
| 457 | public function getStreamSlug() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Get the entry's stream name. |
||
| 466 | * |
||
| 467 | * @return string |
||
| 468 | */ |
||
| 469 | public function getStreamName() |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Get the stream prefix. |
||
| 478 | * |
||
| 479 | * @return string |
||
| 480 | */ |
||
| 481 | public function getStreamPrefix() |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Get the table name. |
||
| 490 | * |
||
| 491 | * @return string |
||
| 492 | */ |
||
| 493 | public function getTableName() |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Get the translations table name. |
||
| 502 | * |
||
| 503 | * @return string |
||
| 504 | */ |
||
| 505 | public function getTranslationsTableName() |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Get all assignments. |
||
| 514 | * |
||
| 515 | * @return AssignmentCollection |
||
| 516 | */ |
||
| 517 | public function getAssignments() |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Get the field slugs for assigned fields. |
||
| 526 | * |
||
| 527 | * @param null $prefix |
||
| 528 | * @return array |
||
| 529 | */ |
||
| 530 | public function getAssignmentFieldSlugs($prefix = null) |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Get all assignments of the |
||
| 539 | * provided field type namespace. |
||
| 540 | * |
||
| 541 | * @param $fieldType |
||
| 542 | * @return AssignmentCollection |
||
| 543 | */ |
||
| 544 | public function getAssignmentsByFieldType($fieldType) |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Get an assignment by field slug. |
||
| 553 | * |
||
| 554 | * @param $fieldSlug |
||
| 555 | * @return AssignmentInterface |
||
| 556 | */ |
||
| 557 | public function getAssignment($fieldSlug) |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Return translated assignments. |
||
| 566 | * |
||
| 567 | * @return AssignmentCollection |
||
| 568 | */ |
||
| 569 | public function getTranslatableAssignments() |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Return relation assignments. |
||
| 579 | * |
||
| 580 | * @return AssignmentCollection |
||
| 581 | */ |
||
| 582 | public function getRelationshipAssignments() |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Get the translatable flag. |
||
| 592 | * |
||
| 593 | * @return bool |
||
| 594 | */ |
||
| 595 | public function isTranslatable() |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Return whether the entry is trashable or not. |
||
| 604 | * |
||
| 605 | * @return bool |
||
| 606 | */ |
||
| 607 | public function isTrashable() |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Return the last modified datetime. |
||
| 616 | * |
||
| 617 | * @return Carbon |
||
| 618 | */ |
||
| 619 | public function lastModified() |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Return the related creator. |
||
| 626 | * |
||
| 627 | * @return Authenticatable |
||
| 628 | */ |
||
| 629 | public function getCreatedBy() |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Return the creator relation. |
||
| 636 | * |
||
| 637 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 638 | */ |
||
| 639 | public function createdBy() |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Return the related updater. |
||
| 646 | * |
||
| 647 | * @return Authenticatable |
||
| 648 | */ |
||
| 649 | public function getUpdatedBy() |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Return the updater relation. |
||
| 656 | * |
||
| 657 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
| 658 | */ |
||
| 659 | public function updatedBy() |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Return whether the title column is |
||
| 666 | * translatable or not. |
||
| 667 | * |
||
| 668 | * @return bool |
||
| 669 | */ |
||
| 670 | public function titleColumnIsTranslatable() |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Return whether or not the assignment for |
||
| 677 | * the given field slug is translatable. |
||
| 678 | * |
||
| 679 | * @param $fieldSlug |
||
| 680 | * @return bool |
||
| 681 | */ |
||
| 682 | public function assignmentIsTranslatable($fieldSlug) |
||
| 686 | |||
| 687 | /** |
||
| 688 | * Return whether or not the assignment for |
||
| 689 | * the given field slug is a relationship. |
||
| 690 | * |
||
| 691 | * @param $fieldSlug |
||
| 692 | * @return bool |
||
| 693 | */ |
||
| 694 | public function assignmentIsRelationship($fieldSlug) |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Fire field type events. |
||
| 703 | * |
||
| 704 | * @param $trigger |
||
| 705 | * @param array $payload |
||
| 706 | */ |
||
| 707 | public function fireFieldTypeEvents($trigger, $payload = []) |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Return the related stream. |
||
| 728 | * |
||
| 729 | * @return StreamInterface|array |
||
| 730 | */ |
||
| 731 | public function stream() |
||
| 739 | |||
| 740 | /** |
||
| 741 | * @param array $items |
||
| 742 | * @return EntryCollection |
||
| 743 | */ |
||
| 744 | View Code Duplication | public function newCollection(array $items = []) |
|
| 754 | |||
| 755 | /** |
||
| 756 | * Return the entry presenter. |
||
| 757 | * |
||
| 758 | * This is against standards but required |
||
| 759 | * by the presentable interface. |
||
| 760 | * |
||
| 761 | * @return EntryPresenter |
||
| 762 | */ |
||
| 763 | View Code Duplication | public function getPresenter() |
|
| 773 | |||
| 774 | /** |
||
| 775 | * Return a new presenter instance. |
||
| 776 | * |
||
| 777 | * @return EntryPresenter |
||
| 778 | */ |
||
| 779 | public function newPresenter() |
||
| 783 | |||
| 784 | /** |
||
| 785 | * Return a model route. |
||
| 786 | * |
||
| 787 | * @return string |
||
| 788 | */ |
||
| 789 | public function route($route, array $parameters = []) |
||
| 795 | |||
| 796 | /** |
||
| 797 | * Return a new router instance. |
||
| 798 | * |
||
| 799 | * @return EntryRouter |
||
| 800 | */ |
||
| 801 | public function newRouter() |
||
| 805 | |||
| 806 | /** |
||
| 807 | * Get the router. |
||
| 808 | * |
||
| 809 | * @return EntryRouter |
||
| 810 | */ |
||
| 811 | public function getRouter() |
||
| 819 | |||
| 820 | /** |
||
| 821 | * Get the router name. |
||
| 822 | * |
||
| 823 | * @return string |
||
| 824 | */ |
||
| 825 | public function getRouterName() |
||
| 831 | |||
| 832 | /** |
||
| 833 | * Create a new Eloquent query builder for the model. |
||
| 834 | * |
||
| 835 | * @param \Illuminate\Database\Query\Builder $query |
||
| 836 | * @return \Illuminate\Database\Eloquent\Builder|static |
||
| 837 | */ |
||
| 838 | public function newEloquentBuilder($query) |
||
| 842 | |||
| 843 | /** |
||
| 844 | * Get the criteria class. |
||
| 845 | * |
||
| 846 | * @return string |
||
| 847 | */ |
||
| 848 | public function getCriteriaName() |
||
| 854 | |||
| 855 | /** |
||
| 856 | * Return whether the model is searchable or not. |
||
| 857 | * |
||
| 858 | * @return boolean |
||
| 859 | */ |
||
| 860 | public function isSearchable() |
||
| 864 | |||
| 865 | /** |
||
| 866 | * Return a searchable array. |
||
| 867 | * |
||
| 868 | * @return array |
||
| 869 | */ |
||
| 870 | public function toSearchableArray() |
||
| 883 | |||
| 884 | /** |
||
| 885 | * Override the __get method. |
||
| 886 | * |
||
| 887 | * @param string $key |
||
| 888 | * @return EntryPresenter|mixed |
||
| 889 | */ |
||
| 890 | public function __get($key) |
||
| 898 | } |
||
| 899 |
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.