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 MissingTraitOnModel extends InvalidConfig implements ProvidesSolution |
||
| 10 | { |
||
| 11 | protected string $modelClass; |
||
|
|
|||
| 12 | |||
| 13 | protected string $trait; |
||
| 14 | |||
| 15 | public static function make(string $modelClass, string $trait): self |
||
| 16 | { |
||
| 17 | return (new static("The method `resolveTransition` was not found on model `{$modelClass}`, are you sure it uses the `{$trait} trait?`")) |
||
| 18 | ->setModelClass($modelClass) |
||
| 19 | ->setTrait($trait); |
||
| 20 | } |
||
| 21 | |||
| 22 | public function setModelClass(string $modelClass): self |
||
| 23 | { |
||
| 24 | $this->modelClass = $modelClass; |
||
| 25 | |||
| 26 | return $this; |
||
| 27 | } |
||
| 28 | |||
| 29 | public function setTrait(string $trait): self |
||
| 30 | { |
||
| 31 | $this->trait = $trait; |
||
| 32 | |||
| 33 | return $this; |
||
| 34 | } |
||
| 35 | |||
| 36 | public function getSolution(): Solution |
||
| 37 | { |
||
| 38 | return BaseSolution::create('Missing trait on model') |
||
| 39 | ->setSolutionDescription("Use the `{$this->trait}` trait on `{$this->modelClass}`") |
||
| 40 | ->setDocumentationLinks([ |
||
| 41 | 'Configuring states' => 'https://docs.spatie.be/laravel-model-states/v1/working-with-states/01-configuring-states/', |
||
| 42 | ]); |
||
| 43 | } |
||
| 44 | } |
||
| 45 |