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 sort order. |
||
| 130 | * |
||
| 131 | * @return int |
||
| 132 | */ |
||
| 133 | public function getSortOrder() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get the entries title. |
||
| 140 | * |
||
| 141 | * @return mixed |
||
| 142 | */ |
||
| 143 | public function getTitle() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Get a field value. |
||
| 150 | * |
||
| 151 | * @param $fieldSlug |
||
| 152 | * @param null $locale |
||
| 153 | * @return mixed |
||
| 154 | */ |
||
| 155 | public function getFieldValue($fieldSlug, $locale = null) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Set a field value. |
||
| 198 | * |
||
| 199 | * @param $fieldSlug |
||
| 200 | * @param $value |
||
| 201 | * @param null $locale |
||
| 202 | * @return $this |
||
| 203 | */ |
||
| 204 | public function setFieldValue($fieldSlug, $value, $locale = null) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Get an entry field. |
||
| 235 | * |
||
| 236 | * @param $slug |
||
| 237 | * @return FieldInterface|null |
||
| 238 | */ |
||
| 239 | public function getField($slug) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Return whether an entry has |
||
| 252 | * a field with a given slug. |
||
| 253 | * |
||
| 254 | * @param $slug |
||
| 255 | * @return bool |
||
| 256 | */ |
||
| 257 | public function hasField($slug) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Get the field type from a field slug. |
||
| 264 | * |
||
| 265 | * @param $fieldSlug |
||
| 266 | * @return null|FieldType |
||
| 267 | */ |
||
| 268 | View Code Duplication | public function getFieldType($fieldSlug) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Get the field type query. |
||
| 299 | * |
||
| 300 | * @param $fieldSlug |
||
| 301 | * @return FieldTypeQuery |
||
| 302 | */ |
||
| 303 | public function getFieldTypeQuery($fieldSlug) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Get the field type presenter. |
||
| 314 | * |
||
| 315 | * @param $fieldSlug |
||
| 316 | * @return FieldTypePresenter |
||
| 317 | */ |
||
| 318 | public function getFieldTypePresenter($fieldSlug) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Set a given attribute on the model. |
||
| 329 | * Override the behavior here to give |
||
| 330 | * the field types a chance to modify things. |
||
| 331 | * |
||
| 332 | * @param string $key |
||
| 333 | * @param mixed $value |
||
| 334 | * @return $this |
||
| 335 | */ |
||
| 336 | public function setAttribute($key, $value) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Get a given attribute on the model. |
||
| 349 | * Override the behavior here to give |
||
| 350 | * the field types a chance to modify things. |
||
| 351 | * |
||
| 352 | * @param string $key |
||
| 353 | * @return mixed |
||
| 354 | */ |
||
| 355 | public function getAttribute($key) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Get a raw unmodified attribute. |
||
| 374 | * |
||
| 375 | * @param $key |
||
| 376 | * @param bool $process |
||
| 377 | * @return mixed|null |
||
| 378 | */ |
||
| 379 | public function getRawAttribute($key, $process = true) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Set a raw unmodified attribute. |
||
| 390 | * |
||
| 391 | * @param $key |
||
| 392 | * @param $value |
||
| 393 | * @return $this |
||
| 394 | */ |
||
| 395 | public function setRawAttribute($key, $value) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Get the stream. |
||
| 404 | * |
||
| 405 | * @return StreamInterface |
||
| 406 | */ |
||
| 407 | public function getStream() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Get the stream namespace. |
||
| 414 | * |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | public function getStreamNamespace() |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Get the stream slug. |
||
| 426 | * |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | public function getStreamSlug() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Get the entry's stream name. |
||
| 438 | * |
||
| 439 | * @return string |
||
| 440 | */ |
||
| 441 | public function getStreamName() |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Get the stream prefix. |
||
| 450 | * |
||
| 451 | * @return string |
||
| 452 | */ |
||
| 453 | public function getStreamPrefix() |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Get the table name. |
||
| 462 | * |
||
| 463 | * @return string |
||
| 464 | */ |
||
| 465 | public function getTableName() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Get the translations table name. |
||
| 474 | * |
||
| 475 | * @return string |
||
| 476 | */ |
||
| 477 | public function getTranslationsTableName() |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Get all assignments. |
||
| 486 | * |
||
| 487 | * @return AssignmentCollection |
||
| 488 | */ |
||
| 489 | public function getAssignments() |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Get the field slugs for assigned fields. |
||
| 498 | * |
||
| 499 | * @param null $prefix |
||
| 500 | * @return array |
||
| 501 | */ |
||
| 502 | public function getAssignmentFieldSlugs($prefix = null) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Get all assignments of the |
||
| 511 | * provided field type namespace. |
||
| 512 | * |
||
| 513 | * @param $fieldType |
||
| 514 | * @return AssignmentCollection |
||
| 515 | */ |
||
| 516 | public function getAssignmentsByFieldType($fieldType) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Get an assignment by field slug. |
||
| 525 | * |
||
| 526 | * @param $fieldSlug |
||
| 527 | * @return AssignmentInterface |
||
| 528 | */ |
||
| 529 | public function getAssignment($fieldSlug) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Return translated assignments. |
||
| 538 | * |
||
| 539 | * @return AssignmentCollection |
||
| 540 | */ |
||
| 541 | public function getTranslatableAssignments() |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Return relation assignments. |
||
| 551 | * |
||
| 552 | * @return AssignmentCollection |
||
| 553 | */ |
||
| 554 | public function getRelationshipAssignments() |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Get the translatable flag. |
||
| 564 | * |
||
| 565 | * @return bool |
||
| 566 | */ |
||
| 567 | public function isTranslatable() |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Return whether the entry is trashable or not. |
||
| 576 | * |
||
| 577 | * @return bool |
||
| 578 | */ |
||
| 579 | public function isTrashable() |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Return the last modified datetime. |
||
| 588 | * |
||
| 589 | * @return Carbon |
||
| 590 | */ |
||
| 591 | public function lastModified() |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Return whether the title column is |
||
| 598 | * translatable or not. |
||
| 599 | * |
||
| 600 | * @return bool |
||
| 601 | */ |
||
| 602 | public function titleColumnIsTranslatable() |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Return whether or not the assignment for |
||
| 609 | * the given field slug is translatable. |
||
| 610 | * |
||
| 611 | * @param $fieldSlug |
||
| 612 | * @return bool |
||
| 613 | */ |
||
| 614 | public function assignmentIsTranslatable($fieldSlug) |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Return whether or not the assignment for |
||
| 621 | * the given field slug is a relationship. |
||
| 622 | * |
||
| 623 | * @param $fieldSlug |
||
| 624 | * @return bool |
||
| 625 | */ |
||
| 626 | public function assignmentIsRelationship($fieldSlug) |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Fire field type events. |
||
| 635 | * |
||
| 636 | * @param $trigger |
||
| 637 | * @param array $payload |
||
| 638 | */ |
||
| 639 | public function fireFieldTypeEvents($trigger, $payload = []) |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Return the related stream. |
||
| 661 | * |
||
| 662 | * @return StreamInterface|array |
||
| 663 | */ |
||
| 664 | public function stream() |
||
| 672 | |||
| 673 | /** |
||
| 674 | * @param array $items |
||
| 675 | * @return EntryCollection |
||
| 676 | */ |
||
| 677 | public function newCollection(array $items = []) |
||
| 678 | { |
||
| 679 | $collection = substr(get_class($this), 0, -5) . 'Collection'; |
||
| 680 | |||
| 681 | if (class_exists($collection)) { |
||
| 682 | return new $collection($items); |
||
| 683 | } |
||
| 684 | |||
| 685 | return new EntryCollection($items); |
||
| 686 | } |
||
| 687 | |||
| 688 | /** |
||
| 689 | * Return the entry presenter. |
||
| 690 | * |
||
| 691 | * This is against standards but required |
||
| 692 | * by the presentable interface. |
||
| 693 | * |
||
| 694 | * @return EntryPresenter |
||
| 695 | */ |
||
| 696 | View Code Duplication | public function getPresenter() |
|
| 706 | |||
| 707 | /** |
||
| 708 | * Return a new presenter instance. |
||
| 709 | * |
||
| 710 | * @return EntryPresenter |
||
| 711 | */ |
||
| 712 | public function newPresenter() |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Get a new query builder for the model's table. |
||
| 719 | * |
||
| 720 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 721 | */ |
||
| 722 | View Code Duplication | public function newQuery() |
|
| 733 | |||
| 734 | /** |
||
| 735 | * Get the criteria class. |
||
| 736 | * |
||
| 737 | * @return string |
||
| 738 | */ |
||
| 739 | public function getCriteriaName() |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Override the __get method. |
||
| 748 | * |
||
| 749 | * @param string $key |
||
| 750 | * @return EntryPresenter|mixed |
||
| 751 | */ |
||
| 752 | public function __get($key) |
||
| 760 | } |
||
| 761 |
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.