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 | public function __construct( |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @{inheritDoc} |
||
| 86 | */ |
||
| 87 | public function initialize() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @{inheritDoc} |
||
| 116 | * |
||
| 117 | * @throws Exception\StateException |
||
| 118 | */ |
||
| 119 | public function apply($transitionName) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @{inheritDoc} |
||
| 148 | */ |
||
| 149 | public function can($transition) |
||
| 150 | { |
||
| 151 | $transition = $transition instanceof TransitionInterface ? $transition : $this->getTransition($transition); |
||
| 152 | |||
| 153 | if (null !== $transition->getGuard() && !call_user_func($transition->getGuard(), $this)) { |
||
| 154 | return false; |
||
| 155 | } |
||
| 156 | |||
| 157 | if (!in_array($transition->getName(), $this->getCurrentState()->getTransitions())) { |
||
| 158 | return false; |
||
| 159 | } |
||
| 160 | |||
| 161 | $event = new TransitionEvent($this->getCurrentState(), $transition, $this); |
||
| 162 | $this->dispatcher->dispatch(FiniteEvents::TEST_TRANSITION, $event); |
||
| 163 | $this->dispatcher->dispatch(FiniteEvents::TEST_TRANSITION . '.' . $transition->getName(), $event); |
||
| 164 | |||
| 165 | return !$event->isRejected(); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @{inheritDoc} |
||
| 170 | */ |
||
| 171 | public function addState($state) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @{inheritDoc} |
||
| 182 | */ |
||
| 183 | public function addTransition($transition, $initialState = null, $finalState = null) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @{inheritDoc} |
||
| 223 | */ |
||
| 224 | public function getTransition($name) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @{inheritDoc} |
||
| 240 | */ |
||
| 241 | public function getState($name) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @{inheritDoc} |
||
| 259 | */ |
||
| 260 | public function getTransitions() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @{inheritDoc} |
||
| 267 | */ |
||
| 268 | public function getStates() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * {@inheritDoc} |
||
| 275 | */ |
||
| 276 | public function setObject($object) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @{inheritDoc} |
||
| 283 | */ |
||
| 284 | public function getObject() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @{inheritDoc} |
||
| 291 | */ |
||
| 292 | public function getCurrentState() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Find and return the Initial state if exists |
||
| 299 | * |
||
| 300 | * @return string |
||
| 301 | * |
||
| 302 | * @throws Exception\StateException |
||
| 303 | */ |
||
| 304 | protected function findInitialState() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param EventDispatcherInterface $dispatcher |
||
| 321 | */ |
||
| 322 | public function setDispatcher(EventDispatcherInterface $dispatcher) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @return EventDispatcherInterface |
||
| 329 | */ |
||
| 330 | public function getDispatcher() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param StateAccessorInterface $stateAccessor |
||
| 337 | */ |
||
| 338 | public function setStateAccessor(StateAccessorInterface $stateAccessor) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @{inheritDoc} |
||
| 345 | */ |
||
| 346 | public function setGraph($graph) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @{inheritDoc} |
||
| 353 | */ |
||
| 354 | public function getGraph() |
||
| 358 | } |
||
| 359 |