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