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 |
||
8 | class RelationManager |
||
9 | { |
||
10 | /** |
||
11 | * @var RelationStore |
||
12 | */ |
||
13 | protected $store; |
||
14 | |||
15 | /** |
||
16 | * Create a new instance |
||
17 | */ |
||
18 | 9 | public function __construct(RelationStore $store) |
|
22 | |||
23 | /** |
||
24 | * @return RelationStore |
||
25 | */ |
||
26 | public function getStore() |
||
30 | |||
31 | /** |
||
32 | * Retrieve all relations |
||
33 | * |
||
34 | * @return array |
||
35 | */ |
||
36 | 1 | public function getDynamicRelations(Model $model) |
|
40 | |||
41 | /** |
||
42 | * Has the model the relation |
||
43 | * |
||
44 | * @param string |
||
45 | * |
||
46 | * @return bool |
||
47 | */ |
||
48 | 1 | public function hasDynamicRelation(Model $model, string $relation) |
|
52 | |||
53 | /** |
||
54 | * Remove a relation from the model |
||
55 | * |
||
56 | * @param string $relation |
||
57 | */ |
||
58 | 2 | public function removeRelation(Model $model, string $relation) |
|
66 | |||
67 | /** |
||
68 | * Define a new relation |
||
69 | * |
||
70 | * @param string $relationType |
||
71 | * @param string $relationName |
||
72 | * @param array $data |
||
73 | * @param array $constraints |
||
74 | */ |
||
75 | 8 | public function defineRelation(Model $model, $relationType, $relationName, $data, $constraints) |
|
88 | /** |
||
89 | * Define a polymorphic, inverse many-to-many relationship. |
||
90 | * |
||
91 | * @param string $relationName |
||
92 | * @param string $related |
||
93 | * @param string $name |
||
94 | * @param string $table |
||
95 | * @param string $foreignPivotKey |
||
96 | * @param string $relatedPivotKey |
||
97 | * @param string $parentKey |
||
98 | * @param string $relatedKey |
||
99 | * |
||
100 | * @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
||
101 | */ |
||
102 | 1 | View Code Duplication | public function morphed_by_many( |
116 | |||
117 | /** |
||
118 | * @param string $relationName |
||
119 | * @param string $related |
||
120 | * @param null $foreignKey |
||
121 | * @param null $localKey |
||
122 | * |
||
123 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
124 | */ |
||
125 | 3 | public function has_many(Model $model, $relationName, $related, $foreignKey = null, $localKey = null) |
|
129 | |||
130 | /** |
||
131 | * Define a one-to-one relationship. |
||
132 | * |
||
133 | * @param string $relationName |
||
134 | * @param string $related |
||
135 | * @param string $foreignKey |
||
136 | * @param string $localKey |
||
137 | * |
||
138 | * @return \Illuminate\Database\Eloquent\Relations\HasOne |
||
139 | */ |
||
140 | 1 | public function has_one(Model $model, $relationName, $related, $foreignKey = null, $localKey = null) |
|
144 | |||
145 | /** |
||
146 | * @param string $relationName |
||
147 | * @param string $related |
||
148 | * @param null $foreignKey |
||
149 | * @param null $ownerKey |
||
150 | * |
||
151 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
152 | */ |
||
153 | 1 | public function belongs_to(Model $model, $relationName, $related, $foreignKey = null, $ownerKey = null) |
|
161 | |||
162 | /** |
||
163 | * Define a many-to-many relationship. |
||
164 | * |
||
165 | * @param string $relationName |
||
166 | * @param string $related |
||
167 | * @param string $table |
||
168 | * @param string $foreignPivotKey |
||
169 | * @param string $relatedPivotKey |
||
170 | * @param string $parentKey |
||
171 | * @param string $relatedKey |
||
172 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
173 | */ |
||
174 | 1 | View Code Duplication | public function belongs_to_many( |
188 | |||
189 | /** |
||
190 | * Define a polymorphic many-to-many relationship. |
||
191 | * |
||
192 | * @param string $relationName |
||
193 | * @param string $related |
||
194 | * @param string $name |
||
195 | * @param string $table |
||
196 | * @param string $foreignPivotKey |
||
197 | * @param string $relatedPivotKey |
||
198 | * @param string $parentKey |
||
199 | * @param string $relatedKey |
||
200 | * @param bool $inverse |
||
201 | * @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
||
202 | */ |
||
203 | 1 | public function morph_to_many( |
|
220 | |||
221 | /** |
||
222 | * Define a polymorphic one-to-many relationship. |
||
223 | * |
||
224 | * @param string $relationName |
||
225 | * @param string $related |
||
226 | * @param string $name |
||
227 | * @param string $type |
||
228 | * @param string $id |
||
229 | * @param string $localKey |
||
230 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany |
||
231 | */ |
||
232 | 1 | public function morph_many(Model $model, $relationName, $related, $name, $type = null, $id = null, $localKey = null) |
|
238 | |||
239 | /** |
||
240 | * Define a polymorphic one-to-one relationship. |
||
241 | * |
||
242 | * @param string $relationName |
||
243 | * @param string $related |
||
244 | * @param string $name |
||
245 | * @param string $type |
||
246 | * @param string $id |
||
247 | * @param string $localKey |
||
248 | * @return \Illuminate\Database\Eloquent\Relations\MorphOne |
||
249 | */ |
||
250 | public function morph_one(Model $model, $relationName, $related, $name, $type = null, $id = null, $localKey = null) |
||
256 | |||
257 | /** |
||
258 | * Define a polymorphic, inverse one-to-one or many relationship. |
||
259 | * |
||
260 | * @param string $relationName |
||
261 | * @param string $type |
||
262 | * @param string $id |
||
263 | * @param string $ownerKey |
||
264 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo |
||
265 | */ |
||
266 | 1 | public function morph_to(Model $model, $relationName, $type = null, $id = null, $ownerKey = null) |
|
272 | |||
273 | /** |
||
274 | * Define a has-many-through relationship. |
||
275 | * |
||
276 | * @param string $relationName |
||
277 | * @param string $related |
||
278 | * @param string $through |
||
279 | * @param string|null $firstKey |
||
280 | * @param string|null $secondKey |
||
281 | * @param string|null $localKey |
||
282 | * @param string|null $secondLocalKey |
||
283 | * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
||
284 | */ |
||
285 | 1 | public function has_many_through(Model $model, $relationName, $related, $through, $firstKey = null, $secondKey = null, $localKey = null, $secondLocalKey = null) |
|
291 | } |
||
292 |
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.