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 |
||
| 24 | class StateMachine implements StateMachineInterface |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * The stateful object. |
||
| 28 | * |
||
| 29 | * @var object |
||
| 30 | */ |
||
| 31 | protected $object; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The available states. |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $states = array(); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The available transitions. |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $transitions = array(); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The current state. |
||
| 49 | * |
||
| 50 | * @var StateInterface |
||
| 51 | */ |
||
| 52 | protected $currentState; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var EventDispatcherInterface |
||
| 56 | */ |
||
| 57 | protected $dispatcher; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var StateAccessorInterface |
||
| 61 | */ |
||
| 62 | protected $stateAccessor; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $graph; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | protected $callbacks = []; |
||
| 73 | 215 | ||
| 74 | /** |
||
| 75 | * @param object $object |
||
| 76 | * @param EventDispatcherInterface $dispatcher |
||
| 77 | * @param StateAccessorInterface $stateAccessor |
||
| 78 | 215 | */ |
|
| 79 | 215 | public function __construct( |
|
| 88 | 160 | ||
| 89 | /** |
||
| 90 | * {@inheritdoc} |
||
| 91 | */ |
||
| 92 | public function initialize() |
||
| 118 | 20 | ||
| 119 | /** |
||
| 120 | 20 | * {@inheritdoc} |
|
| 121 | 20 | * |
|
| 122 | 20 | * @throws Exception\StateException |
|
| 123 | 5 | */ |
|
| 124 | 5 | public function apply($transitionName, array $parameters = array()) |
|
| 148 | 55 | ||
| 149 | /** |
||
| 150 | 55 | * {@inheritdoc} |
|
| 151 | 5 | */ |
|
| 152 | public function can($transition, array $parameters = array()) |
||
| 169 | 180 | ||
| 170 | 175 | /** |
|
| 171 | 140 | * {@inheritdoc} |
|
| 172 | */ |
||
| 173 | 180 | public function addState($state) |
|
| 181 | 175 | ||
| 182 | /** |
||
| 183 | * {@inheritdoc} |
||
| 184 | */ |
||
| 185 | public function addTransition($transition, $initialState = null, $finalState = null) |
||
| 222 | 160 | ||
| 223 | 155 | /** |
|
| 224 | 155 | * {@inheritdoc} |
|
| 225 | 124 | */ |
|
| 226 | 155 | public function getTransition($name) |
|
| 239 | 180 | ||
| 240 | /** |
||
| 241 | 180 | * {@inheritdoc} |
|
| 242 | 95 | */ |
|
| 243 | 95 | public function getState($name) |
|
| 258 | 5 | ||
| 259 | /** |
||
| 260 | * {@inheritdoc} |
||
| 261 | */ |
||
| 262 | public function getTransitions() |
||
| 266 | 5 | ||
| 267 | /** |
||
| 268 | * {@inheritdoc} |
||
| 269 | */ |
||
| 270 | public function getStates() |
||
| 274 | 150 | ||
| 275 | 150 | /** |
|
| 276 | * {@inheritdoc} |
||
| 277 | */ |
||
| 278 | public function setObject($object) |
||
| 282 | 175 | ||
| 283 | /** |
||
| 284 | * {@inheritdoc} |
||
| 285 | */ |
||
| 286 | public function getObject() |
||
| 290 | 115 | ||
| 291 | /** |
||
| 292 | * {@inheritdoc} |
||
| 293 | */ |
||
| 294 | public function getCurrentState() |
||
| 298 | |||
| 299 | /** |
||
| 300 | 20 | * Find and return the Initial state if exists. |
|
| 301 | * |
||
| 302 | 20 | * @return string |
|
| 303 | 20 | * |
|
| 304 | 20 | * @throws Exception\StateException |
|
| 305 | */ |
||
| 306 | protected function findInitialState() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @param EventDispatcherInterface $dispatcher |
||
| 323 | */ |
||
| 324 | public function setDispatcher(EventDispatcherInterface $dispatcher) |
||
| 328 | 5 | ||
| 329 | /** |
||
| 330 | * @return EventDispatcherInterface |
||
| 331 | */ |
||
| 332 | public function getDispatcher() |
||
| 336 | 10 | ||
| 337 | 10 | /** |
|
| 338 | * @param StateAccessorInterface $stateAccessor |
||
| 339 | */ |
||
| 340 | public function setStateAccessor(StateAccessorInterface $stateAccessor) |
||
| 344 | 15 | ||
| 345 | 15 | /** |
|
| 346 | * {@inheritdoc} |
||
| 347 | */ |
||
| 348 | public function hasStateAccessor() |
||
| 352 | 175 | ||
| 353 | /** |
||
| 354 | * {@inheritdoc} |
||
| 355 | */ |
||
| 356 | public function setGraph($graph) |
||
| 360 | 10 | ||
| 361 | 8 | /** |
|
| 362 | * {@inheritdoc} |
||
| 363 | 10 | */ |
|
| 364 | 10 | public function getGraph() |
|
| 368 | 10 | ||
| 369 | 10 | /** |
|
| 370 | * {@inheritDoc} |
||
| 371 | */ |
||
| 372 | 10 | public function findStateWithProperty($property, $value = null) |
|
| 396 | 8 | ||
| 397 | 50 | /** |
|
| 398 | * Dispatches event for the transition |
||
| 399 | * |
||
| 400 | * @param TransitionInterface $transition |
||
| 401 | * @param TransitionEvent $event |
||
| 402 | * @param type $transitionState |
||
| 403 | */ |
||
| 404 | private function dispatchTransitionEvent(TransitionInterface $transition, TransitionEvent $event, $transitionState) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @return array |
||
| 415 | */ |
||
| 416 | public function getCallbacks() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @return array |
||
| 423 | */ |
||
| 424 | public function setCallbacks($callbacks) |
||
| 428 | } |
||
| 429 |
The class complexity is the sum of the complexity of all methods. A very high value is usually an indication that your class does not follow the single reponsibility principle and does more than one job.
Some resources for further reading:
You can also find more detailed suggestions for refactoring in the “Code” section of your repository.