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 | trait HasStates |
||
15 | { |
||
16 | /** @var \Spatie\ModelStates\StateConfig[]|null */ |
||
17 | protected static $stateFields = null; |
||
18 | |||
19 | abstract protected function registerStates(): void; |
||
20 | |||
21 | public function __set($name, $value): void |
||
29 | |||
30 | public static function bootHasStates(): void |
||
95 | |||
96 | public function initializeHasStates(): void |
||
106 | |||
107 | View Code Duplication | public function scopeWhereState(Builder $builder, string $field, $states): Builder |
|
126 | |||
127 | View Code Duplication | public function scopeWhereNotState(Builder $builder, string $field, $states): Builder |
|
142 | |||
143 | /** |
||
144 | * @param \Spatie\ModelStates\State|string $state |
||
145 | * @param string|null $field |
||
146 | * |
||
147 | * @return \Illuminate\Database\Eloquent\Model |
||
148 | */ |
||
149 | public function transitionTo($state, string $field = null) |
||
150 | { |
||
151 | $stateConfig = self::getStateConfig(); |
||
152 | |||
153 | if ($field === null && count($stateConfig) > 1) { |
||
154 | throw CouldNotPerformTransition::couldNotResolveTransitionField($this); |
||
155 | } |
||
156 | |||
157 | $field = $field ?? reset($stateConfig)->field; |
||
158 | |||
159 | return $this->{$field}->transitionTo($state); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @param string $fromClass |
||
164 | * @param string $toClass |
||
165 | * |
||
166 | * @return \Spatie\ModelStates\Transition|string|null |
||
167 | */ |
||
168 | public function resolveTransitionClass(string $fromClass, string $toClass) |
||
169 | { |
||
170 | foreach (static::getStateConfig() as $stateConfig) { |
||
171 | $transitionClass = $stateConfig->resolveTransition($this, $fromClass, $toClass); |
||
172 | |||
173 | if ($transitionClass) { |
||
174 | return $transitionClass; |
||
175 | } |
||
176 | } |
||
177 | |||
178 | throw CouldNotPerformTransition::notFound($fromClass, $toClass, $this); |
||
179 | } |
||
180 | |||
181 | protected function addState(string $field, string $stateClass): StateConfig |
||
182 | { |
||
183 | $stateConfig = new StateConfig($field, $stateClass); |
||
184 | |||
185 | static::$stateFields[$stateConfig->field] = $stateConfig; |
||
186 | |||
187 | return $stateConfig; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @return \Spatie\ModelStates\StateConfig[] |
||
192 | */ |
||
193 | public static function getStateConfig(): array |
||
194 | { |
||
195 | if (static::$stateFields === null) { |
||
196 | static::$stateFields = []; |
||
197 | |||
198 | (new static)->registerStates(); |
||
199 | } |
||
200 | |||
201 | return static::$stateFields ?? []; |
||
202 | } |
||
203 | |||
204 | public static function getStates(): Collection |
||
205 | { |
||
206 | return collect(static::getStateConfig()) |
||
207 | ->map(function ($state) { |
||
208 | return $state->stateClass::all()->map(function ($stateClass) { |
||
209 | /** @var \Spatie\ModelStates\State $stateClass */ |
||
210 | return $stateClass::getMorphClass(); |
||
211 | }); |
||
212 | }); |
||
213 | } |
||
214 | |||
215 | public static function getStatesFor(string $column): Collection |
||
219 | |||
220 | public static function getDefaultStates(): Collection |
||
227 | |||
228 | public static function getDefaultStateFor(string $column): string |
||
232 | } |
||
233 |
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.