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 | */ |
||
| 202 | public function setFieldValue($fieldSlug, $value) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Get an entry field. |
||
| 218 | * |
||
| 219 | * @param $slug |
||
| 220 | * @return FieldInterface|null |
||
| 221 | */ |
||
| 222 | public function getField($slug) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Return whether an entry has |
||
| 235 | * a field with a given slug. |
||
| 236 | * |
||
| 237 | * @param $slug |
||
| 238 | * @return bool |
||
| 239 | */ |
||
| 240 | public function hasField($slug) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Get the field type from a field slug. |
||
| 247 | * |
||
| 248 | * @param $fieldSlug |
||
| 249 | * @return null|FieldType |
||
| 250 | */ |
||
| 251 | View Code Duplication | public function getFieldType($fieldSlug) |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Get the field type query. |
||
| 282 | * |
||
| 283 | * @param $fieldSlug |
||
| 284 | * @return FieldTypeQuery |
||
| 285 | */ |
||
| 286 | public function getFieldTypeQuery($fieldSlug) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Get the field type presenter. |
||
| 297 | * |
||
| 298 | * @param $fieldSlug |
||
| 299 | * @return FieldTypePresenter |
||
| 300 | */ |
||
| 301 | public function getFieldTypePresenter($fieldSlug) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Set a given attribute on the model. |
||
| 312 | * Override the behavior here to give |
||
| 313 | * the field types a chance to modify things. |
||
| 314 | * |
||
| 315 | * @param string $key |
||
| 316 | * @param mixed $value |
||
| 317 | * @return $this |
||
| 318 | */ |
||
| 319 | public function setAttribute($key, $value) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Get a given attribute on the model. |
||
| 332 | * Override the behavior here to give |
||
| 333 | * the field types a chance to modify things. |
||
| 334 | * |
||
| 335 | * @param string $key |
||
| 336 | * @return mixed |
||
| 337 | */ |
||
| 338 | public function getAttribute($key) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Get a raw unmodified attribute. |
||
| 357 | * |
||
| 358 | * @param $key |
||
| 359 | * @param bool $process |
||
| 360 | * @return mixed|null |
||
| 361 | */ |
||
| 362 | public function getRawAttribute($key, $process = true) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Set a raw unmodified attribute. |
||
| 373 | * |
||
| 374 | * @param $key |
||
| 375 | * @param $value |
||
| 376 | * @return $this |
||
| 377 | */ |
||
| 378 | public function setRawAttribute($key, $value) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Get the stream. |
||
| 387 | * |
||
| 388 | * @return StreamInterface |
||
| 389 | */ |
||
| 390 | public function getStream() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Get the stream namespace. |
||
| 397 | * |
||
| 398 | * @return string |
||
| 399 | */ |
||
| 400 | public function getStreamNamespace() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Get the stream slug. |
||
| 409 | * |
||
| 410 | * @return string |
||
| 411 | */ |
||
| 412 | public function getStreamSlug() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Get the entry's stream name. |
||
| 421 | * |
||
| 422 | * @return string |
||
| 423 | */ |
||
| 424 | public function getStreamName() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Get the stream prefix. |
||
| 433 | * |
||
| 434 | * @return string |
||
| 435 | */ |
||
| 436 | public function getStreamPrefix() |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Get the table name. |
||
| 445 | * |
||
| 446 | * @return string |
||
| 447 | */ |
||
| 448 | public function getTableName() |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Get the translations table name. |
||
| 457 | * |
||
| 458 | * @return string |
||
| 459 | */ |
||
| 460 | public function getTranslationsTableName() |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Get all assignments. |
||
| 469 | * |
||
| 470 | * @return AssignmentCollection |
||
| 471 | */ |
||
| 472 | public function getAssignments() |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Get the field slugs for assigned fields. |
||
| 481 | * |
||
| 482 | * @param null $prefix |
||
| 483 | * @return array |
||
| 484 | */ |
||
| 485 | public function getAssignmentFieldSlugs($prefix = null) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Get all assignments of the |
||
| 494 | * provided field type namespace. |
||
| 495 | * |
||
| 496 | * @param $fieldType |
||
| 497 | * @return AssignmentCollection |
||
| 498 | */ |
||
| 499 | public function getAssignmentsByFieldType($fieldType) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Get an assignment by field slug. |
||
| 508 | * |
||
| 509 | * @param $fieldSlug |
||
| 510 | * @return AssignmentInterface |
||
| 511 | */ |
||
| 512 | public function getAssignment($fieldSlug) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Return translated assignments. |
||
| 521 | * |
||
| 522 | * @return AssignmentCollection |
||
| 523 | */ |
||
| 524 | public function getTranslatableAssignments() |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Return relation assignments. |
||
| 534 | * |
||
| 535 | * @return AssignmentCollection |
||
| 536 | */ |
||
| 537 | public function getRelationshipAssignments() |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Get the translatable flag. |
||
| 547 | * |
||
| 548 | * @return bool |
||
| 549 | */ |
||
| 550 | public function isTranslatable() |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Return whether the entry is trashable or not. |
||
| 559 | * |
||
| 560 | * @return bool |
||
| 561 | */ |
||
| 562 | public function isTrashable() |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Return the last modified datetime. |
||
| 571 | * |
||
| 572 | * @return Carbon |
||
| 573 | */ |
||
| 574 | public function lastModified() |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Return whether the title column is |
||
| 581 | * translatable or not. |
||
| 582 | * |
||
| 583 | * @return bool |
||
| 584 | */ |
||
| 585 | public function titleColumnIsTranslatable() |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Return whether or not the assignment for |
||
| 592 | * the given field slug is translatable. |
||
| 593 | * |
||
| 594 | * @param $fieldSlug |
||
| 595 | * @return bool |
||
| 596 | */ |
||
| 597 | public function assignmentIsTranslatable($fieldSlug) |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Return whether or not the assignment for |
||
| 604 | * the given field slug is a relationship. |
||
| 605 | * |
||
| 606 | * @param $fieldSlug |
||
| 607 | * @return bool |
||
| 608 | */ |
||
| 609 | public function assignmentIsRelationship($fieldSlug) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Fire field type events. |
||
| 618 | * |
||
| 619 | * @param $trigger |
||
| 620 | * @param array $payload |
||
| 621 | */ |
||
| 622 | public function fireFieldTypeEvents($trigger, $payload = []) |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Return the related stream. |
||
| 644 | * |
||
| 645 | * @return StreamInterface|array |
||
| 646 | */ |
||
| 647 | public function stream() |
||
| 655 | |||
| 656 | /** |
||
| 657 | * @param array $items |
||
| 658 | * @return EntryCollection |
||
| 659 | */ |
||
| 660 | View Code Duplication | public function newCollection(array $items = []) |
|
| 661 | { |
||
| 662 | $collection = substr(get_class($this), 0, -5) . 'Collection'; |
||
| 663 | |||
| 664 | if (class_exists($collection)) { |
||
| 665 | return new $collection($items); |
||
| 666 | } |
||
| 667 | |||
| 668 | return new EntryCollection($items); |
||
| 669 | } |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Return the entry presenter. |
||
| 673 | * |
||
| 674 | * This is against standards but required |
||
| 675 | * by the presentable interface. |
||
| 676 | * |
||
| 677 | * @return EntryPresenter |
||
| 678 | */ |
||
| 679 | View Code Duplication | public function getPresenter() |
|
| 689 | |||
| 690 | /** |
||
| 691 | * Return a new presenter instance. |
||
| 692 | * |
||
| 693 | * @return EntryPresenter |
||
| 694 | */ |
||
| 695 | public function newPresenter() |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Get a new query builder for the model's table. |
||
| 702 | * |
||
| 703 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 704 | */ |
||
| 705 | View Code Duplication | public function newQuery() |
|
| 716 | |||
| 717 | /** |
||
| 718 | * Override the __get method. |
||
| 719 | * |
||
| 720 | * @param string $key |
||
| 721 | * @return EntryPresenter|mixed |
||
| 722 | */ |
||
| 723 | public function __get($key) |
||
| 731 | } |
||
| 732 |
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.