Complex classes like NodalFlow 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 NodalFlow, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class NodalFlow implements FlowInterface |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Flow steps triggering callbacks |
||
| 27 | */ |
||
| 28 | const FLOW_START = 'start'; |
||
| 29 | const FLOW_PROGRESS = 'progress'; |
||
| 30 | const FLOW_SUCCESS = 'success'; |
||
| 31 | const FLOW_FAIL = 'fail'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The parent Flow, only set when branched |
||
| 35 | * |
||
| 36 | * @var FlowInterface |
||
| 37 | */ |
||
| 38 | public $parent; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * This Flow id |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $flowId; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The underlying node structure |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | protected $nodes = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The current Node index |
||
| 56 | * |
||
| 57 | * @var int |
||
| 58 | */ |
||
| 59 | protected $nodeIdx = 0; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The last index value |
||
| 63 | * |
||
| 64 | * @var int |
||
| 65 | */ |
||
| 66 | protected $lastIdx = 0; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The number of Node in this Flow |
||
| 70 | * |
||
| 71 | * @var int |
||
| 72 | */ |
||
| 73 | protected $nodeCount = 0; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The number of iteration within this Flow |
||
| 77 | * |
||
| 78 | * @var int |
||
| 79 | */ |
||
| 80 | protected $numIterate = 0; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The current registered Callback class if any |
||
| 84 | * |
||
| 85 | * @var CallbackInterface|null |
||
| 86 | */ |
||
| 87 | protected $callBack; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Progress modulo to apply |
||
| 91 | * Set to x if you want to trigger |
||
| 92 | * progress every x iterations in flow |
||
| 93 | * |
||
| 94 | * @var int |
||
| 95 | */ |
||
| 96 | protected $progressMod = 1024; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * The default Node Map values |
||
| 100 | * |
||
| 101 | * @var array |
||
| 102 | */ |
||
| 103 | protected $nodeMapDefault = [ |
||
| 104 | 'class' => null, |
||
| 105 | 'branchId' => null, |
||
| 106 | 'hash' => null, |
||
| 107 | 'index' => 0, |
||
| 108 | 'num_exec' => 0, |
||
| 109 | 'num_iterate' => 0, |
||
| 110 | ]; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * The default Node stats values |
||
| 114 | * |
||
| 115 | * @var array |
||
| 116 | */ |
||
| 117 | protected $nodeStatsDefault = [ |
||
| 118 | 'num_exec' => 0, |
||
| 119 | 'num_iterate' => 0, |
||
| 120 | ]; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Node stats values |
||
| 124 | * |
||
| 125 | * @var array |
||
| 126 | */ |
||
| 127 | protected $nodeStats = []; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * The object map, used to enforce object unicity within the Flow |
||
| 131 | * |
||
| 132 | * @var array |
||
| 133 | */ |
||
| 134 | protected $objectMap = []; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * The Node Map |
||
| 138 | * |
||
| 139 | * @var array |
||
| 140 | */ |
||
| 141 | protected $nodeMap = []; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * The Flow stats default values |
||
| 145 | * |
||
| 146 | * @var array |
||
| 147 | */ |
||
| 148 | protected $statsDefault = [ |
||
| 149 | 'start' => null, |
||
| 150 | 'end' => null, |
||
| 151 | 'duration' => null, |
||
| 152 | 'mib' => null, |
||
| 153 | ]; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * The Flow Stats |
||
| 157 | * |
||
| 158 | * @var array |
||
| 159 | */ |
||
| 160 | protected $stats = [ |
||
| 161 | 'invocations' => [], |
||
| 162 | ]; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Number of exec calls in thhis Flow |
||
| 166 | * |
||
| 167 | * @var int |
||
| 168 | */ |
||
| 169 | protected $numExec = 0; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Continue flag |
||
| 173 | * |
||
| 174 | * @var bool |
||
| 175 | */ |
||
| 176 | protected $continue = false; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Break Flag |
||
| 180 | * |
||
| 181 | * @var bool |
||
| 182 | */ |
||
| 183 | protected $break = false; |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Current Flow Status |
||
| 187 | * |
||
| 188 | * @var FlowStatusInterface |
||
| 189 | */ |
||
| 190 | protected $flowStatus; |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Current nonce |
||
| 194 | * |
||
| 195 | * @var int |
||
| 196 | */ |
||
| 197 | private static $nonce = 0; |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Instantiate a Flow |
||
| 201 | */ |
||
| 202 | public function __construct() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Adds a Node to the flow |
||
| 210 | * |
||
| 211 | * @param NodeInterface $node |
||
| 212 | * |
||
| 213 | * @return $this |
||
| 214 | */ |
||
| 215 | public function add(NodeInterface $node) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Adds a Payload Node to the Flow |
||
| 245 | * |
||
| 246 | * @param callable $payload |
||
| 247 | * @param mixed $isAReturningVal |
||
| 248 | * @param mixed $isATraversable |
||
| 249 | * |
||
| 250 | * @return $this |
||
| 251 | */ |
||
| 252 | public function addPayload(callable $payload, $isAReturningVal, $isATraversable = false) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Register callback class |
||
| 263 | * |
||
| 264 | * @param CallbackInterface $callBack |
||
| 265 | * |
||
| 266 | * @return $this |
||
| 267 | */ |
||
| 268 | public function setCallBack(CallbackInterface $callBack) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Set parent Flow, happens only when branched |
||
| 277 | * |
||
| 278 | * @param FlowInterface $flow |
||
| 279 | * |
||
| 280 | * @return $this |
||
| 281 | */ |
||
| 282 | public function setParent(FlowInterface $flow) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Get eventual parent Flow |
||
| 291 | * |
||
| 292 | * @return FlowInterface |
||
| 293 | */ |
||
| 294 | public function getParent() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Tells if this flow has a parent |
||
| 301 | * |
||
| 302 | * @return bool |
||
| 303 | */ |
||
| 304 | public function hasParent() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Generates a truely unique id for the Flow context |
||
| 311 | * |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | public function uniqId() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Generate a friendly (read humanly distinguishable) object hash |
||
| 323 | * |
||
| 324 | * @param object $object |
||
| 325 | * |
||
| 326 | * @return string |
||
| 327 | */ |
||
| 328 | public function objectHash($object) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Execute the flow |
||
| 335 | * |
||
| 336 | * @param null|mixed $param The eventual init argument to the first node |
||
| 337 | * or, in case of a branch, the last relevant |
||
| 338 | * argument from upstream Flow |
||
| 339 | * |
||
| 340 | * @return mixed the last result of the |
||
| 341 | * last returning value node |
||
| 342 | */ |
||
| 343 | public function exec($param = null) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Computes a human readable duration string from floating seconds |
||
| 374 | * |
||
| 375 | * @param float $seconds |
||
| 376 | * |
||
| 377 | * @return array |
||
| 378 | */ |
||
| 379 | public function duration($seconds) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Resets Nodes stats, used prior to Flow's re-exec |
||
| 402 | * |
||
| 403 | * @return $this |
||
| 404 | */ |
||
| 405 | public function resetNodeStats() |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Get the stats array with latest Node stats |
||
| 416 | * |
||
| 417 | * @return array |
||
| 418 | */ |
||
| 419 | public function getStats() |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Return the Flow id as set during instantiation |
||
| 432 | * |
||
| 433 | * @return string |
||
| 434 | */ |
||
| 435 | public function getFlowId() |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Get the Node array |
||
| 442 | * |
||
| 443 | * @return array |
||
| 444 | */ |
||
| 445 | public function getNodes() |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Generate Node Map |
||
| 452 | * |
||
| 453 | * @return array |
||
| 454 | */ |
||
| 455 | public function getNodeMap() |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Get the Node stats |
||
| 479 | * |
||
| 480 | * @return array |
||
| 481 | */ |
||
| 482 | public function getNodeStats() |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Rewinds the Flow |
||
| 495 | * |
||
| 496 | * @return $this |
||
| 497 | */ |
||
| 498 | public function rewind() |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Define the progress modulo, Progress Callback will be |
||
| 509 | * triggered upon each iteration in the flow modulo $progressMod |
||
| 510 | * |
||
| 511 | * @param int $progressMod |
||
| 512 | * |
||
| 513 | * @return $this |
||
| 514 | */ |
||
| 515 | public function setProgressMod($progressMod) |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Get current $progressMod |
||
| 524 | * |
||
| 525 | * @return int |
||
| 526 | */ |
||
| 527 | public function getProgressMod() |
||
| 531 | |||
| 532 | /** |
||
| 533 | * The Flow status can either indicate be: |
||
| 534 | * - clean (isClean()): everything went well |
||
| 535 | * - dirty (isDirty()): one Node broke the flow |
||
| 536 | * - exception (isException()): an exception was raised during the flow |
||
| 537 | * |
||
| 538 | * @return FlowStatusInterface |
||
| 539 | */ |
||
| 540 | public function getFlowStatus() |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Break the flow's execution, conceptuially similar to breaking |
||
| 547 | * a regular loop |
||
| 548 | * |
||
| 549 | * @return $this |
||
| 550 | */ |
||
| 551 | public function breakFlow() |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Continue the flow's execution, conceptuially similar to continuing |
||
| 562 | * a regular loop |
||
| 563 | * |
||
| 564 | * @return $this |
||
| 565 | */ |
||
| 566 | public function continueFlow() |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Triggered just before the flow starts |
||
| 575 | * |
||
| 576 | * @return $this |
||
| 577 | */ |
||
| 578 | protected function flowStart() |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Triggered right after the flow stops |
||
| 592 | * |
||
| 593 | * @return $this |
||
| 594 | */ |
||
| 595 | protected function flowEnd() |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Return a simple nonce, fully valid within each flow |
||
| 611 | * |
||
| 612 | * @return int |
||
| 613 | */ |
||
| 614 | protected function getNonce() |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Recurse over flows and nodes which may |
||
| 621 | * as well be Traversable ... |
||
| 622 | * Welcome to the abysses of recursion ^^ |
||
| 623 | * |
||
| 624 | * @param mixed $param |
||
| 625 | * @param int $nodeIdx |
||
| 626 | * |
||
| 627 | * @return mixed the last value returned by the last |
||
| 628 | * returning value Node in the flow |
||
| 629 | */ |
||
| 630 | protected function recurse($param = null, $nodeIdx = 0) |
||
| 696 | |||
| 697 | /** |
||
| 698 | * KISS helper to trigger Callback slots |
||
| 699 | * |
||
| 700 | * @param string $which |
||
| 701 | * @param null|NodeInterface $node |
||
| 702 | * |
||
| 703 | * @return $this |
||
| 704 | */ |
||
| 705 | protected function triggerCallback($which, NodeInterface $node = null) |
||
| 713 | } |
||
| 714 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.