Complex classes like State often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use State, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | abstract class State implements JsonSerializable |
||
15 | { |
||
16 | /** |
||
17 | * Static cache for generated state maps. |
||
18 | * |
||
19 | * @var array |
||
20 | * |
||
21 | * @see State::resolveStateMapping |
||
22 | */ |
||
23 | protected static $generatedMapping = []; |
||
24 | |||
25 | /** @var \Illuminate\Database\Eloquent\Model */ |
||
26 | protected $model; |
||
27 | |||
28 | /** @var string|null */ |
||
29 | protected $field; |
||
30 | |||
31 | /** |
||
32 | * Create a state object based on a value (classname or name), |
||
33 | * and optionally provide its constructor arguments. |
||
34 | * |
||
35 | * @param string $name |
||
36 | * @param mixed ...$args |
||
37 | * |
||
38 | * @return \Spatie\ModelStates\State |
||
39 | */ |
||
40 | public static function make(string $name, Model $model): State |
||
50 | |||
51 | public function __construct(Model $model) |
||
52 | { |
||
53 | $this->model = $model; |
||
54 | } |
||
55 | |||
56 | public function setField(string $field): State |
||
57 | { |
||
58 | $this->field = $field; |
||
59 | |||
60 | return $this; |
||
61 | } |
||
62 | |||
63 | public function getField(): string |
||
64 | { |
||
65 | if (! $this->field) { |
||
|
|||
66 | throw new Exception("Could not determine the field name of this state class."); |
||
67 | } |
||
68 | |||
69 | return $this->field; |
||
70 | } |
||
71 | |||
72 | public function getStateConfig(): StateConfig |
||
73 | { |
||
74 | return $this->model::getStateConfig()[$this->field]; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Create a state object based on a value (classname or name), |
||
79 | * and optionally provide its constructor arguments. |
||
80 | * |
||
81 | * @param string $name |
||
82 | * @param mixed ...$args |
||
83 | * |
||
84 | * @return \Spatie\ModelStates\State |
||
85 | */ |
||
86 | public static function find(string $name, Model $model): State |
||
90 | |||
91 | /** |
||
92 | * Get all registered state classes. |
||
93 | * |
||
94 | * @return \Illuminate\Support\Collection|string[]|static[] A list of class names. |
||
95 | */ |
||
96 | public static function all(): Collection |
||
100 | |||
101 | /** |
||
102 | * The value that will be saved in the database. |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | public static function getMorphClass(): string |
||
110 | |||
111 | /** |
||
112 | * The value that will be saved in the database. |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | public function getValue(): string |
||
120 | |||
121 | /** |
||
122 | * Resolve the state class based on a value, for example a stored value in the database. |
||
123 | * |
||
124 | * @param string|\Spatie\ModelStates\State $state |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | public static function resolveStateClass($state): ?string |
||
152 | |||
153 | /** |
||
154 | * Resolve the name of the state, which is the value that will be saved in the database. |
||
155 | * |
||
156 | * Possible names are: |
||
157 | * |
||
158 | * - The classname, if no explicit name is provided |
||
159 | * - A name provided in the state class as a public static property: |
||
160 | * `public static $name = 'dummy'` |
||
161 | * |
||
162 | * @param $state |
||
163 | * |
||
164 | * @return string|null |
||
165 | */ |
||
166 | public static function resolveStateName($state): ?string |
||
184 | |||
185 | /** |
||
186 | * Determine if the current state is one of an arbitrary number of other states. |
||
187 | * This can be either a classname or a name. |
||
188 | * |
||
189 | * @param string|array ...$stateClasses |
||
190 | * |
||
191 | * @return bool |
||
192 | */ |
||
193 | public function isOneOf(...$statesNames): bool |
||
205 | |||
206 | /** |
||
207 | * Determine if the current state equals another. |
||
208 | * This can be either a classname or a name. |
||
209 | * |
||
210 | * @param string|\Spatie\ModelStates\State $state |
||
211 | * |
||
212 | * @return bool |
||
213 | */ |
||
214 | public function equals($state): bool |
||
219 | |||
220 | /** |
||
221 | * Determine if the current state equals another. |
||
222 | * This can be either a classname or a name. |
||
223 | * |
||
224 | * @param string|\Spatie\ModelStates\State $state |
||
225 | * |
||
226 | * @return bool |
||
227 | */ |
||
228 | public function is($state): bool |
||
232 | |||
233 | public function __toString(): string |
||
237 | |||
238 | /** |
||
239 | * @param string|\Spatie\ModelStates\Transition $transitionClass |
||
240 | * @param mixed ...$args |
||
241 | * |
||
242 | * @return \Illuminate\Database\Eloquent\Model |
||
243 | */ |
||
244 | public function transition($transition, ...$args): Model |
||
276 | |||
277 | /** |
||
278 | * @param string|\Spatie\ModelStates\State $state |
||
279 | * @param mixed ...$args |
||
280 | * |
||
281 | * @return \Illuminate\Database\Eloquent\Model |
||
282 | */ |
||
283 | public function transitionTo($state, ...$args): Model |
||
296 | |||
297 | /** |
||
298 | * Check whether the current state can transition to another one |
||
299 | * |
||
300 | * @param string|\Spatie\ModelStates\State $state |
||
301 | * |
||
302 | * @return bool |
||
303 | */ |
||
304 | public function canTransitionTo($state): bool |
||
311 | |||
312 | public function transitionableStates(): array |
||
318 | |||
319 | /** |
||
320 | * This method is used to find all available implementations of a given abstract state class. |
||
321 | * Finding all implementations can be done in two ways:. |
||
322 | * |
||
323 | * - The developer can define his own mapping directly in abstract state classes |
||
324 | * via the `protected $states = []` property |
||
325 | * - If no specific mapping was provided, the same directory where the abstract state class lives |
||
326 | * is scanned, and all concrete state classes extending the abstract state class will be provided. |
||
327 | * |
||
328 | * @return array |
||
329 | */ |
||
330 | private static function resolveStateMapping(): array |
||
368 | |||
369 | public function jsonSerialize() |
||
373 | } |
||
374 |
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: