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 | public function getStateConfig(): StateConfig |
||
56 | |||
57 | /** |
||
58 | * Create a state object based on a value (classname or name), |
||
59 | * and optionally provide its constructor arguments. |
||
60 | * |
||
61 | * @param string $name |
||
62 | * @param mixed ...$args |
||
63 | * |
||
64 | * @return \Spatie\ModelStates\State |
||
65 | */ |
||
66 | public static function make(string $name, Model $model): State |
||
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, is 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 |
||
262 | |||
263 | /** |
||
264 | * @param string|\Spatie\ModelStates\State $state |
||
265 | * @param mixed ...$args |
||
266 | * |
||
267 | * @return \Illuminate\Database\Eloquent\Model |
||
268 | */ |
||
269 | public function transitionTo($state, ...$args): Model |
||
282 | |||
283 | public function transitionableStates(): array |
||
290 | |||
291 | /** |
||
292 | * This method is used to find all available implementations of a given abstract state class. |
||
293 | * Finding all implementations can be done in two ways:. |
||
294 | * |
||
295 | * - The developer can define his own mapping directly in abstract state classes |
||
296 | * via the `protected $states = []` property |
||
297 | * - If no specific mapping was provided, the same directory where the abstract state class lives |
||
298 | * is scanned, and all concrete state classes extending the abstract state class will be provided. |
||
299 | * |
||
300 | * @return array |
||
301 | */ |
||
302 | private static function resolveStateMapping(): array |
||
340 | |||
341 | public function jsonSerialize() |
||
345 | } |
||
346 |
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: