Complex classes like StateMachine 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.
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 StateMachine, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class StateMachine implements StateMachineInterface |
||
|
|
|||
| 24 | { |
||
| 25 | /** |
||
| 26 | * The stateful object. |
||
| 27 | * |
||
| 28 | * @var object |
||
| 29 | */ |
||
| 30 | protected $object; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The available states. |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected $states = array(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The available transitions. |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $transitions = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The current state. |
||
| 48 | * |
||
| 49 | * @var StateInterface |
||
| 50 | */ |
||
| 51 | protected $currentState; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var EventDispatcherInterface |
||
| 55 | */ |
||
| 56 | protected $dispatcher; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var StateAccessorInterface |
||
| 60 | */ |
||
| 61 | protected $stateAccessor; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | protected $graph; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param object $object |
||
| 70 | * @param EventDispatcherInterface $dispatcher |
||
| 71 | * @param StateAccessorInterface $stateAccessor |
||
| 72 | */ |
||
| 73 | 215 | public function __construct( |
|
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | 160 | public function initialize() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * {@inheritdoc} |
||
| 115 | * |
||
| 116 | * @throws Exception\StateException |
||
| 117 | */ |
||
| 118 | 20 | public function apply($transitionName, array $parameters = array()) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * {@inheritdoc} |
||
| 145 | */ |
||
| 146 | 55 | public function can($transition, array $parameters = array()) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritdoc} |
||
| 166 | */ |
||
| 167 | 180 | public function addState($state) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * {@inheritdoc} |
||
| 178 | */ |
||
| 179 | 175 | public function addTransition($transition, $initialState = null, $finalState = null) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * {@inheritdoc} |
||
| 219 | */ |
||
| 220 | 160 | public function getTransition($name) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * {@inheritdoc} |
||
| 236 | */ |
||
| 237 | 180 | public function getState($name) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * {@inheritdoc} |
||
| 255 | */ |
||
| 256 | 5 | public function getTransitions() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * {@inheritdoc} |
||
| 263 | */ |
||
| 264 | 5 | public function getStates() |
|
| 268 | |||
| 269 | /** |
||
| 270 | * {@inheritdoc} |
||
| 271 | */ |
||
| 272 | 150 | public function setObject($object) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * {@inheritdoc} |
||
| 279 | */ |
||
| 280 | 175 | public function getObject() |
|
| 284 | |||
| 285 | /** |
||
| 286 | * {@inheritdoc} |
||
| 287 | */ |
||
| 288 | 115 | public function getCurrentState() |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Find and return the Initial state if exists. |
||
| 295 | * |
||
| 296 | * @return string |
||
| 297 | * |
||
| 298 | * @throws Exception\StateException |
||
| 299 | */ |
||
| 300 | 20 | protected function findInitialState() |
|
| 314 | |||
| 315 | /** |
||
| 316 | * @param EventDispatcherInterface $dispatcher |
||
| 317 | */ |
||
| 318 | public function setDispatcher(EventDispatcherInterface $dispatcher) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @return EventDispatcherInterface |
||
| 325 | */ |
||
| 326 | 5 | public function getDispatcher() |
|
| 330 | |||
| 331 | /** |
||
| 332 | * @param StateAccessorInterface $stateAccessor |
||
| 333 | */ |
||
| 334 | 10 | public function setStateAccessor(StateAccessorInterface $stateAccessor) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * {@inheritdoc} |
||
| 341 | */ |
||
| 342 | 15 | public function setGraph($graph) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * {@inheritdoc} |
||
| 349 | */ |
||
| 350 | 175 | public function getGraph() |
|
| 354 | |||
| 355 | /** |
||
| 356 | * {@inheritDoc} |
||
| 357 | */ |
||
| 358 | 10 | public function findStateWithProperty($property, $value = null) |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Dispatches event for the transition |
||
| 385 | * |
||
| 386 | * @param TransitionInterface $transition |
||
| 387 | * @param TransitionEvent $event |
||
| 388 | * @param type $transitionState |
||
| 389 | */ |
||
| 390 | 50 | private function dispatchTransitionEvent(TransitionInterface $transition, TransitionEvent $event, $transitionState) |
|
| 398 | } |
||
| 399 |