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 |
||
| 13 | abstract class State implements JsonSerializable |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Static cache for generated state maps. |
||
| 17 | * |
||
| 18 | * @var array |
||
| 19 | * |
||
| 20 | * @see State::resolveStateMapping |
||
| 21 | */ |
||
| 22 | protected static $generatedMapping = []; |
||
| 23 | |||
| 24 | /** @var \Illuminate\Database\Eloquent\Model */ |
||
| 25 | protected $model; |
||
| 26 | |||
| 27 | public function __construct(Model $model) |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Create a state object based on a value (classname or name), |
||
| 34 | * and optionally provide its constructor arguments. |
||
| 35 | * |
||
| 36 | * @param string $name |
||
| 37 | * @param \Illuminate\Database\Eloquent\Model $model |
||
| 38 | * |
||
| 39 | * @return \Spatie\ModelStates\State |
||
| 40 | */ |
||
| 41 | public static function make(string $name, Model $model): State |
||
| 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 \Illuminate\Database\Eloquent\Model $model |
||
| 58 | * |
||
| 59 | * @return \Spatie\ModelStates\State |
||
| 60 | */ |
||
| 61 | public static function find(string $name, Model $model): State |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Get all registered state classes. |
||
| 68 | * |
||
| 69 | * @return \Illuminate\Support\Collection|string[]|static[] A list of class names. |
||
| 70 | */ |
||
| 71 | public static function all(): Collection |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The value that will be saved in the database. |
||
| 78 | * |
||
| 79 | * @return string |
||
| 80 | */ |
||
| 81 | public static function getMorphClass(): string |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The value that will be saved in the database. |
||
| 88 | * |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | public function getValue(): string |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Resolve the state class based on a value, for example a stored value in the database. |
||
| 98 | * |
||
| 99 | * @param string|\Spatie\ModelStates\State $state |
||
| 100 | * |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | public static function resolveStateClass($state): ?string |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Resolve the name of the state, which is the value that will be saved in the database. |
||
| 130 | * |
||
| 131 | * Possible names are: |
||
| 132 | * |
||
| 133 | * - The classname, if no explicit name is provided |
||
| 134 | * - A name provided in the state class as a public static property: |
||
| 135 | * `public static $name = 'dummy'` |
||
| 136 | * |
||
| 137 | * @param string|\Spatie\ModelStates\State $state |
||
| 138 | * |
||
| 139 | * @return string|null |
||
| 140 | */ |
||
| 141 | public static function resolveStateName($state): ?string |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Determine if the current state is one of an arbitrary number of other states. |
||
| 162 | * This can be either a classname or a name. |
||
| 163 | * |
||
| 164 | * @param string|array ...$stateClasses |
||
| 165 | * |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | public function isOneOf(...$statesNames): bool |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Determine if the current state equals another. |
||
| 183 | * This can be either a classname or a name. |
||
| 184 | * |
||
| 185 | * @param string|\Spatie\ModelStates\State $state |
||
| 186 | * |
||
| 187 | * @return bool |
||
| 188 | */ |
||
| 189 | public function equals($state): bool |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Determine if the current state equals another. |
||
| 197 | * This can be either a classname or a name. |
||
| 198 | * |
||
| 199 | * @param string|\Spatie\ModelStates\State $state |
||
| 200 | * |
||
| 201 | * @return bool |
||
| 202 | */ |
||
| 203 | public function is($state): bool |
||
| 207 | |||
| 208 | public function __toString(): string |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param string|\Spatie\ModelStates\Transition $transition |
||
| 215 | * @param mixed ...$args |
||
| 216 | * |
||
| 217 | * @return \Illuminate\Database\Eloquent\Model |
||
| 218 | */ |
||
| 219 | public function transition($transition, ...$args): Model |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param string|\Spatie\ModelStates\State $state |
||
| 254 | * @param mixed ...$args |
||
| 255 | * |
||
| 256 | * @return \Illuminate\Database\Eloquent\Model |
||
| 257 | */ |
||
| 258 | public function transitionTo($state, ...$args): Model |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Get the transitionable states from this state. |
||
| 274 | * |
||
| 275 | * @param string|null $field |
||
| 276 | * |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | public function transitionableStates($field = null): array |
||
| 283 | |||
| 284 | /** |
||
| 285 | * This method is used to find all available implementations of a given abstract state class. |
||
| 286 | * Finding all implementations can be done in two ways:. |
||
| 287 | * |
||
| 288 | * - The developer can define his own mapping directly in abstract state classes |
||
| 289 | * via the `protected $states = []` property |
||
| 290 | * - If no specific mapping was provided, the same directory where the abstract state class lives |
||
| 291 | * is scanned, and all concrete state classes extending the abstract state class will be provided. |
||
| 292 | * |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | private static function resolveStateMapping(): array |
||
| 335 | |||
| 336 | public function jsonSerialize() |
||
| 340 | |||
| 341 | /** |
||
| 342 | * This method is used to find the base state class. |
||
| 343 | * |
||
| 344 | * @return string |
||
| 345 | */ |
||
| 346 | public static function resolveBaseStateClass(): string |
||
| 360 | } |
||
| 361 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.