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; |
||
| 25 | class EntryModel extends EloquentModel implements EntryInterface, PresentableInterface |
||
| 26 | { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The foreign key for translations. |
||
| 30 | * |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $translationForeignKey = 'entry_id'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The validation rules. These are |
||
| 37 | * overridden on the compiled models. |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $rules = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The field slugs. These are |
||
| 45 | * overridden on compiled models. |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $fields = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The entry relationships by field slug. |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $relationships = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The compiled stream data. |
||
| 60 | * |
||
| 61 | * @var array|StreamInterface |
||
| 62 | */ |
||
| 63 | protected $stream = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Boot the model |
||
| 67 | */ |
||
| 68 | protected static function boot() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Sort the query. |
||
| 89 | * |
||
| 90 | * @param Builder $builder |
||
| 91 | * @param string $direction |
||
| 92 | */ |
||
| 93 | public function scopeSorted(Builder $builder, $direction = 'asc') |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Get the ID. |
||
| 100 | * |
||
| 101 | * @return mixed |
||
| 102 | */ |
||
| 103 | public function getId() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Get the entry ID. |
||
| 110 | * |
||
| 111 | * @return mixed |
||
| 112 | */ |
||
| 113 | public function getEntryId() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get the entry title. |
||
| 120 | * |
||
| 121 | * @return mixed |
||
| 122 | */ |
||
| 123 | public function getEntryTitle() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Get the model's bound name. |
||
| 130 | * |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | public function getBoundModelName() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get the model's bound namespace. |
||
| 140 | * |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | public function getBoundModelNamespace() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get the sort order. |
||
| 154 | * |
||
| 155 | * @return int |
||
| 156 | */ |
||
| 157 | public function getSortOrder() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get the entries title. |
||
| 164 | * |
||
| 165 | * @return mixed |
||
| 166 | */ |
||
| 167 | public function getTitle() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Get a field value. |
||
| 174 | * |
||
| 175 | * @param $fieldSlug |
||
| 176 | * @param null $locale |
||
| 177 | * @return mixed |
||
| 178 | */ |
||
| 179 | public function getFieldValue($fieldSlug, $locale = null) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Set a field value. |
||
| 222 | * |
||
| 223 | * @param $fieldSlug |
||
| 224 | * @param $value |
||
| 225 | * @param null $locale |
||
| 226 | * @return $this |
||
| 227 | */ |
||
| 228 | public function setFieldValue($fieldSlug, $value, $locale = null) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Get an entry field. |
||
| 259 | * |
||
| 260 | * @param $slug |
||
| 261 | * @return FieldInterface|null |
||
| 262 | */ |
||
| 263 | public function getField($slug) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Return whether an entry has |
||
| 276 | * a field with a given slug. |
||
| 277 | * |
||
| 278 | * @param $slug |
||
| 279 | * @return bool |
||
| 280 | */ |
||
| 281 | public function hasField($slug) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Get the field type from a field slug. |
||
| 288 | * |
||
| 289 | * @param $fieldSlug |
||
| 290 | * @return null|FieldType |
||
| 291 | */ |
||
| 292 | View Code Duplication | public function getFieldType($fieldSlug) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Get the field type query. |
||
| 323 | * |
||
| 324 | * @param $fieldSlug |
||
| 325 | * @return FieldTypeQuery |
||
| 326 | */ |
||
| 327 | public function getFieldTypeQuery($fieldSlug) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Get the field type presenter. |
||
| 338 | * |
||
| 339 | * @param $fieldSlug |
||
| 340 | * @return FieldTypePresenter |
||
| 341 | */ |
||
| 342 | public function getFieldTypePresenter($fieldSlug) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Set a given attribute on the model. |
||
| 353 | * Override the behavior here to give |
||
| 354 | * the field types a chance to modify things. |
||
| 355 | * |
||
| 356 | * @param string $key |
||
| 357 | * @param mixed $value |
||
| 358 | * @return $this |
||
| 359 | */ |
||
| 360 | public function setAttribute($key, $value) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Get a given attribute on the model. |
||
| 373 | * Override the behavior here to give |
||
| 374 | * the field types a chance to modify things. |
||
| 375 | * |
||
| 376 | * @param string $key |
||
| 377 | * @return mixed |
||
| 378 | */ |
||
| 379 | public function getAttribute($key) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Get a raw unmodified attribute. |
||
| 398 | * |
||
| 399 | * @param $key |
||
| 400 | * @param bool $process |
||
| 401 | * @return mixed|null |
||
| 402 | */ |
||
| 403 | public function getRawAttribute($key, $process = true) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Set a raw unmodified attribute. |
||
| 414 | * |
||
| 415 | * @param $key |
||
| 416 | * @param $value |
||
| 417 | * @return $this |
||
| 418 | */ |
||
| 419 | public function setRawAttribute($key, $value) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Get the stream. |
||
| 428 | * |
||
| 429 | * @return StreamInterface |
||
| 430 | */ |
||
| 431 | public function getStream() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Get the stream namespace. |
||
| 438 | * |
||
| 439 | * @return string |
||
| 440 | */ |
||
| 441 | public function getStreamNamespace() |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Get the stream slug. |
||
| 450 | * |
||
| 451 | * @return string |
||
| 452 | */ |
||
| 453 | public function getStreamSlug() |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Get the entry's stream name. |
||
| 462 | * |
||
| 463 | * @return string |
||
| 464 | */ |
||
| 465 | public function getStreamName() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Get the stream prefix. |
||
| 474 | * |
||
| 475 | * @return string |
||
| 476 | */ |
||
| 477 | public function getStreamPrefix() |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Get the table name. |
||
| 486 | * |
||
| 487 | * @return string |
||
| 488 | */ |
||
| 489 | public function getTableName() |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Get the translations table name. |
||
| 498 | * |
||
| 499 | * @return string |
||
| 500 | */ |
||
| 501 | public function getTranslationsTableName() |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Get all assignments. |
||
| 510 | * |
||
| 511 | * @return AssignmentCollection |
||
| 512 | */ |
||
| 513 | public function getAssignments() |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Get the field slugs for assigned fields. |
||
| 522 | * |
||
| 523 | * @param null $prefix |
||
| 524 | * @return array |
||
| 525 | */ |
||
| 526 | public function getAssignmentFieldSlugs($prefix = null) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Get all assignments of the |
||
| 535 | * provided field type namespace. |
||
| 536 | * |
||
| 537 | * @param $fieldType |
||
| 538 | * @return AssignmentCollection |
||
| 539 | */ |
||
| 540 | public function getAssignmentsByFieldType($fieldType) |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Get an assignment by field slug. |
||
| 549 | * |
||
| 550 | * @param $fieldSlug |
||
| 551 | * @return AssignmentInterface |
||
| 552 | */ |
||
| 553 | public function getAssignment($fieldSlug) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Return translated assignments. |
||
| 562 | * |
||
| 563 | * @return AssignmentCollection |
||
| 564 | */ |
||
| 565 | public function getTranslatableAssignments() |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Return relation assignments. |
||
| 575 | * |
||
| 576 | * @return AssignmentCollection |
||
| 577 | */ |
||
| 578 | public function getRelationshipAssignments() |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Get the translatable flag. |
||
| 588 | * |
||
| 589 | * @return bool |
||
| 590 | */ |
||
| 591 | public function isTranslatable() |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Return whether the entry is trashable or not. |
||
| 600 | * |
||
| 601 | * @return bool |
||
| 602 | */ |
||
| 603 | public function isTrashable() |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Return the last modified datetime. |
||
| 612 | * |
||
| 613 | * @return Carbon |
||
| 614 | */ |
||
| 615 | public function lastModified() |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Return whether the title column is |
||
| 622 | * translatable or not. |
||
| 623 | * |
||
| 624 | * @return bool |
||
| 625 | */ |
||
| 626 | public function titleColumnIsTranslatable() |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Return whether or not the assignment for |
||
| 633 | * the given field slug is translatable. |
||
| 634 | * |
||
| 635 | * @param $fieldSlug |
||
| 636 | * @return bool |
||
| 637 | */ |
||
| 638 | public function assignmentIsTranslatable($fieldSlug) |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Return whether or not the assignment for |
||
| 645 | * the given field slug is a relationship. |
||
| 646 | * |
||
| 647 | * @param $fieldSlug |
||
| 648 | * @return bool |
||
| 649 | */ |
||
| 650 | public function assignmentIsRelationship($fieldSlug) |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Fire field type events. |
||
| 659 | * |
||
| 660 | * @param $trigger |
||
| 661 | * @param array $payload |
||
| 662 | */ |
||
| 663 | public function fireFieldTypeEvents($trigger, $payload = []) |
||
| 682 | |||
| 683 | /** |
||
| 684 | * Return the related stream. |
||
| 685 | * |
||
| 686 | * @return StreamInterface|array |
||
| 687 | */ |
||
| 688 | public function stream() |
||
| 696 | |||
| 697 | /** |
||
| 698 | * @param array $items |
||
| 699 | * @return EntryCollection |
||
| 700 | */ |
||
| 701 | public function newCollection(array $items = []) |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Return the entry presenter. |
||
| 714 | * |
||
| 715 | * This is against standards but required |
||
| 716 | * by the presentable interface. |
||
| 717 | * |
||
| 718 | * @return EntryPresenter |
||
| 719 | */ |
||
| 720 | View Code Duplication | public function getPresenter() |
|
| 730 | |||
| 731 | /** |
||
| 732 | * Return a new presenter instance. |
||
| 733 | * |
||
| 734 | * @return EntryPresenter |
||
| 735 | */ |
||
| 736 | public function newPresenter() |
||
| 740 | |||
| 741 | /** |
||
| 742 | * Get a new query builder for the model's table. |
||
| 743 | * |
||
| 744 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 745 | */ |
||
| 746 | View Code Duplication | public function newQuery() |
|
| 757 | |||
| 758 | /** |
||
| 759 | * Get the criteria class. |
||
| 760 | * |
||
| 761 | * @return string |
||
| 762 | */ |
||
| 763 | public function getCriteriaName() |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Override the __get method. |
||
| 772 | * |
||
| 773 | * @param string $key |
||
| 774 | * @return EntryPresenter|mixed |
||
| 775 | */ |
||
| 776 | public function __get($key) |
||
| 784 | } |
||
| 785 |
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.