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 |
||
| 9 | class CouldNotResolveTransitionField extends CouldNotPerformTransition implements ProvidesSolution |
||
| 10 | { |
||
| 11 | protected string $modelClass; |
||
|
|
|||
| 12 | |||
| 13 | public static function make(string $modelClass): self |
||
| 14 | { |
||
| 15 | return (new static("You tried to invoke {$modelClass}::transitionTo() directly, though there are multiple state fields configured.")) |
||
| 16 | ->setModelClass($modelClass); |
||
| 17 | } |
||
| 18 | |||
| 19 | public function setModelClass(string $modelClass): self |
||
| 20 | { |
||
| 21 | $this->modelClass = $modelClass; |
||
| 22 | |||
| 23 | return $this; |
||
| 24 | } |
||
| 25 | |||
| 26 | public function getSolution(): Solution |
||
| 27 | { |
||
| 28 | return BaseSolution::create('Could not resolve transition field') |
||
| 29 | ->setSolutionDescription("Use {$this->modelClass}->stateField->transitionTo()") |
||
| 30 | ->setDocumentationLinks([ |
||
| 31 | 'Using transitions' => 'https://docs.spatie.be/laravel-model-states/v1/working-with-transitions/01-configuring-transitions/#using-transitions', |
||
| 32 | ]); |
||
| 33 | } |
||
| 34 | } |
||
| 35 |