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 = []; |
||
38 | |||
39 | /** |
||
40 | * The available transitions. |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $transitions = []; |
||
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 | 220 | public function __construct( |
|
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | 160 | public function initialize() |
|
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | * |
||
118 | 20 | * @throws Exception\StateException |
|
119 | */ |
||
120 | 20 | public function apply($transitionName, array $parameters = []) |
|
121 | 20 | { |
|
122 | 20 | $transition = $this->getTransition($transitionName); |
|
123 | 5 | $event = new TransitionEvent($this->getCurrentState(), $transition, $this, $parameters); |
|
124 | 5 | if (!$this->can($transition, $parameters)) { |
|
125 | 5 | throw new Exception\StateException( |
|
126 | 5 | sprintf( |
|
127 | 5 | 'The "%s" transition can not be applied to the "%s" state of object "%s" with graph "%s".', |
|
128 | 5 | $transition->getName(), |
|
129 | 2 | $this->currentState->getName(), |
|
130 | $this->getObject() ? get_class($this->getObject()) : 'undefined', |
||
131 | $this->getGraph() |
||
132 | 20 | ) |
|
133 | ); |
||
134 | 20 | } |
|
135 | 20 | ||
136 | 20 | $this->dispatchTransitionEvent($transition, $event, FiniteEvents::PRE_TRANSITION); |
|
137 | |||
138 | 20 | $returnValue = $transition->process($this); |
|
139 | $this->stateAccessor->setState($this->object, $transition->getState()); |
||
140 | 20 | $this->currentState = $this->getState($transition->getState()); |
|
141 | |||
142 | $this->dispatchTransitionEvent($transition, $event, FiniteEvents::POST_TRANSITION); |
||
143 | |||
144 | return $returnValue; |
||
145 | } |
||
146 | 55 | ||
147 | /** |
||
148 | 55 | * {@inheritdoc} |
|
149 | */ |
||
150 | 55 | public function can($transition, array $parameters = []) |
|
167 | 185 | ||
168 | /** |
||
169 | 185 | * {@inheritdoc} |
|
170 | 175 | */ |
|
171 | 70 | public function addState($state) |
|
179 | 180 | ||
180 | /** |
||
181 | 180 | * {@inheritdoc} |
|
182 | */ |
||
183 | public function addTransition($transition, $initialState = null, $finalState = null) |
||
220 | 160 | ||
221 | /** |
||
222 | 160 | * {@inheritdoc} |
|
223 | 155 | */ |
|
224 | 155 | public function getTransition($name) |
|
239 | 185 | ||
240 | /** |
||
241 | 185 | * {@inheritdoc} |
|
242 | 95 | */ |
|
243 | 95 | public function getState($name) |
|
260 | |||
261 | /** |
||
262 | * {@inheritdoc} |
||
263 | */ |
||
264 | 5 | public function getTransitions() |
|
268 | |||
269 | /** |
||
270 | * {@inheritdoc} |
||
271 | */ |
||
272 | 150 | public function getStates() |
|
276 | |||
277 | /** |
||
278 | * {@inheritdoc} |
||
279 | */ |
||
280 | 175 | public function setObject($object) |
|
284 | |||
285 | /** |
||
286 | * {@inheritdoc} |
||
287 | */ |
||
288 | 115 | public function getObject() |
|
292 | |||
293 | /** |
||
294 | * {@inheritdoc} |
||
295 | */ |
||
296 | public function getCurrentState() |
||
300 | 20 | ||
301 | /** |
||
302 | 20 | * Find and return the Initial state if exists. |
|
303 | 20 | * |
|
304 | 20 | * @return string |
|
305 | * |
||
306 | * @throws Exception\StateException |
||
307 | */ |
||
308 | protected function findInitialState() |
||
309 | { |
||
310 | foreach ($this->states as $state) { |
||
311 | if (State::TYPE_INITIAL === $state->getType()) { |
||
312 | return $state->getName(); |
||
313 | } |
||
314 | } |
||
315 | |||
316 | throw new Exception\StateException( |
||
317 | sprintf( |
||
318 | 'No initial state found on object "%s" with graph "%s".', |
||
319 | $this->getObject() ? get_class($this->getObject()) : 'undefined', |
||
320 | $this->getGraph() |
||
321 | ) |
||
322 | ); |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | 5 | * @param EventDispatcherInterface $dispatcher |
|
327 | */ |
||
328 | 5 | public function setDispatcher(EventDispatcherInterface $dispatcher) |
|
332 | |||
333 | /** |
||
334 | 5 | * @return EventDispatcherInterface |
|
335 | */ |
||
336 | 5 | public function getDispatcher() |
|
340 | |||
341 | /** |
||
342 | 15 | * @param StateAccessorInterface $stateAccessor |
|
343 | */ |
||
344 | 15 | public function setStateAccessor(StateAccessorInterface $stateAccessor) |
|
348 | |||
349 | public function getStateAccessor(): ?StateAccessorInterface |
||
350 | 20 | { |
|
353 | 20 | ||
354 | /** |
||
355 | * {@inheritdoc} |
||
356 | */ |
||
357 | public function hasStateAccessor() |
||
361 | |||
362 | /** |
||
363 | * {@inheritdoc} |
||
364 | */ |
||
365 | public function setGraph($graph) |
||
369 | 10 | ||
370 | /** |
||
371 | 10 | * {@inheritdoc} |
|
372 | 10 | */ |
|
373 | 10 | public function getGraph() |
|
377 | 10 | ||
378 | /** |
||
379 | * {@inheritDoc} |
||
380 | 10 | */ |
|
381 | 5 | public function findStateWithProperty($property, $value = null) |
|
405 | 50 | ||
406 | /** |
||
407 | * Dispatches event for the transition |
||
408 | * |
||
409 | * @param TransitionInterface $transition |
||
410 | * @param TransitionEvent $event |
||
411 | * @param string $transitionState |
||
412 | */ |
||
413 | private function dispatchTransitionEvent(TransitionInterface $transition, TransitionEvent $event, $transitionState) |
||
421 | } |
||
422 |
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: