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 | 'num_exec' => 0, |
||
| 51 | 'num_iterate' => 0, |
||
| 52 | 'num_break' => 0, |
||
| 53 | 'num_continue' => 0, |
||
| 54 | ]; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The default Node stats values |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected $nodeIncrements = [ |
||
| 62 | 'num_exec' => 0, |
||
| 63 | 'num_iterate' => 0, |
||
| 64 | 'num_break' => 0, |
||
| 65 | 'num_continue' => 0, |
||
| 66 | ]; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The Flow map default values |
||
| 70 | * |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $flowMapDefault = [ |
||
| 74 | 'class' => null, |
||
| 75 | 'id' => null, |
||
| 76 | 'start' => null, |
||
| 77 | 'end' => null, |
||
| 78 | 'elapsed' => null, |
||
| 79 | 'duration' => null, |
||
| 80 | 'mib' => null, |
||
| 81 | ]; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | protected $incrementTotals = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | protected $flowIncrements; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | protected $flowStats; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var array |
||
| 100 | */ |
||
| 101 | protected static $registry = []; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var FlowInterface |
||
| 105 | */ |
||
| 106 | protected $flow; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var string |
||
| 110 | */ |
||
| 111 | protected $flowId; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Instantiate a Flow Status |
||
| 115 | * |
||
| 116 | * @param FlowInterface $flow |
||
| 117 | * @param array $flowIncrements |
||
| 118 | */ |
||
| 119 | public function __construct(FlowInterface $flow, array $flowIncrements = []) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * The goal is to offer a cheap global state that supports |
||
| 128 | * sending a parameter to whatever node in any live Flows. |
||
| 129 | * We also need some kind of specific setup for each Flows |
||
| 130 | * and Nodes (custom increments etc). |
||
| 131 | * |
||
| 132 | * The idea is to share a registry among all instances |
||
| 133 | * without : |
||
| 134 | * - singleton: would only be useful to store the global state |
||
| 135 | * not the specific setup |
||
| 136 | * - breaking references: we want to be able to increment fast |
||
| 137 | * thus multiple entries at once by reference |
||
| 138 | * - DI: it would be a bit too much of code for the purpose and |
||
| 139 | * would come with the same limitation as the singleton |
||
| 140 | * - Complex and redundant merge strategies: registering a Node |
||
| 141 | * would then require to look up for ascendant and descendant |
||
| 142 | * Flows each time. |
||
| 143 | * - Breaking serialization: Playing with static and references |
||
| 144 | * require some attention. The matter is dealt with transparently |
||
| 145 | * as the static registry acts like a global cache feed with each |
||
| 146 | * instance data upon un-serialization. |
||
| 147 | * |
||
| 148 | * By using a static, we make sure all instances share the same |
||
| 149 | * registry at all time in the simplest way. |
||
| 150 | * Each FlowMap instance keeps dynamic reference to the portion |
||
| 151 | * of the registry that belongs to the Flow holding the instance. |
||
| 152 | * Upon Serialization, every FlowMap instance will thus only store |
||
| 153 | * a portion of the global state, that is relevant to its carrying |
||
| 154 | * Flow. |
||
| 155 | * |
||
| 156 | * Upon un-serialization, the global state is restored bit by bit |
||
| 157 | * as each FlowMap gets un-serialized. |
||
| 158 | * This leverage one edge case, that is, if we un-serialize two |
||
| 159 | * time the same Flow (could only be members of different Flows). |
||
| 160 | * This is not very likely as Flow id are immutable and unique, |
||
| 161 | * but it could occur. |
||
| 162 | * This situation is currently dealt with by throwing an exception, |
||
| 163 | * as it would introduce the need to deal with distinct instances |
||
| 164 | * with the same Id. And generating new ids is not simple either |
||
| 165 | * as their whole point is to stay the same for others to know |
||
| 166 | * who's who. |
||
| 167 | * I find it a small trade of though, as un-serializing twice |
||
| 168 | * the same string seems buggy and reusing the same Flow without |
||
| 169 | * cloning is not such a good idea either (nor required). |
||
| 170 | * |
||
| 171 | * The use of a static registry also bring a somehow exotic feature : |
||
| 172 | * The ability to target any Node in any Flow is not limited to the |
||
| 173 | * flow within the same root Flow. You can actually target any Flow |
||
| 174 | * in the current process. Using reference implementation would limit |
||
| 175 | * the sharing to the root FLow members. |
||
| 176 | * I don't know if this can be actually useful, but I don't think |
||
| 177 | * it's such a big deal either. |
||
| 178 | * |
||
| 179 | * If you don't feel like doing this at home, I completely |
||
| 180 | * understand, I'd be very happy to hear about a better and |
||
| 181 | * more efficient way |
||
| 182 | */ |
||
| 183 | public function __wakeup() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | public function getRegistry() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param NodeInterface $node |
||
| 205 | * @param int $index |
||
| 206 | * |
||
| 207 | * @throws NodalFlowException |
||
| 208 | */ |
||
| 209 | public function register(NodeInterface $node, $index) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Triggered right before the flow starts |
||
| 235 | * |
||
| 236 | * @return $this |
||
| 237 | */ |
||
| 238 | public function flowStart() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Triggered right after the flow stops |
||
| 247 | * |
||
| 248 | * @return $this |
||
| 249 | */ |
||
| 250 | public function flowEnd() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Let's be fast at incrementing while we are at it |
||
| 263 | * |
||
| 264 | * @param string $nodeHash |
||
| 265 | * |
||
| 266 | * @return array |
||
| 267 | */ |
||
| 268 | public function &getNodeStat($nodeHash) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Get/Generate Node Map |
||
| 275 | * |
||
| 276 | * @throws NodalFlowException |
||
| 277 | * |
||
| 278 | * @return array |
||
| 279 | */ |
||
| 280 | public function getNodeMap() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Get the latest Node stats |
||
| 306 | * |
||
| 307 | * @return array |
||
| 308 | */ |
||
| 309 | public function getStats() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param string $nodeHash |
||
| 335 | * @param string $key |
||
| 336 | * |
||
| 337 | * @return $this |
||
| 338 | */ |
||
| 339 | public function incrementNode($nodeHash, $key) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @param string $key |
||
| 348 | * |
||
| 349 | * @return $this |
||
| 350 | */ |
||
| 351 | public function incrementFlow($key) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Resets Nodes stats, can be used prior to Flow's re-exec |
||
| 360 | * |
||
| 361 | * @return $this |
||
| 362 | */ |
||
| 363 | public function resetNodeStats() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Computes a human readable duration string from floating seconds |
||
| 378 | * |
||
| 379 | * @param float $seconds |
||
| 380 | * |
||
| 381 | * @return array<string,integer|string> |
||
| 382 | */ |
||
| 383 | public function duration($seconds) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @return $this |
||
| 406 | */ |
||
| 407 | protected function setRefs() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @return $this |
||
| 419 | */ |
||
| 420 | protected function initDefaults() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Set additional increment keys, use : |
||
| 441 | * 'keyName' => int |
||
| 442 | * to add keyName as increment, starting at int |
||
| 443 | * or : |
||
| 444 | * 'keyName' => 'existingIncrement' |
||
| 445 | * to assign keyName as a reference to existingIncrement |
||
| 446 | * |
||
| 447 | * @param array $flowIncrements |
||
| 448 | * |
||
| 449 | * @throws NodalFlowException |
||
| 450 | * |
||
| 451 | * @return $this |
||
| 452 | */ |
||
| 453 | protected function setFlowIncrement(array $flowIncrements) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @param NodeInterface $node |
||
| 480 | * |
||
| 481 | * @throws NodalFlowException |
||
| 482 | * |
||
| 483 | * @return $this |
||
| 484 | */ |
||
| 485 | protected function setNodeIncrement(NodeInterface $node) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * @param NodeInterface $node |
||
| 510 | * |
||
| 511 | * @throws NodalFlowException |
||
| 512 | * |
||
| 513 | * @return $this |
||
| 514 | */ |
||
| 515 | protected function enforceUniqueness(NodeInterface $node) |
||
| 526 | } |
||
| 527 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.