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 TransitionNotAllowed extends CouldNotPerformTransition implements ProvidesSolution |
||
10 | { |
||
11 | protected string $transitionClass; |
||
|
|||
12 | |||
13 | public static function make(string $modelClass, string $transitionClass): self |
||
14 | { |
||
15 | return (new static("The transition `{$transitionClass}` is not allowed on model `{$modelClass}` at the moment.")) |
||
16 | ->setTransitionClass($transitionClass); |
||
17 | } |
||
18 | |||
19 | public function setTransitionClass(string $transitionClass): self |
||
20 | { |
||
21 | $this->transitionClass = $transitionClass; |
||
22 | |||
23 | return $this; |
||
24 | } |
||
25 | |||
26 | public function getSolution(): Solution |
||
27 | { |
||
28 | return BaseSolution::create('Transition not allowed') |
||
29 | ->setSolutionDescription("Review your implementation of `canTransition` in {$this->transitionClass} if this is unexpected") |
||
30 | ->setDocumentationLinks([ |
||
31 | 'Custom transition classes' => 'https://docs.spatie.be/laravel-model-states/v1/working-with-transitions/02-custom-transition-classes/', |
||
32 | ]); |
||
33 | } |
||
34 | } |
||
35 |