Complex classes like FlowMap 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 FlowMap, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class FlowMap implements FlowMapInterface |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Flow map |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected $nodeMap = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var NodeInterface[] |
||
| 34 | */ |
||
| 35 | protected $reverseMap = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The default Node Map values |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $nodeMapDefault = [ |
||
| 43 | 'class' => null, |
||
| 44 | 'flowId' => null, |
||
| 45 | 'hash' => null, |
||
| 46 | 'index' => null, |
||
| 47 | 'isATraversable' => null, |
||
| 48 | 'isAReturningVal' => null, |
||
| 49 | 'isAFlow' => null, |
||
| 50 | ]; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The default Node stats values |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $nodeIncrements = [ |
||
| 58 | 'num_exec' => 0, |
||
| 59 | 'num_iterate' => 0, |
||
| 60 | 'num_break' => 0, |
||
| 61 | 'num_continue' => 0, |
||
| 62 | ]; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The Flow map default values |
||
| 66 | * |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | protected $flowMapDefault = [ |
||
| 70 | 'class' => null, |
||
| 71 | 'id' => null, |
||
| 72 | 'start' => null, |
||
| 73 | 'end' => null, |
||
| 74 | 'elapsed' => null, |
||
| 75 | 'duration' => null, |
||
| 76 | 'mib' => null, |
||
| 77 | ]; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | protected $incrementTotals = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var array |
||
| 86 | */ |
||
| 87 | protected $flowIncrements; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | protected $flowStats; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var FlowRegistryInterface |
||
| 96 | */ |
||
| 97 | protected $registry; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var array |
||
| 101 | */ |
||
| 102 | protected $registryData = []; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var FlowInterface |
||
| 106 | */ |
||
| 107 | protected $flow; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var string |
||
| 111 | */ |
||
| 112 | protected $flowId; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Instantiate a Flow Status |
||
| 116 | * |
||
| 117 | * @param FlowInterface $flow |
||
| 118 | * @param array $flowIncrements |
||
| 119 | */ |
||
| 120 | public function __construct(FlowInterface $flow, array $flowIncrements = []) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * If you don't feel like doing this at home, I completely |
||
| 130 | * understand, I'd be very happy to hear about a better and |
||
| 131 | * more efficient way |
||
| 132 | */ |
||
| 133 | public function __wakeup() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param NodeInterface $node |
||
| 141 | * @param int $index |
||
| 142 | * |
||
| 143 | * @throws NodalFlowException |
||
| 144 | */ |
||
| 145 | public function register(NodeInterface $node, $index) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param string $nodeId |
||
| 171 | * |
||
| 172 | * @return int|null |
||
| 173 | */ |
||
| 174 | public function getNodeIndex($nodeId) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Triggered right before the flow starts |
||
| 181 | * |
||
| 182 | * @return $this |
||
| 183 | */ |
||
| 184 | public function flowStart() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Triggered right after the flow stops |
||
| 193 | * |
||
| 194 | * @return $this |
||
| 195 | */ |
||
| 196 | public function flowEnd() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Let's be fast at incrementing while we are at it |
||
| 209 | * |
||
| 210 | * @param string $nodeHash |
||
| 211 | * |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | public function &getNodeStat($nodeHash) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get/Generate Node Map |
||
| 221 | * |
||
| 222 | * @throws NodalFlowException |
||
| 223 | * |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | public function getNodeMap() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Get the latest Node stats |
||
| 252 | * |
||
| 253 | * @return array |
||
| 254 | */ |
||
| 255 | public function getStats() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param string $nodeHash |
||
| 282 | * @param string $key |
||
| 283 | * |
||
| 284 | * @return $this |
||
| 285 | */ |
||
| 286 | public function incrementNode($nodeHash, $key) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param string $key |
||
| 295 | * |
||
| 296 | * @return $this |
||
| 297 | */ |
||
| 298 | public function incrementFlow($key) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Resets Nodes stats, can be used prior to Flow's re-exec |
||
| 307 | * |
||
| 308 | * @return $this |
||
| 309 | */ |
||
| 310 | public function resetNodeStats() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Computes a human readable duration string from floating seconds |
||
| 325 | * |
||
| 326 | * @param float $seconds |
||
| 327 | * |
||
| 328 | * @return array<string,integer|string> |
||
| 329 | */ |
||
| 330 | public function duration($seconds) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @return $this |
||
| 353 | */ |
||
| 354 | protected function setRefs() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @return $this |
||
| 366 | */ |
||
| 367 | protected function initDefaults() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @return $this |
||
| 388 | */ |
||
| 389 | protected function resetTotals() |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Set additional increment keys, use : |
||
| 400 | * 'keyName' => int |
||
| 401 | * to add keyName as increment, starting at int |
||
| 402 | * or : |
||
| 403 | * 'keyName' => 'existingIncrement' |
||
| 404 | * to assign keyName as a reference to existingIncrement |
||
| 405 | * |
||
| 406 | * @param array $flowIncrements |
||
| 407 | * |
||
| 408 | * @throws NodalFlowException |
||
| 409 | * |
||
| 410 | * @return $this |
||
| 411 | */ |
||
| 412 | protected function setFlowIncrement(array $flowIncrements) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @param NodeInterface $node |
||
| 435 | * |
||
| 436 | * @throws NodalFlowException |
||
| 437 | * |
||
| 438 | * @return $this |
||
| 439 | */ |
||
| 440 | protected function setNodeIncrement(NodeInterface $node) |
||
| 462 | } |
||
| 463 |