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 |
||
15 | trait HasEnums |
||
16 | { |
||
17 | 96 | View Code Duplication | public function setAttribute($key, $value) |
|
|||
18 | { |
||
19 | 96 | if (! $this->isEnumAttribute($key) || $this->isNullableEnum($key, $value)) { |
|
20 | 72 | return parent::setAttribute($key, $value); |
|
21 | } |
||
22 | |||
23 | 96 | return $this->setEnumAttribute($key, $value); |
|
24 | } |
||
25 | |||
26 | 40 | View Code Duplication | public function getAttribute($key) |
27 | { |
||
28 | 40 | $value = parent::getAttribute($key); |
|
29 | |||
30 | 40 | if (! $this->isEnumAttribute($key) || $this->isNullableEnum($key, $value)) { |
|
31 | 24 | return $value; |
|
32 | } |
||
33 | |||
34 | 40 | return $this->getEnumAttribute($key, $value); |
|
35 | } |
||
36 | |||
37 | /** |
||
38 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
39 | * @param string $key |
||
40 | * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables |
||
41 | * |
||
42 | * @see \Illuminate\Database\Eloquent\Builder::whereIn() |
||
43 | */ |
||
44 | 32 | public function scopeWhereEnum( |
|
45 | Builder $builder, |
||
46 | string $key, |
||
47 | $enumerables |
||
48 | ): void { |
||
49 | 32 | $this->buildEnumScope( |
|
50 | 32 | $builder, |
|
51 | 32 | 'whereIn', |
|
52 | 16 | $key, |
|
53 | 16 | $enumerables |
|
54 | ); |
||
55 | 28 | } |
|
56 | |||
57 | /** |
||
58 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
59 | * @param string $key |
||
60 | * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables |
||
61 | * |
||
62 | * @see \Illuminate\Database\Eloquent\Builder::orWhereIn() |
||
63 | */ |
||
64 | 12 | public function scopeOrWhereEnum( |
|
65 | Builder $builder, |
||
66 | string $key, |
||
67 | $enumerables |
||
68 | ): void { |
||
69 | 12 | $this->buildEnumScope( |
|
70 | 12 | $builder, |
|
71 | 12 | 'orWhereIn', |
|
72 | 6 | $key, |
|
73 | 6 | $enumerables |
|
74 | ); |
||
75 | 8 | } |
|
76 | |||
77 | /** |
||
78 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
79 | * @param string $key |
||
80 | * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables |
||
81 | * |
||
82 | * @see \Illuminate\Database\Eloquent\Builder::whereNotIn() |
||
83 | */ |
||
84 | 20 | public function scopeWhereNotEnum( |
|
85 | Builder $builder, |
||
86 | string $key, |
||
87 | $enumerables |
||
88 | ): void { |
||
89 | 20 | $this->buildEnumScope( |
|
90 | 20 | $builder, |
|
91 | 20 | 'whereNotIn', |
|
92 | 10 | $key, |
|
93 | 10 | $enumerables |
|
94 | ); |
||
95 | 16 | } |
|
96 | |||
97 | /** |
||
98 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
99 | * @param string $key |
||
100 | * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables |
||
101 | * |
||
102 | * @see \Illuminate\Database\Eloquent\Builder::orWhereNotIn() |
||
103 | */ |
||
104 | 12 | public function scopeOrWhereNotEnum( |
|
105 | Builder $builder, |
||
106 | string $key, |
||
107 | $enumerables |
||
108 | ): void { |
||
109 | 12 | $this->buildEnumScope( |
|
110 | 12 | $builder, |
|
111 | 12 | 'orWhereNotIn', |
|
112 | 6 | $key, |
|
113 | 6 | $enumerables |
|
114 | ); |
||
115 | 8 | } |
|
116 | |||
117 | /** |
||
118 | * @param string $key |
||
119 | * @param int|string|\Spatie\Enum\Enumerable $value |
||
120 | * |
||
121 | * @return $this |
||
122 | */ |
||
123 | 96 | protected function setEnumAttribute(string $key, $value) |
|
124 | { |
||
125 | 96 | $enumClass = $this->getEnumClass($key); |
|
126 | |||
127 | 88 | if (is_string($value) || is_int($value)) { |
|
128 | 16 | $value = $this->asEnum($enumClass, $value); |
|
129 | } |
||
130 | |||
131 | 88 | if (! is_a($value, $enumClass)) { |
|
132 | 4 | throw InvalidEnumError::make(static::class, $key, $enumClass, get_class($value)); |
|
133 | } |
||
134 | |||
135 | 84 | $this->attributes[$key] = $this->getStoredValue($key, $value); |
|
136 | |||
137 | 84 | return $this; |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param string $key |
||
142 | * @param \Spatie\Enum\Enumerable $enum |
||
143 | * |
||
144 | * @return int|string |
||
145 | */ |
||
146 | 84 | protected function getStoredValue(string $key, Enumerable $enum) |
|
147 | { |
||
148 | 84 | return $this->hasCast($key, ['int', 'integer']) |
|
149 | 4 | ? $enum->getIndex() |
|
150 | 84 | : $enum->getValue(); |
|
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param string $key |
||
155 | * @param int|string $value |
||
156 | * |
||
157 | * @return \Spatie\Enum\Enumerable |
||
158 | */ |
||
159 | 84 | protected function getEnumAttribute(string $key, $value): Enumerable |
|
163 | |||
164 | 112 | protected function isEnumAttribute(string $key): bool |
|
168 | |||
169 | 96 | protected function isNullableEnum(string $key, $value): bool |
|
170 | { |
||
171 | 96 | $enum = $this->enums[$key]; |
|
172 | |||
173 | 96 | if (! is_array($enum)) { |
|
174 | 92 | return false; |
|
175 | } |
||
176 | |||
183 | |||
184 | 96 | protected function getEnumClass(string $key): string |
|
206 | |||
207 | /** |
||
208 | * @param string $class |
||
209 | * @param int|string $value |
||
210 | * |
||
211 | * @return \Spatie\Enum\Enumerable |
||
212 | */ |
||
213 | 84 | protected function asEnum(string $class, $value): Enumerable |
|
224 | |||
225 | /** |
||
226 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
227 | * @param string $method |
||
228 | * @param string $key |
||
229 | * @param int|string|\Spatie\Enum\Enumerable|int[]|string[]|\Spatie\Enum\Enumerable[] $enumerables |
||
230 | */ |
||
231 | 60 | protected function buildEnumScope( |
|
250 | } |
||
251 |
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.