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 | public function __construct(Model $model) |
||
35 | |||
36 | public function setField(string $field): State |
||
42 | |||
43 | public function getField(): string |
||
51 | |||
52 | /** |
||
53 | * Create a state object based on a value (classname or name), |
||
54 | * and optionally provide its constructor arguments. |
||
55 | * |
||
56 | * @param string $name |
||
57 | * @param mixed ...$args |
||
58 | * |
||
59 | * @return \Spatie\ModelStates\State |
||
60 | */ |
||
61 | public static function make(string $name, Model $model): State |
||
71 | |||
72 | /** |
||
73 | * Create a state object based on a value (classname or name), |
||
74 | * and optionally provide its constructor arguments. |
||
75 | * |
||
76 | * @param string $name |
||
77 | * @param mixed ...$args |
||
78 | * |
||
79 | * @return \Spatie\ModelStates\State |
||
80 | */ |
||
81 | public static function find(string $name, Model $model): State |
||
85 | |||
86 | /** |
||
87 | * Get all registered state classes. |
||
88 | * |
||
89 | * @return \Illuminate\Support\Collection|string[]|static[] A list of class names. |
||
90 | */ |
||
91 | public static function all(): Collection |
||
95 | |||
96 | /** |
||
97 | * The value that will be saved in the database. |
||
98 | * |
||
99 | * @return string |
||
100 | */ |
||
101 | public static function getMorphClass(): string |
||
105 | |||
106 | /** |
||
107 | * The value that will be saved in the database. |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | public function getValue(): string |
||
115 | |||
116 | /** |
||
117 | * Resolve the state class based on a value, for example a stored value in the database. |
||
118 | * |
||
119 | * @param string|\Spatie\ModelStates\State $state |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | public static function resolveStateClass($state): ?string |
||
147 | |||
148 | /** |
||
149 | * Resolve the name of the state, which is the value that will be saved in the database. |
||
150 | * |
||
151 | * Possible names are: |
||
152 | * |
||
153 | * - The classname, is no explicit name is provided |
||
154 | * - A name provided in the state class as a public static property: |
||
155 | * `public static $name = 'dummy'` |
||
156 | * |
||
157 | * @param $state |
||
158 | * |
||
159 | * @return string|null |
||
160 | */ |
||
161 | public static function resolveStateName($state): ?string |
||
179 | |||
180 | /** |
||
181 | * Determine if the current state is one of an arbitrary number of other states. |
||
182 | * This can be either a classname or a name. |
||
183 | * |
||
184 | * @param string|array ...$stateClasses |
||
185 | * |
||
186 | * @return bool |
||
187 | */ |
||
188 | public function isOneOf(...$statesNames): bool |
||
200 | |||
201 | /** |
||
202 | * Determine if the current state equals another. |
||
203 | * This can be either a classname or a name. |
||
204 | * |
||
205 | * @param string|\Spatie\ModelStates\State $state |
||
206 | * |
||
207 | * @return bool |
||
208 | */ |
||
209 | public function equals($state): bool |
||
214 | |||
215 | /** |
||
216 | * Determine if the current state equals another. |
||
217 | * This can be either a classname or a name. |
||
218 | * |
||
219 | * @param string|\Spatie\ModelStates\State $state |
||
220 | * |
||
221 | * @return bool |
||
222 | */ |
||
223 | public function is($state): bool |
||
227 | |||
228 | public function __toString(): string |
||
232 | |||
233 | /** |
||
234 | * @param string|\Spatie\ModelStates\Transition $transitionClass |
||
235 | * @param mixed ...$args |
||
236 | * |
||
237 | * @return \Illuminate\Database\Eloquent\Model |
||
238 | */ |
||
239 | public function transition($transition, ...$args): Model |
||
257 | |||
258 | /** |
||
259 | * @param string|\Spatie\ModelStates\State $state |
||
260 | * @param mixed ...$args |
||
261 | * |
||
262 | * @return \Illuminate\Database\Eloquent\Model |
||
263 | */ |
||
264 | public function transitionTo($state, ...$args): Model |
||
277 | |||
278 | /** |
||
279 | * Get the transitionable states from this state. |
||
280 | * |
||
281 | * @return array |
||
282 | */ |
||
283 | public function transitionableStates($field = null): array |
||
287 | |||
288 | /** |
||
289 | * This method is used to find all available implementations of a given abstract state class. |
||
290 | * Finding all implementations can be done in two ways:. |
||
291 | * |
||
292 | * - The developer can define his own mapping directly in abstract state classes |
||
293 | * via the `protected $states = []` property |
||
294 | * - If no specific mapping was provided, the same directory where the abstract state class lives |
||
295 | * is scanned, and all concrete state classes extending the abstract state class will be provided. |
||
296 | * |
||
297 | * @return array |
||
298 | */ |
||
299 | private static function resolveStateMapping(): array |
||
337 | |||
338 | public function jsonSerialize() |
||
342 | } |
||
343 |
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: