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:
| 1 | <?php |
||
| 14 | class MorphToMany extends HasAndBelongsToMany |
||
| 15 | { |
||
| 16 | use HasCollectionResults; |
||
| 17 | |||
| 18 | protected $type = 'morphToMany'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The type of the polymorphic relation. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $morphType = null; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Indicates if we are connecting the inverse of the relation. |
||
| 29 | * |
||
| 30 | * This primarily affects the morphClass constraint. |
||
| 31 | * |
||
| 32 | * @var bool |
||
| 33 | */ |
||
| 34 | protected $inverse = false; |
||
| 35 | |||
| 36 | /** @noinspection PhpMissingParentCallCommonInspection |
||
| 37 | * Builds the name of a has-and-belongs-to-many association table |
||
| 38 | * @return string |
||
| 39 | */ |
||
| 40 | public function generatePivotTable() |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @return \Nip\Records\AbstractModels\RecordManager |
||
| 49 | */ |
||
| 50 | protected function getPivotManager() |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @return bool |
||
| 57 | */ |
||
| 58 | public function isInverse(): bool |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param bool $inverse |
||
| 65 | */ |
||
| 66 | public function setInverse(bool $inverse) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param AbstractQuery $query |
||
| 73 | * @return AbstractQuery |
||
| 74 | */ |
||
| 75 | View Code Duplication | public function populateQuerySpecific(AbstractQuery $query) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * @inheritdoc |
||
| 89 | */ |
||
| 90 | protected function formatAttachData($record) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Get the foreign key "type" name. |
||
| 99 | * |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function getMorphKey() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Get the foreign key "type" name. |
||
| 109 | * |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | public function getMorphType() |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param string $morphType |
||
| 122 | */ |
||
| 123 | public function setMorphType(string $morphType) |
||
| 127 | |||
| 128 | protected function initMorphType() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | protected function generateMorphType() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param DeleteQuery $query |
||
| 143 | * @param $records |
||
| 144 | */ |
||
| 145 | protected function queryDetachRecords($query, $records) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param SelectQuery $query |
||
| 153 | */ |
||
| 154 | protected function hydrateQueryWithPivotConstraints($query) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @return mixed |
||
| 165 | */ |
||
| 166 | protected function getPivotFK() |
||
| 173 | } |
||
| 174 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.