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 | * Triggered right before the flow starts |
||
| 171 | * |
||
| 172 | * @return $this |
||
| 173 | */ |
||
| 174 | public function flowStart() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Triggered right after the flow stops |
||
| 183 | * |
||
| 184 | * @return $this |
||
| 185 | */ |
||
| 186 | public function flowEnd() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Let's be fast at incrementing while we are at it |
||
| 199 | * |
||
| 200 | * @param string $nodeHash |
||
| 201 | * |
||
| 202 | * @return array |
||
| 203 | */ |
||
| 204 | public function &getNodeStat($nodeHash) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Get/Generate Node Map |
||
| 211 | * |
||
| 212 | * @throws NodalFlowException |
||
| 213 | * |
||
| 214 | * @return array |
||
| 215 | */ |
||
| 216 | public function getNodeMap() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Get the latest Node stats |
||
| 242 | * |
||
| 243 | * @return array |
||
| 244 | */ |
||
| 245 | public function getStats() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param string $nodeHash |
||
| 272 | * @param string $key |
||
| 273 | * |
||
| 274 | * @return $this |
||
| 275 | */ |
||
| 276 | public function incrementNode($nodeHash, $key) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param string $key |
||
| 285 | * |
||
| 286 | * @return $this |
||
| 287 | */ |
||
| 288 | public function incrementFlow($key) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Resets Nodes stats, can be used prior to Flow's re-exec |
||
| 297 | * |
||
| 298 | * @return $this |
||
| 299 | */ |
||
| 300 | public function resetNodeStats() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Computes a human readable duration string from floating seconds |
||
| 315 | * |
||
| 316 | * @param float $seconds |
||
| 317 | * |
||
| 318 | * @return array<string,integer|string> |
||
| 319 | */ |
||
| 320 | public function duration($seconds) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @return $this |
||
| 343 | */ |
||
| 344 | protected function setRefs() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @return $this |
||
| 356 | */ |
||
| 357 | protected function initDefaults() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @return $this |
||
| 378 | */ |
||
| 379 | protected function resetTotals() |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Set additional increment keys, use : |
||
| 390 | * 'keyName' => int |
||
| 391 | * to add keyName as increment, starting at int |
||
| 392 | * or : |
||
| 393 | * 'keyName' => 'existingIncrement' |
||
| 394 | * to assign keyName as a reference to existingIncrement |
||
| 395 | * |
||
| 396 | * @param array $flowIncrements |
||
| 397 | * |
||
| 398 | * @throws NodalFlowException |
||
| 399 | * |
||
| 400 | * @return $this |
||
| 401 | */ |
||
| 402 | protected function setFlowIncrement(array $flowIncrements) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @param NodeInterface $node |
||
| 425 | * |
||
| 426 | * @throws NodalFlowException |
||
| 427 | * |
||
| 428 | * @return $this |
||
| 429 | */ |
||
| 430 | protected function setNodeIncrement(NodeInterface $node) |
||
| 452 | } |
||
| 453 |