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 BelongsToMany 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 BelongsToMany, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class BelongsToMany extends Relationship |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * The intermediate table for the relation. |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $table; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The foreign key of the parent model. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $foreignKey; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The associated key of the relation. |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $otherKey; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The "name" of the relationship. |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $relationName; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The pivot table columns to retrieve. |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $pivotColumns = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * This relationship has pivot attributes |
||
| 52 | * |
||
| 53 | * @var boolean |
||
| 54 | */ |
||
| 55 | protected static $hasPivot = true; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Create a new has many relationship instance. |
||
| 59 | * |
||
| 60 | * @param Mapper $mapper |
||
| 61 | * @param Mappable $parent |
||
| 62 | * @param string $table |
||
| 63 | * @param string $foreignKey |
||
| 64 | * @param string $otherKey |
||
| 65 | * @param string $relationName |
||
| 66 | */ |
||
| 67 | public function __construct(Mapper $mapper, $parent, $table, $foreignKey, $otherKey, $relationName = null) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param $related |
||
| 79 | */ |
||
| 80 | public function detachMany($related) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param array $hashes |
||
| 89 | * @return array |
||
| 90 | */ |
||
| 91 | protected function getIdsFromHashes(array $hashes) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Get the results of the relationship. |
||
| 104 | * |
||
| 105 | * @param $relation |
||
| 106 | * |
||
| 107 | * @return EntityCollection |
||
| 108 | */ |
||
| 109 | public function getResults($relation) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Set a where clause for a pivot table column. |
||
| 120 | * |
||
| 121 | * @param string $column |
||
| 122 | * @param string $operator |
||
| 123 | * @param mixed $value |
||
| 124 | * @param string $boolean |
||
| 125 | * @return self |
||
| 126 | */ |
||
| 127 | public function wherePivot($column, $operator = null, $value = null, $boolean = 'and') |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Set an or where clause for a pivot table column. |
||
| 134 | * |
||
| 135 | * @param string $column |
||
| 136 | * @param string $operator |
||
| 137 | * @param mixed $value |
||
| 138 | * @return self |
||
| 139 | */ |
||
| 140 | public function orWherePivot($column, $operator = null, $value = null) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Return Pivot attributes when available on a relationship |
||
| 147 | * |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | public function getPivotAttributes() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Execute the query and get the first result. |
||
| 157 | * |
||
| 158 | * @param array $columns |
||
| 159 | * @return mixed |
||
| 160 | */ |
||
| 161 | public function first($columns = ['*']) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Execute the query and get the first result or throw an exception. |
||
| 170 | * |
||
| 171 | * @param array $columns |
||
| 172 | * |
||
| 173 | * @throws EntityNotFoundException |
||
| 174 | * |
||
| 175 | * @return Mappable|self |
||
| 176 | */ |
||
| 177 | public function firstOrFail($columns = ['*']) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Execute the query as a "select" statement. |
||
| 188 | * |
||
| 189 | * @param array $columns |
||
| 190 | * @return \Analogue\ORM\EntityCollection |
||
| 191 | */ |
||
| 192 | public function get($columns = ['*']) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Hydrate the pivot table relationship on the models. |
||
| 210 | * |
||
| 211 | * @param array $entities |
||
| 212 | * @return void |
||
| 213 | */ |
||
| 214 | protected function hydratePivotRelation(array $entities) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Get the pivot attributes from a model. |
||
| 231 | * |
||
| 232 | * @param $entity |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | protected function cleanPivotAttributes(InternallyMappable $entity) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Set the base constraints on the relation query. |
||
| 260 | * |
||
| 261 | * @return void |
||
| 262 | */ |
||
| 263 | public function addConstraints() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Add the constraints for a relationship count query. |
||
| 274 | * |
||
| 275 | * @param Query $query |
||
| 276 | * @param Query $parent |
||
| 277 | * @return Query |
||
| 278 | */ |
||
| 279 | public function getRelationCountQuery(Query $query, Query $parent) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Add the constraints for a relationship count query on the same table. |
||
| 292 | * |
||
| 293 | * @param Query $query |
||
| 294 | * @param Query $parent |
||
| 295 | * @return Query |
||
| 296 | */ |
||
| 297 | public function getRelationCountQueryForSelfJoin(Query $query, Query $parent) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Get a relationship join table hash. |
||
| 312 | * |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | public function getRelationCountHash() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Set the select clause for the relation query. |
||
| 322 | * |
||
| 323 | * @param array $columns |
||
| 324 | * @return \Analogue\ORM\Relationships\BelongsToMany |
||
| 325 | */ |
||
| 326 | protected function getSelectColumns(array $columns = ['*']) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Get the pivot columns for the relation. |
||
| 337 | * |
||
| 338 | * @return array |
||
| 339 | */ |
||
| 340 | protected function getAliasedPivotColumns() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Set the join clause for the relation query. |
||
| 358 | * |
||
| 359 | * @param \Analogue\ORM\Query|null |
||
| 360 | * @return $this |
||
| 361 | */ |
||
| 362 | protected function setJoin($query = null) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Set the where clause for the relation query. |
||
| 380 | * |
||
| 381 | * @return $this |
||
| 382 | */ |
||
| 383 | protected function setWhere() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Set the constraints for an eager load of the relation. |
||
| 396 | * |
||
| 397 | * @param array $results |
||
| 398 | * @return void |
||
| 399 | */ |
||
| 400 | public function addEagerConstraints(array $results) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Match Eagerly loaded relation to result |
||
| 407 | * |
||
| 408 | * @param array $results |
||
| 409 | * @param string $relation |
||
| 410 | * @return array |
||
| 411 | */ |
||
| 412 | View Code Duplication | public function match(array $results, $relation) |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Build model dictionary keyed by the relation's foreign key. |
||
| 448 | * |
||
| 449 | * @param EntityCollection $results |
||
| 450 | * @return array |
||
| 451 | */ |
||
| 452 | protected function buildDictionary(EntityCollection $results) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Get all of the IDs for the related models. |
||
| 471 | * |
||
| 472 | * @return array |
||
| 473 | */ |
||
| 474 | public function getRelatedIds() |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Update Pivot |
||
| 483 | * |
||
| 484 | * @param \Analogue\ORM\Entity $entity |
||
| 485 | * @return void |
||
| 486 | */ |
||
| 487 | public function updatePivot($entity) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Update Multiple pivot |
||
| 499 | * |
||
| 500 | * @param $relatedEntities |
||
| 501 | * @return void |
||
| 502 | */ |
||
| 503 | public function updatePivots($relatedEntities) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Create Pivot Records |
||
| 512 | * |
||
| 513 | * @param \Analogue\ORM\Entity[] $relatedEntities |
||
| 514 | * @return void |
||
| 515 | */ |
||
| 516 | public function createPivots($relatedEntities) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Update an existing pivot record on the table. |
||
| 534 | * |
||
| 535 | * @param mixed $id |
||
| 536 | * @param array $attributes |
||
| 537 | * @throws \InvalidArgumentException |
||
| 538 | * @return integer |
||
| 539 | */ |
||
| 540 | public function updateExistingPivot($id, array $attributes) |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Attach a model to the parent. |
||
| 551 | * |
||
| 552 | * @param mixed $id |
||
| 553 | * @param array $attributes |
||
| 554 | * @return void |
||
| 555 | */ |
||
| 556 | public function attach($id, array $attributes = []) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @param array $entities |
||
| 565 | * |
||
| 566 | * @throws \InvalidArgumentException |
||
| 567 | */ |
||
| 568 | public function sync(array $entities) |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Detach related entities that are not in $id |
||
| 575 | * |
||
| 576 | * @param array $entities |
||
| 577 | * |
||
| 578 | * @throws \InvalidArgumentException |
||
| 579 | * |
||
| 580 | * @return void |
||
| 581 | */ |
||
| 582 | protected function detachExcept(array $entities = []) |
||
| 599 | |||
| 600 | |||
| 601 | /** |
||
| 602 | * Create an array of records to insert into the pivot table. |
||
| 603 | * |
||
| 604 | * @param array $ids |
||
| 605 | * @param array $attributes |
||
| 606 | * @return array |
||
| 607 | */ |
||
| 608 | protected function createAttachRecords($ids, array $attributes) |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Create a full attachment record payload. |
||
| 626 | * |
||
| 627 | * @param int $key |
||
| 628 | * @param mixed $value |
||
| 629 | * @param array $attributes |
||
| 630 | * @param bool $timed |
||
| 631 | * @return array |
||
| 632 | */ |
||
| 633 | protected function attacher($key, $value, $attributes, $timed) |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Get the attach record ID and extra attributes. |
||
| 647 | * |
||
| 648 | * @param int $key |
||
| 649 | * @param mixed $value |
||
| 650 | * @param array $attributes |
||
| 651 | * @return array |
||
| 652 | */ |
||
| 653 | protected function getAttachId($key, $value, array $attributes) |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Create a new pivot attachment record. |
||
| 664 | * |
||
| 665 | * @param int $id |
||
| 666 | * @param bool $timed |
||
| 667 | * @return array |
||
| 668 | */ |
||
| 669 | protected function createAttachRecord($id, $timed) |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Set the creation and update timestamps on an attach record. |
||
| 691 | * |
||
| 692 | * @param array $record |
||
| 693 | * @param bool $exists |
||
| 694 | * @return array |
||
| 695 | */ |
||
| 696 | protected function setTimestampsOnAttach(array $record, $exists = false) |
||
| 708 | |||
| 709 | /** |
||
| 710 | * @param EntityCollection $entities |
||
| 711 | * @return array |
||
| 712 | */ |
||
| 713 | protected function getModelKeysFromCollection(EntityCollection $entities) |
||
| 721 | |||
| 722 | /** |
||
| 723 | * Detach models from the relationship. |
||
| 724 | * |
||
| 725 | * @param int|array $ids |
||
| 726 | * @throws \InvalidArgumentException |
||
| 727 | * @return int |
||
| 728 | */ |
||
| 729 | public function detach($ids = []) |
||
| 751 | |||
| 752 | /** |
||
| 753 | * Create a new query builder for the pivot table. |
||
| 754 | * |
||
| 755 | * @throws \InvalidArgumentException |
||
| 756 | * |
||
| 757 | * @return \Illuminate\Database\Query\Builder |
||
| 758 | */ |
||
| 759 | protected function newPivotQuery() |
||
| 767 | |||
| 768 | /** |
||
| 769 | * Get a new plain query builder for the pivot table. |
||
| 770 | * |
||
| 771 | * @return \Illuminate\Database\Query\Builder |
||
| 772 | */ |
||
| 773 | public function newPivotStatement() |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Get a new pivot statement for a given "other" ID. |
||
| 780 | * |
||
| 781 | * @param mixed $id |
||
| 782 | * |
||
| 783 | * @throws \InvalidArgumentException |
||
| 784 | * |
||
| 785 | * @return \Illuminate\Database\Query\Builder |
||
| 786 | */ |
||
| 787 | public function newPivotStatementForId($id) |
||
| 797 | |||
| 798 | /** |
||
| 799 | * Create a new pivot model instance. |
||
| 800 | * |
||
| 801 | * @param array $attributes |
||
| 802 | * @param bool $exists |
||
| 803 | * @return \Analogue\ORM\Relationships\Pivot |
||
| 804 | */ |
||
| 805 | public function newPivot(array $attributes = [], $exists = false) |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Create a new existing pivot model instance. |
||
| 814 | * |
||
| 815 | * @param array $attributes |
||
| 816 | * @return \Analogue\ORM\Relationships\Pivot |
||
| 817 | */ |
||
| 818 | public function newExistingPivot(array $attributes = []) |
||
| 822 | |||
| 823 | /** |
||
| 824 | * Set the columns on the pivot table to retrieve. |
||
| 825 | * |
||
| 826 | * @param array $columns |
||
| 827 | * @return $this |
||
| 828 | */ |
||
| 829 | public function withPivot($columns) |
||
| 837 | |||
| 838 | /** |
||
| 839 | * Specify that the pivot table has creation and update timestamps. |
||
| 840 | * |
||
| 841 | * @param mixed $createdAt |
||
| 842 | * @param mixed $updatedAt |
||
| 843 | * @return \Analogue\ORM\Relationships\BelongsToMany |
||
| 844 | */ |
||
| 845 | public function withTimestamps($createdAt = null, $updatedAt = null) |
||
| 849 | |||
| 850 | /** |
||
| 851 | * Get the key for comparing against the parent key in "has" query. |
||
| 852 | * |
||
| 853 | * @return string |
||
| 854 | */ |
||
| 855 | public function getHasCompareKey() |
||
| 859 | |||
| 860 | /** |
||
| 861 | * Get the fully qualified foreign key for the relation. |
||
| 862 | * |
||
| 863 | * @return string |
||
| 864 | */ |
||
| 865 | public function getForeignKey() |
||
| 869 | |||
| 870 | /** |
||
| 871 | * Get the fully qualified "other key" for the relation. |
||
| 872 | * |
||
| 873 | * @return string |
||
| 874 | */ |
||
| 875 | public function getOtherKey() |
||
| 879 | |||
| 880 | /** |
||
| 881 | * Get the fully qualified parent key name. |
||
| 882 | * |
||
| 883 | * @return string |
||
| 884 | */ |
||
| 885 | protected function getQualifiedParentKeyName() |
||
| 889 | |||
| 890 | /** |
||
| 891 | * Get the intermediate table for the relationship. |
||
| 892 | * |
||
| 893 | * @return string |
||
| 894 | */ |
||
| 895 | public function getTable() |
||
| 899 | |||
| 900 | /** |
||
| 901 | * Get the relationship name for the relationship. |
||
| 902 | * |
||
| 903 | * @return string |
||
| 904 | */ |
||
| 905 | public function getRelationName() |
||
| 909 | } |
||
| 910 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: