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 | public function __construct(Model $model) |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Create a state object based on a value (classname or name), |
||
| 35 | * and optionally provide its constructor arguments. |
||
| 36 | * |
||
| 37 | * @param string $name |
||
| 38 | * @param mixed ...$args |
||
| 39 | * |
||
| 40 | * @return \Spatie\ModelStates\State |
||
| 41 | */ |
||
| 42 | public static function make(string $name, Model $model): State |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Create a state object based on a value (classname or name), |
||
| 55 | * and optionally provide its constructor arguments. |
||
| 56 | * |
||
| 57 | * @param string $name |
||
| 58 | * @param mixed ...$args |
||
| 59 | * |
||
| 60 | * @return \Spatie\ModelStates\State |
||
| 61 | */ |
||
| 62 | public static function find(string $name, Model $model): State |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Get all registered state classes. |
||
| 69 | * |
||
| 70 | * @return \Illuminate\Support\Collection|string[]|static[] A list of class names. |
||
| 71 | */ |
||
| 72 | public static function all(): Collection |
||
| 76 | |||
| 77 | /** |
||
| 78 | * The value that will be saved in the database. |
||
| 79 | * |
||
| 80 | * @return string |
||
| 81 | */ |
||
| 82 | public static function getMorphClass(): string |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The value that will be saved in the database. |
||
| 89 | * |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | public function getValue(): string |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Resolve the state class based on a value, for example a stored value in the database. |
||
| 99 | * |
||
| 100 | * @param string|\Spatie\ModelStates\State $state |
||
| 101 | * |
||
| 102 | * @return string |
||
| 103 | */ |
||
| 104 | public static function resolveStateClass($state): ?string |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Resolve the name of the state, which is the value that will be saved in the database. |
||
| 131 | * |
||
| 132 | * Possible names are: |
||
| 133 | * |
||
| 134 | * - The classname, if no explicit name is provided |
||
| 135 | * - A name provided in the state class as a public static property: |
||
| 136 | * `public static $name = 'dummy'` |
||
| 137 | * |
||
| 138 | * @param $state |
||
| 139 | * |
||
| 140 | * @return string|null |
||
| 141 | */ |
||
| 142 | public static function resolveStateName($state): ?string |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Determine if the current state is one of an arbitrary number of other states. |
||
| 163 | * This can be either a classname or a name. |
||
| 164 | * |
||
| 165 | * @param string|array ...$stateClasses |
||
| 166 | * |
||
| 167 | * @return bool |
||
| 168 | */ |
||
| 169 | public function isOneOf(...$statesNames): bool |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Determine if the current state equals another. |
||
| 184 | * This can be either a classname or a name. |
||
| 185 | * |
||
| 186 | * @param string|\Spatie\ModelStates\State $state |
||
| 187 | * |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | public function equals($state): bool |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Determine if the current state equals another. |
||
| 198 | * This can be either a classname or a name. |
||
| 199 | * |
||
| 200 | * @param string|\Spatie\ModelStates\State $state |
||
| 201 | * |
||
| 202 | * @return bool |
||
| 203 | */ |
||
| 204 | public function is($state): bool |
||
| 208 | |||
| 209 | public function __toString(): string |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param string|\Spatie\ModelStates\Transition $transitionClass |
||
| 216 | * @param mixed ...$args |
||
| 217 | * |
||
| 218 | * @return \Illuminate\Database\Eloquent\Model |
||
| 219 | */ |
||
| 220 | public function transition($transition, ...$args): Model |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param string|\Spatie\ModelStates\State $state |
||
| 255 | * @param mixed ...$args |
||
| 256 | * |
||
| 257 | * @return \Illuminate\Database\Eloquent\Model |
||
| 258 | */ |
||
| 259 | public function transitionTo($state, ...$args): Model |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Get the transitionable states from this state. |
||
| 275 | * |
||
| 276 | * @return array |
||
| 277 | */ |
||
| 278 | public function transitionableStates($field = null): array |
||
| 282 | |||
| 283 | /** |
||
| 284 | * This method is used to find all available implementations of a given abstract state class. |
||
| 285 | * Finding all implementations can be done in two ways:. |
||
| 286 | * |
||
| 287 | * - The developer can define his own mapping directly in abstract state classes |
||
| 288 | * via the `protected $states = []` property |
||
| 289 | * - If no specific mapping was provided, the same directory where the abstract state class lives |
||
| 290 | * is scanned, and all concrete state classes extending the abstract state class will be provided. |
||
| 291 | * |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | private static function resolveStateMapping(): array |
||
| 332 | |||
| 333 | public function shouldSetTimestamp() : bool |
||
| 337 | |||
| 338 | public function getTimestampField() : ?string |
||
| 342 | |||
| 343 | public function getTimestamp(): ?Carbon |
||
| 349 | |||
| 350 | public function jsonSerialize() |
||
| 354 | } |
||
| 355 |
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: