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 | * @throws NodalFlowException |
||
| 121 | */ |
||
| 122 | public function __construct(FlowInterface $flow, array $flowIncrements = []) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * If you don't feel like doing this at home, I completely |
||
| 132 | * understand, I'd be very happy to hear about a better and |
||
| 133 | * more efficient way |
||
| 134 | */ |
||
| 135 | public function __wakeup() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param NodeInterface $node |
||
| 143 | * @param int $index |
||
| 144 | * |
||
| 145 | * @throws NodalFlowException |
||
| 146 | * |
||
| 147 | * @return $this |
||
| 148 | */ |
||
| 149 | public function register(NodeInterface $node, $index) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param string $nodeId |
||
| 177 | * |
||
| 178 | * @return int|null |
||
| 179 | */ |
||
| 180 | public function getNodeIndex($nodeId) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Triggered right before the flow starts |
||
| 187 | * |
||
| 188 | * @return $this |
||
| 189 | */ |
||
| 190 | public function flowStart() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Triggered right after the flow stops |
||
| 199 | * |
||
| 200 | * @return $this |
||
| 201 | */ |
||
| 202 | public function flowEnd() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Let's be fast at incrementing while we are at it |
||
| 215 | * |
||
| 216 | * @param string $nodeHash |
||
| 217 | * |
||
| 218 | * @return array |
||
| 219 | */ |
||
| 220 | public function &getNodeStat($nodeHash) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Get/Generate Node Map |
||
| 227 | * |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | public function getNodeMap() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Get the latest Node stats |
||
| 245 | * |
||
| 246 | * @return array<string,integer|string> |
||
| 247 | */ |
||
| 248 | public function getStats() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param string $nodeHash |
||
| 280 | * @param string $key |
||
| 281 | * |
||
| 282 | * @return $this |
||
| 283 | */ |
||
| 284 | public function incrementNode($nodeHash, $key) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param string $key |
||
| 293 | * |
||
| 294 | * @return $this |
||
| 295 | */ |
||
| 296 | public function incrementFlow($key) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Resets Nodes stats, can be used prior to Flow's re-exec |
||
| 305 | * |
||
| 306 | * @return $this |
||
| 307 | */ |
||
| 308 | public function resetNodeStats() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Computes a human readable duration string from floating seconds |
||
| 323 | * |
||
| 324 | * @param float $seconds |
||
| 325 | * |
||
| 326 | * @return array<string,integer|string> |
||
| 327 | */ |
||
| 328 | public function duration($seconds) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @return $this |
||
| 351 | */ |
||
| 352 | protected function setRefs() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @return $this |
||
| 364 | */ |
||
| 365 | protected function initDefaults() |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @return $this |
||
| 386 | */ |
||
| 387 | protected function resetTotals() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Set additional increment keys, use : |
||
| 398 | * 'keyName' => int |
||
| 399 | * to add keyName as increment, starting at int |
||
| 400 | * or : |
||
| 401 | * 'keyName' => 'existingIncrement' |
||
| 402 | * to assign keyName as a reference to existingIncrement |
||
| 403 | * |
||
| 404 | * @param array $flowIncrements |
||
| 405 | * |
||
| 406 | * @throws NodalFlowException |
||
| 407 | * |
||
| 408 | * @return $this |
||
| 409 | */ |
||
| 410 | protected function setFlowIncrement(array $flowIncrements) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @param NodeInterface $node |
||
| 433 | * |
||
| 434 | * @throws NodalFlowException |
||
| 435 | * |
||
| 436 | * @return $this |
||
| 437 | */ |
||
| 438 | protected function setNodeIncrement(NodeInterface $node) |
||
| 460 | } |
||
| 461 |