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 validation rules. These are |
||
| 30 | * overridden on the compiled models. |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $rules = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The field slugs. These are |
||
| 38 | * overridden on compiled models. |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $fields = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The entry relationships by field slug. |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $relationships = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The compiled stream data. |
||
| 53 | * |
||
| 54 | * @var array|StreamInterface |
||
| 55 | */ |
||
| 56 | protected $stream = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Boot the model |
||
| 60 | */ |
||
| 61 | protected static function boot() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Sort the query. |
||
| 82 | * |
||
| 83 | * @param Builder $builder |
||
| 84 | * @param string $direction |
||
| 85 | */ |
||
| 86 | public function scopeSorted(Builder $builder, $direction = 'asc') |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Get the ID. |
||
| 93 | * |
||
| 94 | * @return mixed |
||
| 95 | */ |
||
| 96 | public function getId() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get the entry ID. |
||
| 103 | * |
||
| 104 | * @return mixed |
||
| 105 | */ |
||
| 106 | public function getEntryId() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Get the entry title. |
||
| 113 | * |
||
| 114 | * @return mixed |
||
| 115 | */ |
||
| 116 | public function getEntryTitle() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Get the sort order. |
||
| 123 | * |
||
| 124 | * @return int |
||
| 125 | */ |
||
| 126 | public function getSortOrder() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Get the entries title. |
||
| 133 | * |
||
| 134 | * @return mixed |
||
| 135 | */ |
||
| 136 | public function getTitle() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get a field value. |
||
| 143 | * |
||
| 144 | * @param $fieldSlug |
||
| 145 | * @param null $locale |
||
| 146 | * @return mixed |
||
| 147 | */ |
||
| 148 | public function getFieldValue($fieldSlug, $locale = null) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Set a field value. |
||
| 191 | * |
||
| 192 | * @param $fieldSlug |
||
| 193 | * @param $value |
||
| 194 | */ |
||
| 195 | public function setFieldValue($fieldSlug, $value) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Get an entry field. |
||
| 211 | * |
||
| 212 | * @param $slug |
||
| 213 | * @return FieldInterface|null |
||
| 214 | */ |
||
| 215 | public function getField($slug) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Return whether an entry has |
||
| 228 | * a field with a given slug. |
||
| 229 | * |
||
| 230 | * @param $slug |
||
| 231 | * @return bool |
||
| 232 | */ |
||
| 233 | public function hasField($slug) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get the field type from a field slug. |
||
| 240 | * |
||
| 241 | * @param $fieldSlug |
||
| 242 | * @return null|FieldType |
||
| 243 | */ |
||
| 244 | View Code Duplication | public function getFieldType($fieldSlug) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Get the field type query. |
||
| 275 | * |
||
| 276 | * @param $fieldSlug |
||
| 277 | * @return FieldTypeQuery |
||
| 278 | */ |
||
| 279 | public function getFieldTypeQuery($fieldSlug) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Get the field type presenter. |
||
| 290 | * |
||
| 291 | * @param $fieldSlug |
||
| 292 | * @return FieldTypePresenter |
||
| 293 | */ |
||
| 294 | public function getFieldTypePresenter($fieldSlug) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Set a given attribute on the model. |
||
| 305 | * Override the behavior here to give |
||
| 306 | * the field types a chance to modify things. |
||
| 307 | * |
||
| 308 | * @param string $key |
||
| 309 | * @param mixed $value |
||
| 310 | * @return $this |
||
| 311 | */ |
||
| 312 | public function setAttribute($key, $value) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Get a given attribute on the model. |
||
| 325 | * Override the behavior here to give |
||
| 326 | * the field types a chance to modify things. |
||
| 327 | * |
||
| 328 | * @param string $key |
||
| 329 | * @return mixed |
||
| 330 | */ |
||
| 331 | public function getAttribute($key) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Get a raw unmodified attribute. |
||
| 350 | * |
||
| 351 | * @param $key |
||
| 352 | * @param bool $process |
||
| 353 | * @return mixed|null |
||
| 354 | */ |
||
| 355 | public function getRawAttribute($key, $process = true) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Set a raw unmodified attribute. |
||
| 366 | * |
||
| 367 | * @param $key |
||
| 368 | * @param $value |
||
| 369 | * @return $this |
||
| 370 | */ |
||
| 371 | public function setRawAttribute($key, $value) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Get the stream. |
||
| 380 | * |
||
| 381 | * @return StreamInterface |
||
| 382 | */ |
||
| 383 | public function getStream() |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Get the stream namespace. |
||
| 390 | * |
||
| 391 | * @return string |
||
| 392 | */ |
||
| 393 | public function getStreamNamespace() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Get the stream slug. |
||
| 402 | * |
||
| 403 | * @return string |
||
| 404 | */ |
||
| 405 | public function getStreamSlug() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Get the entry's stream name. |
||
| 414 | * |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | public function getStreamName() |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Get the stream prefix. |
||
| 426 | * |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | public function getStreamPrefix() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Get the table name. |
||
| 438 | * |
||
| 439 | * @return string |
||
| 440 | */ |
||
| 441 | public function getTableName() |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Get the translations table name. |
||
| 450 | * |
||
| 451 | * @return string |
||
| 452 | */ |
||
| 453 | public function getTranslationsTableName() |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Get all assignments. |
||
| 462 | * |
||
| 463 | * @return AssignmentCollection |
||
| 464 | */ |
||
| 465 | public function getAssignments() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Get the field slugs for assigned fields. |
||
| 474 | * |
||
| 475 | * @param null $prefix |
||
| 476 | * @return array |
||
| 477 | */ |
||
| 478 | public function getAssignmentFieldSlugs($prefix = null) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Get all assignments of the |
||
| 487 | * provided field type namespace. |
||
| 488 | * |
||
| 489 | * @param $fieldType |
||
| 490 | * @return AssignmentCollection |
||
| 491 | */ |
||
| 492 | public function getAssignmentsByFieldType($fieldType) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Get an assignment by field slug. |
||
| 501 | * |
||
| 502 | * @param $fieldSlug |
||
| 503 | * @return AssignmentInterface |
||
| 504 | */ |
||
| 505 | public function getAssignment($fieldSlug) |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Return translated assignments. |
||
| 514 | * |
||
| 515 | * @return AssignmentCollection |
||
| 516 | */ |
||
| 517 | public function getTranslatableAssignments() |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Return relation assignments. |
||
| 527 | * |
||
| 528 | * @return AssignmentCollection |
||
| 529 | */ |
||
| 530 | public function getRelationshipAssignments() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Get the translatable flag. |
||
| 540 | * |
||
| 541 | * @return bool |
||
| 542 | */ |
||
| 543 | public function isTranslatable() |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Return whether the entry is trashable or not. |
||
| 552 | * |
||
| 553 | * @return bool |
||
| 554 | */ |
||
| 555 | public function isTrashable() |
||
| 556 | { |
||
| 557 | $stream = $this->getStream(); |
||
| 558 | |||
| 559 | return $stream->isTrashable(); |
||
| 560 | } |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Return the last modified datetime. |
||
| 564 | * |
||
| 565 | * @return Carbon |
||
| 566 | */ |
||
| 567 | public function lastModified() |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Return whether the title column is |
||
| 574 | * translatable or not. |
||
| 575 | * |
||
| 576 | * @return bool |
||
| 577 | */ |
||
| 578 | public function titleColumnIsTranslatable() |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Return whether or not the assignment for |
||
| 585 | * the given field slug is translatable. |
||
| 586 | * |
||
| 587 | * @param $fieldSlug |
||
| 588 | * @return bool |
||
| 589 | */ |
||
| 590 | public function assignmentIsTranslatable($fieldSlug) |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Return whether or not the assignment for |
||
| 597 | * the given field slug is a relationship. |
||
| 598 | * |
||
| 599 | * @param $fieldSlug |
||
| 600 | * @return bool |
||
| 601 | */ |
||
| 602 | public function assignmentIsRelationship($fieldSlug) |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Fire field type events. |
||
| 611 | * |
||
| 612 | * @param $trigger |
||
| 613 | * @param array $payload |
||
| 614 | */ |
||
| 615 | public function fireFieldTypeEvents($trigger, $payload = []) |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Return the related stream. |
||
| 637 | * |
||
| 638 | * @return StreamInterface|array |
||
| 639 | */ |
||
| 640 | public function stream() |
||
| 648 | |||
| 649 | /** |
||
| 650 | * @param array $items |
||
| 651 | * @return EntryCollection |
||
| 652 | */ |
||
| 653 | public function newCollection(array $items = []) |
||
| 654 | { |
||
| 655 | $collection = substr(get_class($this), 0, -5) . 'Collection'; |
||
| 656 | |||
| 657 | if (class_exists($collection)) { |
||
| 658 | return new $collection($items); |
||
| 659 | } |
||
| 660 | |||
| 661 | return new EntryCollection($items); |
||
| 662 | } |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Return the entry presenter. |
||
| 666 | * |
||
| 667 | * This is against standards but required |
||
| 668 | * by the presentable interface. |
||
| 669 | * |
||
| 670 | * @return EntryPresenter |
||
| 671 | */ |
||
| 672 | View Code Duplication | public function getPresenter() |
|
| 682 | |||
| 683 | /** |
||
| 684 | * Return a new presenter instance. |
||
| 685 | * |
||
| 686 | * @return EntryPresenter |
||
| 687 | */ |
||
| 688 | public function newPresenter() |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Get a new query builder for the model's table. |
||
| 695 | * |
||
| 696 | * @return \Illuminate\Database\Eloquent\Builder |
||
| 697 | */ |
||
| 698 | View Code Duplication | public function newQuery() |
|
| 709 | |||
| 710 | /** |
||
| 711 | * Override the __get method. |
||
| 712 | * |
||
| 713 | * @param string $key |
||
| 714 | * @return EntryPresenter|mixed |
||
| 715 | */ |
||
| 716 | public function __get($key) |
||
| 724 | } |
||
| 725 |
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.