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 |
||
| 22 | class StateMachine implements StateMachineInterface |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * The stateful object. |
||
| 26 | * |
||
| 27 | * @var object |
||
| 28 | */ |
||
| 29 | protected $object; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The available states. |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $states = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The available transitions. |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $transitions = array(); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The current state. |
||
| 47 | * |
||
| 48 | * @var StateInterface |
||
| 49 | */ |
||
| 50 | protected $currentState; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var StateMachineDispatcher |
||
| 54 | */ |
||
| 55 | protected $dispatcher; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var StateAccessorInterface |
||
| 59 | */ |
||
| 60 | protected $stateAccessor; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $graph; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param object $object |
||
| 69 | * @param StateMachineDispatcher $dispatcher |
||
| 70 | * @param StateAccessorInterface $stateAccessor |
||
| 71 | */ |
||
| 72 | 185 | public function __construct( |
|
| 73 | $object = null, |
||
| 74 | StateMachineDispatcher $dispatcher = null, |
||
| 75 | StateAccessorInterface $stateAccessor = null |
||
| 76 | ) { |
||
| 77 | 185 | $this->object = $object; |
|
| 78 | 185 | $this->dispatcher = $dispatcher ?: new StateMachineDispatcher(); |
|
| 79 | 185 | $this->stateAccessor = $stateAccessor ?: new PropertyPathStateAccessor(); |
|
| 80 | 185 | } |
|
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritdoc} |
||
| 84 | */ |
||
| 85 | 160 | public function initialize() |
|
| 86 | { |
||
| 87 | 160 | if (null === $this->object) { |
|
| 88 | throw new Exception\ObjectException('No object bound to the State Machine'); |
||
| 89 | } |
||
| 90 | |||
| 91 | try { |
||
| 92 | 160 | $initialState = $this->stateAccessor->getState($this->object); |
|
| 93 | } catch (Exception\NoSuchPropertyException $e) { |
||
| 94 | throw new Exception\ObjectException(sprintf( |
||
| 95 | 'StateMachine can\'t be initialized because the defined property_path of object "%s" does not exist.', |
||
| 96 | $this->getObject() ? get_class($this->getObject()) : null |
||
| 97 | ), $e->getCode(), $e); |
||
| 98 | } |
||
| 99 | |||
| 100 | 160 | if (null === $initialState) { |
|
| 101 | 20 | $initialState = $this->findInitialState(); |
|
| 102 | 20 | $this->stateAccessor->setState($this->object, $initialState); |
|
| 103 | |||
| 104 | 20 | $this->dispatcher->dispatch(FiniteEvents::SET_INITIAL_STATE, new StateMachineEvent($this)); |
|
| 105 | } |
||
| 106 | |||
| 107 | 158 | $this->currentState = $this->getState($initialState); |
|
| 108 | |||
| 109 | 158 | $this->dispatcher->dispatch(FiniteEvents::INITIALIZE, new StateMachineEvent($this)); |
|
| 110 | 144 | } |
|
| 111 | |||
| 112 | /** |
||
| 113 | * {@inheritdoc} |
||
| 114 | * |
||
| 115 | * @throws Exception\StateException |
||
| 116 | */ |
||
| 117 | 19 | public function apply($transitionName, array $parameters = array()) |
|
| 118 | { |
||
| 119 | 19 | $transition = $this->getTransition($transitionName); |
|
| 120 | 19 | $event = new TransitionEvent($this->getCurrentState(), $transition, $this, $parameters); |
|
| 121 | 19 | if (!$this->can($transition, $parameters)) { |
|
| 122 | 5 | throw new Exception\StateException(sprintf( |
|
| 123 | 5 | 'The "%s" transition can not be applied to the "%s" state of object "%s" with graph "%s".', |
|
| 124 | 5 | $transition->getName(), |
|
| 125 | 5 | $this->currentState->getName(), |
|
| 126 | 5 | $this->getObject() ? get_class($this->getObject()) : null, |
|
| 127 | 5 | $this->getGraph() |
|
| 128 | )); |
||
| 129 | } |
||
| 130 | |||
| 131 | 19 | $this->dispatchTransitionEvent($transition, $event, FiniteEvents::PRE_TRANSITION); |
|
| 132 | |||
| 133 | 19 | $returnValue = $transition->process($this); |
|
| 134 | 19 | $this->stateAccessor->setState($this->object, $transition->getState()); |
|
| 135 | 19 | $this->currentState = $this->getState($transition->getState()); |
|
| 136 | |||
| 137 | 19 | $this->dispatchTransitionEvent($transition, $event, FiniteEvents::POST_TRANSITION); |
|
| 138 | |||
| 139 | 19 | return $returnValue; |
|
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * {@inheritdoc} |
||
| 144 | */ |
||
| 145 | 52 | public function can($transition, array $parameters = array()) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * {@inheritdoc} |
||
| 165 | */ |
||
| 166 | 185 | public function addState($state) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * {@inheritdoc} |
||
| 177 | */ |
||
| 178 | 180 | public function addTransition($transition, $initialState = null, $finalState = null) |
|
| 179 | { |
||
| 180 | 180 | if ((null === $initialState || null === $finalState) && !$transition instanceof TransitionInterface) { |
|
| 181 | throw new \InvalidArgumentException( |
||
| 182 | 'You must provide a TransitionInterface instance or the $transition, '. |
||
| 183 | '$initialState and $finalState parameters' |
||
| 184 | ); |
||
| 185 | } |
||
| 186 | // If transition isn't a TransitionInterface instance, we create one from the states date |
||
| 187 | 180 | if (!$transition instanceof TransitionInterface) { |
|
| 188 | try { |
||
| 189 | 155 | $transition = $this->getTransition($transition); |
|
| 190 | 155 | } catch (Exception\TransitionException $e) { |
|
| 191 | 155 | $transition = new Transition($transition, $initialState, $finalState); |
|
|
|
|||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | 180 | $this->transitions[$transition->getName()] = $transition; |
|
| 196 | |||
| 197 | // We add missings states to the State Machine |
||
| 198 | try { |
||
| 199 | 180 | $this->getState($transition->getState()); |
|
| 200 | 90 | } catch (Exception\StateException $e) { |
|
| 201 | 90 | $this->addState($transition->getState()); |
|
| 202 | } |
||
| 203 | 180 | foreach ($transition->getInitialStates() as $state) { |
|
| 204 | try { |
||
| 205 | 180 | $this->getState($state); |
|
| 206 | 35 | } catch (Exception\StateException $e) { |
|
| 207 | 35 | $this->addState($state); |
|
| 208 | } |
||
| 209 | 180 | $state = $this->getState($state); |
|
| 210 | 180 | if ($state instanceof State) { |
|
| 211 | 180 | $state->addTransition($transition); |
|
| 212 | } |
||
| 213 | } |
||
| 214 | 180 | } |
|
| 215 | |||
| 216 | /** |
||
| 217 | * {@inheritdoc} |
||
| 218 | */ |
||
| 219 | 159 | public function getTransition($name) |
|
| 220 | { |
||
| 221 | 159 | if (!isset($this->transitions[$name])) { |
|
| 222 | 155 | throw new Exception\TransitionException(sprintf( |
|
| 223 | 155 | 'Unable to find a transition called "%s" on object "%s" with graph "%s".', |
|
| 224 | 155 | $name, |
|
| 225 | 155 | $this->getObject() ? get_class($this->getObject()) : null, |
|
| 226 | 155 | $this->getGraph() |
|
| 227 | )); |
||
| 228 | } |
||
| 229 | |||
| 230 | 51 | return $this->transitions[$name]; |
|
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * {@inheritdoc} |
||
| 235 | */ |
||
| 236 | 185 | public function getState($name) |
|
| 237 | { |
||
| 238 | 185 | $name = (string) $name; |
|
| 239 | |||
| 240 | 185 | if (!isset($this->states[$name])) { |
|
| 241 | 95 | throw new Exception\StateException(sprintf( |
|
| 242 | 95 | 'Unable to find a state called "%s" on object "%s" with graph "%s".', |
|
| 243 | 95 | $name, |
|
| 244 | 95 | $this->getObject() ? get_class($this->getObject()) : null, |
|
| 245 | 95 | $this->getGraph() |
|
| 246 | )); |
||
| 247 | } |
||
| 248 | |||
| 249 | 185 | return $this->states[$name]; |
|
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * {@inheritdoc} |
||
| 254 | */ |
||
| 255 | 5 | public function getTransitions() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * {@inheritdoc} |
||
| 262 | */ |
||
| 263 | 5 | public function getStates() |
|
| 267 | |||
| 268 | /** |
||
| 269 | * {@inheritdoc} |
||
| 270 | */ |
||
| 271 | 150 | public function setObject($object) |
|
| 272 | { |
||
| 273 | 150 | $this->object = $object; |
|
| 274 | 150 | } |
|
| 275 | |||
| 276 | /** |
||
| 277 | * {@inheritdoc} |
||
| 278 | */ |
||
| 279 | 174 | public function getObject() |
|
| 283 | |||
| 284 | /** |
||
| 285 | * {@inheritdoc} |
||
| 286 | */ |
||
| 287 | 100 | public function getCurrentState() |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Find and return the Initial state if exists. |
||
| 294 | * |
||
| 295 | * @return string |
||
| 296 | * |
||
| 297 | * @throws Exception\StateException |
||
| 298 | */ |
||
| 299 | 20 | protected function findInitialState() |
|
| 300 | { |
||
| 301 | 20 | foreach ($this->states as $state) { |
|
| 302 | 20 | if (State::TYPE_INITIAL === $state->getType()) { |
|
| 303 | 20 | return $state->getName(); |
|
| 304 | } |
||
| 305 | } |
||
| 306 | |||
| 307 | throw new Exception\StateException(sprintf( |
||
| 308 | 'No initial state found on object "%s" with graph "%s".', |
||
| 309 | $this->getObject() ? get_class($this->getObject()) : null, |
||
| 310 | $this->getGraph() |
||
| 311 | )); |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param StateMachineDispatcher $dispatcher |
||
| 316 | */ |
||
| 317 | public function setDispatcher(StateMachineDispatcher $dispatcher) |
||
| 318 | { |
||
| 319 | $this->dispatcher = $dispatcher; |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @return StateMachineDispatcher |
||
| 324 | */ |
||
| 325 | 5 | public function getDispatcher() |
|
| 329 | |||
| 330 | /** |
||
| 331 | * @param StateAccessorInterface $stateAccessor |
||
| 332 | */ |
||
| 333 | 5 | public function setStateAccessor(StateAccessorInterface $stateAccessor) |
|
| 334 | { |
||
| 335 | 5 | $this->stateAccessor = $stateAccessor; |
|
| 336 | 5 | } |
|
| 337 | |||
| 338 | /** |
||
| 339 | * {@inheritdoc} |
||
| 340 | */ |
||
| 341 | 15 | public function hasStateAccessor() |
|
| 345 | |||
| 346 | /** |
||
| 347 | * {@inheritdoc} |
||
| 348 | */ |
||
| 349 | 20 | public function setGraph($graph) |
|
| 350 | { |
||
| 351 | 20 | $this->graph = $graph; |
|
| 352 | 20 | } |
|
| 353 | |||
| 354 | /** |
||
| 355 | * {@inheritdoc} |
||
| 356 | */ |
||
| 357 | 174 | public function getGraph() |
|
| 361 | |||
| 362 | /** |
||
| 363 | * {@inheritDoc} |
||
| 364 | */ |
||
| 365 | 10 | public function findStateWithProperty($property, $value = null) |
|
| 366 | { |
||
| 367 | 10 | return array_keys( |
|
| 368 | 10 | array_map( |
|
| 369 | 6 | function (State $state) { |
|
| 370 | 10 | return $state->getName(); |
|
| 371 | 10 | }, |
|
| 372 | 10 | array_filter( |
|
| 373 | 10 | $this->states, |
|
| 374 | 10 | function (State $state) use ($property, $value) { |
|
| 375 | 10 | if (!$state->has($property)) { |
|
| 376 | 10 | return false; |
|
| 377 | } |
||
| 378 | |||
| 379 | 10 | if (null !== $value && $state->get($property) !== $value) { |
|
| 380 | 5 | return false; |
|
| 381 | } |
||
| 382 | |||
| 383 | 10 | return true; |
|
| 384 | 10 | } |
|
| 385 | ) |
||
| 386 | ) |
||
| 387 | ); |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Dispatches event for the transition |
||
| 392 | * |
||
| 393 | * @param TransitionInterface $transition |
||
| 394 | * @param TransitionEvent $event |
||
| 395 | * @param type $transitionState |
||
| 396 | */ |
||
| 397 | 47 | private function dispatchTransitionEvent(TransitionInterface $transition, TransitionEvent $event, $transitionState) |
|
| 405 | } |
||
| 406 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: