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 | trait DynamicRelations |
||
9 | { |
||
10 | use BaseEloquentOverrides; |
||
11 | |||
12 | protected static $macros = []; |
||
13 | |||
14 | 5 | public static function macro($name, $macro) |
|
18 | |||
19 | /** |
||
20 | * Dynamically handle calls to the class. |
||
21 | * |
||
22 | * @param string $method |
||
23 | * @param array $parameters |
||
24 | * @return mixed |
||
25 | * |
||
26 | * @throws \BadMethodCallException |
||
27 | */ |
||
28 | 5 | public function __call($method, $parameters) |
|
41 | |||
42 | 1 | public static function morphed_by_many($relationName, $related, $name, $table = null, $foreignPivotKey = null, |
|
48 | |||
49 | /** |
||
50 | * @param string $related |
||
51 | * @param $relationName |
||
52 | * @param null $foreignKey |
||
53 | * @param null $localKey |
||
54 | * |
||
55 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
56 | */ |
||
57 | 1 | public static function has_many($relationName = null, string $related, $foreignKey = null, $localKey = null) |
|
61 | |||
62 | 1 | public static function has_one($relationName = null, $related, $foreignKey = null, $localKey = null) |
|
66 | |||
67 | /** |
||
68 | * @param string $related |
||
69 | * @param $relationName |
||
70 | * @param null $foreignKey |
||
71 | * @param null $ownerKey |
||
72 | * |
||
73 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
74 | */ |
||
75 | 1 | public static function belongs_to($relationName = null, string $related, $foreignKey = null, $ownerKey = null) |
|
83 | |||
84 | 1 | View Code Duplication | public static function belongs_to_many($relationName, $related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null, |
91 | |||
92 | /** |
||
93 | * Define a polymorphic many-to-many relationship. |
||
94 | * |
||
95 | * @param string $relationName |
||
96 | * @param string $related |
||
97 | * @param string $name |
||
98 | * @param string $table |
||
99 | * @param string $foreignPivotKey |
||
100 | * @param string $relatedPivotKey |
||
101 | * @param string $parentKey |
||
102 | * @param string $relatedKey |
||
103 | * @param bool $inverse |
||
104 | * |
||
105 | * @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
||
106 | */ |
||
107 | 1 | View Code Duplication | public static function morph_to_many($relationName, $related, $name, $table = null, $foreignPivotKey = null, |
115 | |||
116 | /** |
||
117 | * Define a polymorphic one-to-many relationship. |
||
118 | * |
||
119 | * @param string $relationName |
||
120 | * @param string $related |
||
121 | * @param string $name |
||
122 | * @param string $type |
||
123 | * @param string $id |
||
124 | * @param string $localKey |
||
125 | * |
||
126 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany |
||
127 | */ |
||
128 | 1 | public static function morph_many($relationName, $related, $name, $type = null, $id = null, $localKey = null) |
|
134 | |||
135 | public static function morph_one($relationName, $related, $name, $type = null, $id = null, $localKey = null) |
||
140 | |||
141 | public static function morph_to($relationName, $type = null, $id = null, $ownerKey = null) |
||
147 | |||
148 | public static function forceEagerLoading(...$relation) |
||
154 | } |
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.