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 |
||
10 | View Code Duplication | trait NotifableLaravel53 |
|
|
|||
11 | { |
||
12 | use NotifableBasic; |
||
13 | |||
14 | /** |
||
15 | * Define a polymorphic one-to-many relationship. |
||
16 | * |
||
17 | * @param string $related |
||
18 | * @param string $name |
||
19 | * @param string $type |
||
20 | * @param string $id |
||
21 | * @param string $localKey |
||
22 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany |
||
23 | */ |
||
24 | abstract public function morphMany($related, $name, $type = null, $id = null, $localKey = null); |
||
25 | |||
26 | /** |
||
27 | * Define a one-to-many relationship. |
||
28 | * |
||
29 | * @param string $related |
||
30 | * @param string $foreignKey |
||
31 | * @param string $localKey |
||
32 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
33 | */ |
||
34 | abstract public function hasMany($related, $foreignKey = null, $localKey = null); |
||
35 | |||
36 | /** |
||
37 | * Get the notifications Relationship. |
||
38 | * |
||
39 | * @return \Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\MorphMany |
||
40 | */ |
||
41 | public function notifynderNotifications() |
||
50 | |||
51 | /** |
||
52 | * Get the notifications Relationship without any eager loading. |
||
53 | * |
||
54 | * @return \Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\MorphMany |
||
55 | */ |
||
56 | public function getLazyNotificationRelation() |
||
60 | } |
||
61 |
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.