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 UnknownState extends InvalidConfig implements ProvidesSolution |
||
10 | { |
||
11 | protected string $field; |
||
|
|||
12 | |||
13 | protected string $modelClass; |
||
14 | |||
15 | public static function make(string $field, string $modelClass): self |
||
16 | { |
||
17 | return (new static("No state field found for {$modelClass}::{$field}, did you forget to provide a mapping in {$modelClass}::registerStates()?")) |
||
18 | ->setField($field) |
||
19 | ->setModelClass($modelClass); |
||
20 | } |
||
21 | |||
22 | public function setField(string $field): self |
||
23 | { |
||
24 | $this->field = $field; |
||
25 | |||
26 | return $this; |
||
27 | } |
||
28 | |||
29 | public function setModelClass(string $modelClass): self |
||
30 | { |
||
31 | $this->modelClass = $modelClass; |
||
32 | |||
33 | return $this; |
||
34 | } |
||
35 | |||
36 | public function getSolution(): Solution |
||
37 | { |
||
38 | return BaseSolution::create('The state field is unknown') |
||
39 | ->setSolutionDescription("Add the `{$this->field}` field to the `registerStates` method inside `{$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 |