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 array |
||
96 | */ |
||
97 | protected static $registry = []; |
||
98 | |||
99 | /** |
||
100 | * @var FlowInterface |
||
101 | */ |
||
102 | protected $flow; |
||
103 | |||
104 | /** |
||
105 | * @var string |
||
106 | */ |
||
107 | protected $flowId; |
||
108 | |||
109 | /** |
||
110 | * Instantiate a Flow Status |
||
111 | * |
||
112 | * @param FlowInterface $flow |
||
113 | * @param array $flowIncrements |
||
114 | */ |
||
115 | public function __construct(FlowInterface $flow, array $flowIncrements = []) |
||
121 | |||
122 | /** |
||
123 | * The goal is to offer a cheap global state that supports |
||
124 | * sending a parameter to whatever node in any live Flows. |
||
125 | * We also need some kind of specific setup for each Flows |
||
126 | * and Nodes (custom increments etc). |
||
127 | * |
||
128 | * The idea is to share a registry among all instances |
||
129 | * without : |
||
130 | * - singleton: would only be useful to store the global state |
||
131 | * not the specific setup |
||
132 | * - breaking references: we want to be able to increment fast |
||
133 | * thus multiple entries at once by reference |
||
134 | * - DI: it would be a bit too much of code for the purpose and |
||
135 | * would come with the same limitation as the singleton |
||
136 | * - Complex and redundant merge strategies: registering a Node |
||
137 | * would then require to look up for ascendant and descendant |
||
138 | * Flows each time. |
||
139 | * - Breaking serialization: Playing with static and references |
||
140 | * require some attention. The matter is dealt with transparently |
||
141 | * as the static registry acts like a global cache feed with each |
||
142 | * instance data upon un-serialization. |
||
143 | * |
||
144 | * By using a static, we make sure all instances share the same |
||
145 | * registry at all time in the simplest way. |
||
146 | * Each FlowMap instance keeps dynamic reference to the portion |
||
147 | * of the registry that belongs to the Flow holding the instance. |
||
148 | * Upon Serialization, every FlowMap instance will thus only store |
||
149 | * a portion of the global state, that is relevant to its carrying |
||
150 | * Flow. |
||
151 | * |
||
152 | * Upon un-serialization, the global state is restored bit by bit |
||
153 | * as each FlowMap gets un-serialized. |
||
154 | * This leverage one edge case, that is, if we un-serialize two |
||
155 | * time the same Flow (could only be members of different Flows). |
||
156 | * This is not very likely as Flow id are immutable and unique, |
||
157 | * but it could occur. |
||
158 | * This situation is currently dealt with by throwing an exception, |
||
159 | * as it would introduce the need to deal with distinct instances |
||
160 | * with the same Id. And generating new ids is not simple either |
||
161 | * as their whole point is to stay the same for others to know |
||
162 | * who's who. |
||
163 | * I find it a small trade of though, as un-serializing twice |
||
164 | * the same string seems buggy and reusing the same Flow without |
||
165 | * cloning is not such a good idea either (nor required). |
||
166 | * |
||
167 | * The use of a static registry also bring a somehow exotic feature : |
||
168 | * The ability to target any Node in any Flow is not limited to the |
||
169 | * flow within the same root Flow. You can actually target any Flow |
||
170 | * in the current process. Using reference implementation would limit |
||
171 | * the sharing to the root FLow members. |
||
172 | * I don't know if this can be actually useful, but I don't think |
||
173 | * it's such a big deal either. |
||
174 | * |
||
175 | * If you don't feel like doing this at home, I completely |
||
176 | * understand, I'd be very happy to hear about a better and |
||
177 | * more efficient way |
||
178 | */ |
||
179 | public function __wakeup() |
||
190 | |||
191 | /** |
||
192 | * @param NodeInterface $node |
||
193 | * @param int $index |
||
194 | * |
||
195 | * @throws NodalFlowException |
||
196 | */ |
||
197 | public function register(NodeInterface $node, $index) |
||
220 | |||
221 | /** |
||
222 | * Triggered right before the flow starts |
||
223 | * |
||
224 | * @return $this |
||
225 | */ |
||
226 | public function flowStart() |
||
232 | |||
233 | /** |
||
234 | * Triggered right after the flow stops |
||
235 | * |
||
236 | * @return $this |
||
237 | */ |
||
238 | public function flowEnd() |
||
248 | |||
249 | /** |
||
250 | * Let's be fast at incrementing while we are at it |
||
251 | * |
||
252 | * @param string $nodeHash |
||
253 | * |
||
254 | * @return array |
||
255 | */ |
||
256 | public function &getNodeStat($nodeHash) |
||
260 | |||
261 | /** |
||
262 | * Get/Generate Node Map |
||
263 | * |
||
264 | * @throws NodalFlowException |
||
265 | * |
||
266 | * @return array |
||
267 | */ |
||
268 | public function getNodeMap() |
||
291 | |||
292 | /** |
||
293 | * Get the latest Node stats |
||
294 | * |
||
295 | * @return array |
||
296 | */ |
||
297 | public function getStats() |
||
320 | |||
321 | /** |
||
322 | * @param string $nodeHash |
||
323 | * @param string $key |
||
324 | * |
||
325 | * @return $this |
||
326 | */ |
||
327 | public function incrementNode($nodeHash, $key) |
||
333 | |||
334 | /** |
||
335 | * @param string $key |
||
336 | * |
||
337 | * @return $this |
||
338 | */ |
||
339 | public function incrementFlow($key) |
||
345 | |||
346 | /** |
||
347 | * Resets Nodes stats, can be used prior to Flow's re-exec |
||
348 | * |
||
349 | * @return $this |
||
350 | */ |
||
351 | public function resetNodeStats() |
||
363 | |||
364 | /** |
||
365 | * Computes a human readable duration string from floating seconds |
||
366 | * |
||
367 | * @param float $seconds |
||
368 | * |
||
369 | * @return array<string,integer|string> |
||
370 | */ |
||
371 | public function duration($seconds) |
||
391 | |||
392 | /** |
||
393 | * @return $this |
||
394 | */ |
||
395 | protected function setRefs() |
||
404 | |||
405 | /** |
||
406 | * @return $this |
||
407 | */ |
||
408 | protected function initDefaults() |
||
426 | |||
427 | /** |
||
428 | * Set additional increment keys, use : |
||
429 | * 'keyName' => int |
||
430 | * to add keyName as increment, starting at int |
||
431 | * or : |
||
432 | * 'keyName' => 'existingIncrement' |
||
433 | * to assign keyName as a reference to existingIncrement |
||
434 | * |
||
435 | * @param array $flowIncrements |
||
436 | * |
||
437 | * @throws NodalFlowException |
||
438 | * |
||
439 | * @return $this |
||
440 | */ |
||
441 | protected function setFlowIncrement(array $flowIncrements) |
||
465 | |||
466 | /** |
||
467 | * @param NodeInterface $node |
||
468 | * |
||
469 | * @throws NodalFlowException |
||
470 | * |
||
471 | * @return $this |
||
472 | */ |
||
473 | protected function setNodeIncrement(NodeInterface $node) |
||
495 | |||
496 | /** |
||
497 | * @param NodeInterface $node |
||
498 | * |
||
499 | * @throws NodalFlowException |
||
500 | * |
||
501 | * @return $this |
||
502 | */ |
||
503 | protected function enforceUniqueness(NodeInterface $node) |
||
514 | } |
||
515 |