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 | 96 | } 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 | 12 | } |
|
106 | |||
107 | 160 | $this->currentState = $this->getState($initialState); |
|
108 | |||
109 | 160 | $this->dispatcher->dispatch(FiniteEvents::INITIALIZE, new StateMachineEvent($this)); |
|
110 | 160 | } |
|
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | * |
||
115 | * @throws Exception\StateException |
||
116 | */ |
||
117 | 20 | public function apply($transitionName, array $parameters = array()) |
|
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | 55 | public function can($transition, array $parameters = array()) |
|
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | 185 | public function addState($state) |
|
167 | { |
||
168 | 185 | if (!$state instanceof StateInterface) { |
|
169 | 175 | $state = new State($state); |
|
170 | 105 | } |
|
171 | |||
172 | 185 | $this->states[$state->getName()] = $state; |
|
173 | 185 | } |
|
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 | 93 | } |
|
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 | 144 | } 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 | 122 | } 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 | 108 | } |
|
213 | 108 | } |
|
214 | 180 | } |
|
215 | |||
216 | /** |
||
217 | * {@inheritdoc} |
||
218 | */ |
||
219 | 160 | public function getTransition($name) |
|
232 | |||
233 | /** |
||
234 | * {@inheritdoc} |
||
235 | */ |
||
236 | 185 | public function getState($name) |
|
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 | 175 | public function getObject() |
|
283 | |||
284 | /** |
||
285 | * {@inheritdoc} |
||
286 | */ |
||
287 | 115 | 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() |
|
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 | 175 | public function getGraph() |
|
361 | |||
362 | /** |
||
363 | * {@inheritDoc} |
||
364 | */ |
||
365 | 10 | public function findStateWithProperty($property, $value = null) |
|
389 | |||
390 | /** |
||
391 | * Dispatches event for the transition |
||
392 | * |
||
393 | * @param TransitionInterface $transition |
||
394 | * @param TransitionEvent $event |
||
395 | * @param type $transitionState |
||
396 | */ |
||
397 | 50 | 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: