| Total Complexity | 45 |
| Total Lines | 442 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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.
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 = []) |
||
| 123 | { |
||
| 124 | $this->flow = $flow; |
||
| 125 | $this->flowId = $this->flow->getId(); |
||
| 126 | $this->registry = (new FlowRegistry)->registerFlow($flow); |
||
| 127 | $this->initDefaults()->setRefs()->setFlowIncrement($flowIncrements); |
||
| 128 | } |
||
| 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() |
||
| 136 | { |
||
| 137 | $this->registry->load($this->flow, $this->registryData); |
||
| 138 | $this->setRefs(); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param NodeInterface $node |
||
| 143 | * @param int $index |
||
| 144 | * @param bool $replace |
||
| 145 | * |
||
| 146 | * @throws NodalFlowException |
||
| 147 | * |
||
| 148 | * @return $this |
||
| 149 | */ |
||
| 150 | public function register(NodeInterface $node, $index, $replace = false) |
||
| 151 | { |
||
| 152 | if (!$replace) { |
||
| 153 | $this->registry->registerNode($node); |
||
| 154 | } else { |
||
| 155 | $this->registry->removeNode($this->reverseMap[$index]); |
||
| 156 | } |
||
| 157 | |||
| 158 | $nodeId = $node->getId(); |
||
| 159 | $this->nodeMap[$nodeId] = array_replace($this->nodeMapDefault, [ |
||
| 160 | 'class' => get_class($node), |
||
| 161 | 'flowId' => $this->flowId, |
||
| 162 | 'hash' => $nodeId, |
||
| 163 | 'index' => $index, |
||
| 164 | 'isATraversable' => $node->isTraversable(), |
||
| 165 | 'isAReturningVal' => $node->isReturningVal(), |
||
| 166 | 'isAFlow' => $node->isFlow(), |
||
| 167 | ], $this->nodeIncrements); |
||
| 168 | |||
| 169 | $this->setNodeIncrement($node); |
||
| 170 | |||
| 171 | if (isset($this->reverseMap[$index])) { |
||
| 172 | // replacing a node, maintain nodeMap accordingly |
||
| 173 | unset($this->nodeMap[$this->reverseMap[$index]->getId()], $this->reverseMap[$index]); |
||
| 174 | } |
||
| 175 | |||
| 176 | $this->reverseMap[$index] = $node; |
||
| 177 | |||
| 178 | return $this; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param string $nodeId |
||
| 183 | * |
||
| 184 | * @return int|null |
||
| 185 | */ |
||
| 186 | public function getNodeIndex($nodeId) |
||
| 187 | { |
||
| 188 | return isset($this->nodeMap[$nodeId]) ? $this->nodeMap[$nodeId]['index'] : null; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Triggered right before the flow starts |
||
| 193 | * |
||
| 194 | * @return $this |
||
| 195 | */ |
||
| 196 | public function flowStart() |
||
| 197 | { |
||
| 198 | $this->flowStats['start'] = microtime(true); |
||
| 199 | |||
| 200 | return $this; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Triggered right after the flow stops |
||
| 205 | * |
||
| 206 | * @return $this |
||
| 207 | */ |
||
| 208 | public function flowEnd() |
||
| 209 | { |
||
| 210 | $this->flowStats['end'] = microtime(true); |
||
| 211 | $this->flowStats['mib'] = memory_get_peak_usage(true) / 1048576; |
||
| 212 | $this->flowStats['elapsed'] = $this->flowStats['end'] - $this->flowStats['start']; |
||
| 213 | |||
| 214 | $this->flowStats = array_replace($this->flowStats, $this->duration($this->flowStats['elapsed'])); |
||
| 215 | |||
| 216 | return $this; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Let's be fast at incrementing while we are at it |
||
| 221 | * |
||
| 222 | * @param string $nodeHash |
||
| 223 | * |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | public function &getNodeStat($nodeHash) |
||
| 227 | { |
||
| 228 | return $this->nodeMap[$nodeHash]; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Get/Generate Node Map |
||
| 233 | * |
||
| 234 | * @return array |
||
| 235 | */ |
||
| 236 | public function getNodeMap() |
||
| 237 | { |
||
| 238 | foreach ($this->flow->getNodes() as $node) { |
||
| 239 | $nodeId = $node->getId(); |
||
| 240 | if ($node instanceof BranchNodeInterface || $node instanceof AggregateNodeInterface) { |
||
| 241 | $this->nodeMap[$nodeId]['nodes'] = $node->getPayload()->getNodeMap(); |
||
|
|
|||
| 242 | continue; |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | return $this->nodeMap; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Get the latest Node stats |
||
| 251 | * |
||
| 252 | * @return array<string,integer|string> |
||
| 253 | */ |
||
| 254 | public function getStats() |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param string $nodeHash |
||
| 286 | * @param string $key |
||
| 287 | * |
||
| 288 | * @return $this |
||
| 289 | */ |
||
| 290 | public function incrementNode($nodeHash, $key) |
||
| 291 | { |
||
| 292 | ++$this->nodeMap[$nodeHash][$key]; |
||
| 293 | |||
| 294 | return $this; |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param string $key |
||
| 299 | * |
||
| 300 | * @return $this |
||
| 301 | */ |
||
| 302 | public function incrementFlow($key) |
||
| 303 | { |
||
| 304 | ++$this->flowStats[$key]; |
||
| 305 | |||
| 306 | return $this; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Resets Nodes stats, can be used prior to Flow's re-exec |
||
| 311 | * |
||
| 312 | * @return $this |
||
| 313 | */ |
||
| 314 | public function resetNodeStats() |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Computes a human readable duration string from floating seconds |
||
| 329 | * |
||
| 330 | * @param float $seconds |
||
| 331 | * |
||
| 332 | * @return array<string,integer|string> |
||
| 333 | */ |
||
| 334 | public function duration($seconds) |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @return $this |
||
| 357 | */ |
||
| 358 | protected function setRefs() |
||
| 359 | { |
||
| 360 | $this->registryData = &$this->registry->get($this->flowId); |
||
| 361 | $this->registryData['flowStats'] = &$this->flowStats; |
||
| 362 | $this->registryData['nodeStats'] = &$this->nodeMap; |
||
| 363 | $this->registryData['nodes'] = &$this->reverseMap; |
||
| 364 | |||
| 365 | return $this; |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @return $this |
||
| 370 | */ |
||
| 371 | protected function initDefaults() |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @return $this |
||
| 392 | */ |
||
| 393 | protected function resetTotals() |
||
| 394 | { |
||
| 395 | foreach ($this->incrementTotals as $totalKey) { |
||
| 396 | $this->flowStats[$totalKey] = 0; |
||
| 397 | } |
||
| 398 | |||
| 399 | return $this; |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Set additional increment keys, use : |
||
| 404 | * 'keyName' => int |
||
| 405 | * to add keyName as increment, starting at int |
||
| 406 | * or : |
||
| 407 | * 'keyName' => 'existingIncrement' |
||
| 408 | * to assign keyName as a reference to existingIncrement |
||
| 409 | * |
||
| 410 | * @param array $flowIncrements |
||
| 411 | * |
||
| 412 | * @throws NodalFlowException |
||
| 413 | * |
||
| 414 | * @return $this |
||
| 415 | */ |
||
| 416 | protected function setFlowIncrement(array $flowIncrements) |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @param NodeInterface $node |
||
| 439 | * |
||
| 440 | * @throws NodalFlowException |
||
| 441 | * |
||
| 442 | * @return $this |
||
| 443 | */ |
||
| 444 | protected function setNodeIncrement(NodeInterface $node) |
||
| 465 | } |
||
| 466 | } |
||
| 467 |