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 | 215 | public function __construct( |
|
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | 160 | public function initialize() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * {@inheritdoc} |
||
| 115 | * |
||
| 116 | * @throws Exception\StateException |
||
| 117 | */ |
||
| 118 | 20 | public function apply($transitionName, array $parameters = array()) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * {@inheritdoc} |
||
| 153 | */ |
||
| 154 | 55 | public function can($transition, array $parameters = array()) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * {@inheritdoc} |
||
| 178 | */ |
||
| 179 | 180 | public function addState($state) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * {@inheritdoc} |
||
| 190 | */ |
||
| 191 | 175 | public function addTransition($transition, $initialState = null, $finalState = null) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * {@inheritdoc} |
||
| 231 | */ |
||
| 232 | 160 | public function getTransition($name) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * {@inheritdoc} |
||
| 248 | */ |
||
| 249 | 180 | public function getState($name) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * {@inheritdoc} |
||
| 267 | */ |
||
| 268 | 5 | public function getTransitions() |
|
| 272 | |||
| 273 | /** |
||
| 274 | * {@inheritdoc} |
||
| 275 | */ |
||
| 276 | 5 | public function getStates() |
|
| 280 | |||
| 281 | /** |
||
| 282 | * {@inheritdoc} |
||
| 283 | */ |
||
| 284 | 150 | public function setObject($object) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * {@inheritdoc} |
||
| 291 | */ |
||
| 292 | 175 | public function getObject() |
|
| 296 | |||
| 297 | /** |
||
| 298 | * {@inheritdoc} |
||
| 299 | */ |
||
| 300 | 115 | public function getCurrentState() |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Find and return the Initial state if exists. |
||
| 307 | * |
||
| 308 | * @return string |
||
| 309 | * |
||
| 310 | * @throws Exception\StateException |
||
| 311 | */ |
||
| 312 | 20 | protected function findInitialState() |
|
| 326 | |||
| 327 | /** |
||
| 328 | * @param EventDispatcherInterface $dispatcher |
||
| 329 | */ |
||
| 330 | public function setDispatcher(EventDispatcherInterface $dispatcher) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @return EventDispatcherInterface |
||
| 337 | */ |
||
| 338 | 5 | public function getDispatcher() |
|
| 342 | |||
| 343 | /** |
||
| 344 | * @param StateAccessorInterface $stateAccessor |
||
| 345 | */ |
||
| 346 | 10 | public function setStateAccessor(StateAccessorInterface $stateAccessor) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * {@inheritdoc} |
||
| 353 | */ |
||
| 354 | 15 | public function setGraph($graph) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * {@inheritdoc} |
||
| 361 | */ |
||
| 362 | 175 | public function getGraph() |
|
| 366 | |||
| 367 | /** |
||
| 368 | * {@inheritDoc} |
||
| 369 | */ |
||
| 370 | 10 | public function findStateWithProperty($property, $value = null) |
|
| 394 | } |
||
| 395 |