| Conditions | 7 |
| Paths | 2 |
| Total Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 25 | if ($model->{$stateConfig->fieldName} !== null) { |
||
| 26 | continue; |
||
| 27 | } |
||
| 28 | |||
| 29 | $model->{$stateConfig->fieldName} = $stateConfig->defaultStateClass; |
||
| 30 | } |
||
| 31 | }); |
||
| 32 | } |
||
| 33 | |||
| 34 | public function getCasts(): array |
||
| 35 | { |
||
| 36 | $this->initStateConfigs(); |
||
| 37 | |||
| 38 | return array_merge( |
||
| 39 | parent::getCasts(), |
||
| 40 | $this->stateCasts, |
||
| 41 | ); |
||
| 42 | } |
||
| 43 | |||
| 44 | protected function addState(string $fieldName, string $stateClass): StateConfig |
||
| 45 | { |
||
| 46 | $stateConfig = new StateConfig( |
||
| 47 | static::class, |
||
| 48 | $fieldName, |
||
| 49 | $stateClass, |
||
| 50 | ); |
||
| 51 | |||
| 52 | $this->stateCasts[$fieldName] = $stateClass; |
||
| 53 | |||
| 54 | $this->stateConfigs[$fieldName] = $stateConfig; |
||
| 55 | |||
| 56 | return $stateConfig; |
||
| 57 | } |
||
| 58 | |||
| 59 | public function getStateConfig(string $fieldName): StateConfig |
||
| 60 | { |
||
| 61 | return $this->stateConfigs[$fieldName]; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @return \Spatie\ModelStates\StateConfig[] |
||
| 66 | */ |
||
| 67 | public function getStateConfigs(): array |
||
| 68 | { |
||
| 69 | return $this->stateConfigs ?? []; |
||
| 70 | } |
||
| 71 | |||
| 72 | private function initStateConfigs(): void |
||
| 73 | { |
||
| 74 | if ($this->stateConfigs === null) { |
||
| 75 | $this->stateConfigs = []; |
||
| 76 | $this->registerStates(); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | } |
||
| 80 |