Conditions | 7 |
Paths | 2 |
Total Lines | 58 |
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 |
||
20 | public static function bootHasStates(): void |
||
21 | { |
||
22 | $serialiseState = function (StateConfig $stateConfig) { |
||
23 | return function (Model $model) use ($stateConfig) { |
||
24 | $value = $model->getAttribute($stateConfig->field); |
||
25 | |||
26 | if ($value === null) { |
||
27 | $value = $stateConfig->defaultStateClass; |
||
28 | } |
||
29 | |||
30 | if ($value === null) { |
||
31 | return; |
||
32 | } |
||
33 | |||
34 | $stateClass = $stateConfig->stateClass::resolveStateClass($value); |
||
35 | |||
36 | if (! is_subclass_of($stateClass, $stateConfig->stateClass)) { |
||
37 | throw InvalidConfig::fieldDoesNotExtendState( |
||
38 | $stateConfig->field, |
||
39 | $stateConfig->stateClass, |
||
40 | $stateClass |
||
41 | ); |
||
42 | } |
||
43 | |||
44 | $model->setAttribute( |
||
45 | $stateConfig->field, |
||
46 | State::resolveStateName($value) |
||
47 | ); |
||
48 | }; |
||
49 | }; |
||
50 | |||
51 | $unserialiseState = function (StateConfig $stateConfig) { |
||
52 | return function (Model $model) use ($stateConfig) { |
||
53 | $stateClass = $stateConfig->stateClass::resolveStateClass($model->getAttribute($stateConfig->field)); |
||
54 | |||
55 | $defaultState = $stateConfig->defaultStateClass |
||
56 | ? new $stateConfig->defaultStateClass($model) |
||
57 | : null; |
||
58 | |||
59 | $model->setAttribute( |
||
60 | $stateConfig->field, |
||
61 | class_exists($stateClass) |
||
62 | ? new $stateClass($model) |
||
63 | : $defaultState |
||
64 | ); |
||
65 | }; |
||
66 | }; |
||
67 | |||
68 | foreach (self::getStateConfig() as $stateConfig) { |
||
69 | static::retrieved($unserialiseState($stateConfig)); |
||
70 | static::created($unserialiseState($stateConfig)); |
||
71 | static::saved($unserialiseState($stateConfig)); |
||
72 | |||
73 | static::updating($serialiseState($stateConfig)); |
||
74 | static::creating($serialiseState($stateConfig)); |
||
75 | static::saving($serialiseState($stateConfig)); |
||
76 | } |
||
77 | } |
||
78 | |||
202 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: