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 | 180 | public function __construct( |
|
| 83 | |||
| 84 | /** |
||
| 85 | * @{inheritDoc} |
||
| 86 | */ |
||
| 87 | 140 | public function initialize() |
|
| 113 | |||
| 114 | /** |
||
| 115 | * {@inheritDoc} |
||
| 116 | * |
||
| 117 | * @throws Exception\StateException |
||
| 118 | */ |
||
| 119 | 15 | public function apply($transitionName, array $parameters = array()) |
|
| 120 | { |
||
| 121 | 15 | $transition = $this->getTransition($transitionName); |
|
| 122 | 15 | $event = new TransitionEvent($this->getCurrentState(), $transition, $this, $parameters); |
|
| 123 | 15 | if (!$this->can($transition, $parameters)) { |
|
| 124 | 5 | throw new Exception\StateException(sprintf( |
|
| 125 | 5 | 'The "%s" transition can not be applied to the "%s" state of object "%s" with graph "%s".', |
|
| 126 | 5 | $transition->getName(), |
|
| 127 | 5 | $this->currentState->getName(), |
|
| 128 | 5 | get_class($this->getObject()), |
|
| 129 | 5 | $this->getGraph() |
|
| 130 | 4 | )); |
|
| 131 | } |
||
| 132 | |||
| 133 | 15 | $this->dispatcher->dispatch(FiniteEvents::PRE_TRANSITION, $event); |
|
| 134 | 15 | $this->dispatcher->dispatch(FiniteEvents::PRE_TRANSITION . '.' . $transitionName, $event); |
|
| 135 | 15 | if (null !== $this->getGraph()) { |
|
| 136 | 5 | $this->dispatcher->dispatch(FiniteEvents::PRE_TRANSITION . '.' . $this->getGraph() . '.' . $transition->getName(), $event); |
|
| 137 | 4 | } |
|
| 138 | |||
| 139 | 15 | $returnValue = $transition->process($this); |
|
| 140 | 15 | $this->stateAccessor->setState($this->object, $transition->getState()); |
|
| 141 | 15 | $this->currentState = $this->getState($transition->getState()); |
|
| 142 | |||
| 143 | 15 | $this->dispatcher->dispatch(FiniteEvents::POST_TRANSITION, $event); |
|
| 144 | 15 | $this->dispatcher->dispatch(FiniteEvents::POST_TRANSITION . '.' . $transitionName, $event); |
|
| 145 | 15 | if (null !== $this->getGraph()) { |
|
| 146 | 5 | $this->dispatcher->dispatch(FiniteEvents::POST_TRANSITION . '.' . $this->getGraph() . '.' . $transition->getName(), $event); |
|
| 147 | 4 | } |
|
| 148 | |||
| 149 | 15 | return $returnValue; |
|
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * {@inheritDoc} |
||
| 154 | */ |
||
| 155 | 50 | public function can($transition, array $parameters = array()) |
|
| 156 | { |
||
| 157 | 50 | $transition = $transition instanceof TransitionInterface ? $transition : $this->getTransition($transition); |
|
| 158 | |||
| 159 | 50 | if (null !== $transition->getGuard() && !call_user_func($transition->getGuard(), $this)) { |
|
| 160 | 5 | return false; |
|
| 161 | } |
||
| 162 | |||
| 163 | 45 | if (!in_array($transition->getName(), $this->getCurrentState()->getTransitions())) { |
|
| 164 | 30 | return false; |
|
| 165 | } |
||
| 166 | |||
| 167 | 45 | $event = new TransitionEvent($this->getCurrentState(), $transition, $this, $parameters); |
|
| 168 | 45 | $this->dispatcher->dispatch(FiniteEvents::TEST_TRANSITION, $event); |
|
| 169 | 45 | $this->dispatcher->dispatch(FiniteEvents::TEST_TRANSITION . '.' . $transition->getName(), $event); |
|
| 170 | 45 | if (null !== $this->getGraph()) { |
|
| 171 | 5 | $this->dispatcher->dispatch(FiniteEvents::TEST_TRANSITION . '.' . $this->getGraph() . '.' . $transition->getName(), $event); |
|
| 172 | 4 | } |
|
| 173 | |||
| 174 | 45 | return !$event->isRejected(); |
|
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @{inheritDoc} |
||
| 179 | */ |
||
| 180 | 160 | public function addState($state) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * @{inheritDoc} |
||
| 191 | */ |
||
| 192 | 155 | public function addTransition($transition, $initialState = null, $finalState = null) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * @{inheritDoc} |
||
| 232 | */ |
||
| 233 | 140 | public function getTransition($name) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @{inheritDoc} |
||
| 249 | */ |
||
| 250 | 160 | public function getState($name) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * @{inheritDoc} |
||
| 268 | */ |
||
| 269 | 5 | public function getTransitions() |
|
| 273 | |||
| 274 | /** |
||
| 275 | * @{inheritDoc} |
||
| 276 | */ |
||
| 277 | 5 | public function getStates() |
|
| 281 | |||
| 282 | /** |
||
| 283 | * {@inheritDoc} |
||
| 284 | */ |
||
| 285 | 135 | public function setObject($object) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * @{inheritDoc} |
||
| 292 | */ |
||
| 293 | 155 | public function getObject() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * @{inheritDoc} |
||
| 300 | */ |
||
| 301 | 105 | public function getCurrentState() |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Find and return the Initial state if exists |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | * |
||
| 311 | * @throws Exception\StateException |
||
| 312 | */ |
||
| 313 | 15 | protected function findInitialState() |
|
| 327 | |||
| 328 | /** |
||
| 329 | * @param EventDispatcherInterface $dispatcher |
||
| 330 | */ |
||
| 331 | public function setDispatcher(EventDispatcherInterface $dispatcher) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @return EventDispatcherInterface |
||
| 338 | */ |
||
| 339 | public function getDispatcher() |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param StateAccessorInterface $stateAccessor |
||
| 346 | */ |
||
| 347 | 5 | public function setStateAccessor(StateAccessorInterface $stateAccessor) |
|
| 348 | { |
||
| 349 | 5 | $this->stateAccessor = $stateAccessor; |
|
| 350 | 5 | } |
|
| 351 | |||
| 352 | /** |
||
| 353 | * @{inheritDoc} |
||
| 354 | */ |
||
| 355 | 10 | public function setGraph($graph) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * @{inheritDoc} |
||
| 362 | */ |
||
| 363 | 155 | public function getGraph() |
|
| 367 | } |
||
| 368 |