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 |
||
| 28 | class NodalFlow implements FlowInterface |
||
| 29 | { |
||
| 30 | use FlowIdTrait; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Flow steps triggering callbacks |
||
| 34 | */ |
||
| 35 | const FLOW_START = 'start'; |
||
| 36 | const FLOW_PROGRESS = 'progress'; |
||
| 37 | const FLOW_SUCCESS = 'success'; |
||
| 38 | const FLOW_FAIL = 'fail'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The parent Flow, only set when branched |
||
| 42 | * |
||
| 43 | * @var FlowInterface |
||
| 44 | */ |
||
| 45 | public $parent; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $flowIncrements = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The underlying node structure |
||
| 54 | * |
||
| 55 | * @var NodeInterface[] |
||
| 56 | */ |
||
| 57 | protected $nodes = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The current Node index |
||
| 61 | * |
||
| 62 | * @var int |
||
| 63 | */ |
||
| 64 | protected $nodeIdx = 0; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The last index value |
||
| 68 | * |
||
| 69 | * @var int |
||
| 70 | */ |
||
| 71 | protected $lastIdx = 0; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The number of Node in this Flow |
||
| 75 | * |
||
| 76 | * @var int |
||
| 77 | */ |
||
| 78 | protected $nodeCount = 0; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The number of iteration within this Flow |
||
| 82 | * |
||
| 83 | * @var int |
||
| 84 | */ |
||
| 85 | protected $numIterate = 0; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The current registered Callback class if any |
||
| 89 | * |
||
| 90 | * @var CallbackInterface|null |
||
| 91 | */ |
||
| 92 | protected $callBack; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Progress modulo to apply |
||
| 96 | * Set to x if you want to trigger |
||
| 97 | * progress every x iterations in flow |
||
| 98 | * |
||
| 99 | * @var int |
||
| 100 | */ |
||
| 101 | protected $progressMod = 1024; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Continue flag |
||
| 105 | * |
||
| 106 | * @var bool |
||
| 107 | */ |
||
| 108 | protected $continue = false; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Break Flag |
||
| 112 | * |
||
| 113 | * @var bool |
||
| 114 | */ |
||
| 115 | protected $break = false; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Current Flow Status |
||
| 119 | * |
||
| 120 | * @var FlowStatusInterface |
||
| 121 | */ |
||
| 122 | protected $flowStatus; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var FlowMapInterface |
||
| 126 | */ |
||
| 127 | protected $flowMap; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var string|bool |
||
| 131 | */ |
||
| 132 | protected $interruptNodeId; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Instantiate a Flow |
||
| 136 | */ |
||
| 137 | public function __construct() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Adds a Node to the flow |
||
| 144 | * |
||
| 145 | * @param NodeInterface $node |
||
| 146 | * |
||
| 147 | * @throws NodalFlowException |
||
| 148 | * |
||
| 149 | * @return $this |
||
| 150 | */ |
||
| 151 | public function add(NodeInterface $node) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Adds a Payload Node to the Flow |
||
| 170 | * |
||
| 171 | * @param callable $payload |
||
| 172 | * @param mixed $isAReturningVal |
||
| 173 | * @param mixed $isATraversable |
||
| 174 | * |
||
| 175 | * @return $this |
||
| 176 | */ |
||
| 177 | public function addPayload(callable $payload, $isAReturningVal, $isATraversable = false) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Register callback class |
||
| 188 | * |
||
| 189 | * @param CallbackInterface $callBack |
||
| 190 | * |
||
| 191 | * @return $this |
||
| 192 | */ |
||
| 193 | public function setCallBack(CallbackInterface $callBack) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Used to set the eventual Node Target of an Interrupt signal |
||
| 202 | * set to : |
||
| 203 | * - A node hash to target |
||
| 204 | * - true to interrupt every upstream nodes |
||
| 205 | * in this Flow |
||
| 206 | * - false to only interrupt up to the first |
||
| 207 | * upstream Traversable in this Flow |
||
| 208 | * |
||
| 209 | * @param string|bool $interruptNodeId |
||
| 210 | * |
||
| 211 | * @return $this |
||
| 212 | */ |
||
| 213 | public function setInterruptNodeId($interruptNodeId) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Set parent Flow, happens only when branched |
||
| 222 | * |
||
| 223 | * @param FlowInterface $flow |
||
| 224 | * |
||
| 225 | * @return $this |
||
| 226 | */ |
||
| 227 | public function setParent(FlowInterface $flow) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Get eventual parent Flow |
||
| 236 | * |
||
| 237 | * @return FlowInterface |
||
| 238 | */ |
||
| 239 | public function getParent() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Tells if this flow has a parent |
||
| 246 | * |
||
| 247 | * @return bool |
||
| 248 | */ |
||
| 249 | public function hasParent() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Execute the flow |
||
| 256 | * |
||
| 257 | * @param null|mixed $param The eventual init argument to the first node |
||
| 258 | * or, in case of a branch, the last relevant |
||
| 259 | * argument from upstream Flow |
||
| 260 | * |
||
| 261 | * @throws NodalFlowException |
||
| 262 | * |
||
| 263 | * @return mixed the last result of the |
||
| 264 | * last returning value node |
||
| 265 | */ |
||
| 266 | public function exec($param = null) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Get the stats array with latest Node stats |
||
| 298 | * |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | public function getStats() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Get the stats array with latest Node stats |
||
| 308 | * |
||
| 309 | * @return FlowMapInterface |
||
| 310 | */ |
||
| 311 | public function getFlowMap() |
||
| 315 | |||
| 316 | /** |
||
| 317 | * getId() alias for backward compatibility |
||
| 318 | * |
||
| 319 | * @deprecated use `getId` instead |
||
| 320 | * |
||
| 321 | * @return string |
||
| 322 | */ |
||
| 323 | public function getFlowId() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Get the Node array |
||
| 330 | * |
||
| 331 | * @return NodeInterface[] |
||
| 332 | */ |
||
| 333 | public function getNodes() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Get/Generate Node Map |
||
| 340 | * |
||
| 341 | * @return array |
||
| 342 | */ |
||
| 343 | public function getNodeMap() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Rewinds the Flow |
||
| 350 | * |
||
| 351 | * @return $this |
||
| 352 | */ |
||
| 353 | public function rewind() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Define the progress modulo, Progress Callback will be |
||
| 366 | * triggered upon each iteration in the flow modulo $progressMod |
||
| 367 | * |
||
| 368 | * @param int $progressMod |
||
| 369 | * |
||
| 370 | * @return $this |
||
| 371 | */ |
||
| 372 | public function setProgressMod($progressMod) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Get current $progressMod |
||
| 381 | * |
||
| 382 | * @return int |
||
| 383 | */ |
||
| 384 | public function getProgressMod() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * The Flow status can either indicate be: |
||
| 391 | * - clean (isClean()): everything went well |
||
| 392 | * - dirty (isDirty()): one Node broke the flow |
||
| 393 | * - exception (isException()): an exception was raised during the flow |
||
| 394 | * |
||
| 395 | * @return FlowStatusInterface |
||
| 396 | */ |
||
| 397 | public function getFlowStatus() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Break the flow's execution, conceptually similar to breaking |
||
| 404 | * a regular loop |
||
| 405 | * |
||
| 406 | * @param InterrupterInterface|null $flowInterrupt |
||
| 407 | * |
||
| 408 | * @return $this |
||
| 409 | */ |
||
| 410 | public function breakFlow(InterrupterInterface $flowInterrupt = null) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Continue the flow's execution, conceptually similar to continuing |
||
| 417 | * a regular loop |
||
| 418 | * |
||
| 419 | * @param InterrupterInterface|null $flowInterrupt |
||
| 420 | * |
||
| 421 | * @return $this |
||
| 422 | */ |
||
| 423 | public function continueFlow(InterrupterInterface $flowInterrupt = null) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @param string $interruptType |
||
| 430 | * @param InterrupterInterface|null $flowInterrupt |
||
| 431 | * |
||
| 432 | * @throws NodalFlowException |
||
| 433 | * |
||
| 434 | * @return $this |
||
| 435 | */ |
||
| 436 | public function interruptFlow($interruptType, InterrupterInterface $flowInterrupt = null) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @param NodeInterface $node |
||
| 461 | * |
||
| 462 | * @return bool |
||
| 463 | */ |
||
| 464 | protected function interruptNode(NodeInterface $node) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Triggered just before the flow starts |
||
| 473 | * |
||
| 474 | * @return $this |
||
| 475 | */ |
||
| 476 | protected function flowStart() |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Triggered right after the flow stops |
||
| 489 | * |
||
| 490 | * @return $this |
||
| 491 | */ |
||
| 492 | protected function flowEnd() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Recurse over nodes which may as well be Flows and |
||
| 503 | * Traversable ... |
||
| 504 | * Welcome to the abysses of recursion or iter-recursion ^^ |
||
| 505 | * |
||
| 506 | * `recurse` perform kind of an hybrid recursion as the |
||
| 507 | * Flow is effectively iterating and recurring over its |
||
| 508 | * Nodes, which may as well be seen as over itself |
||
| 509 | * |
||
| 510 | * Iterating tends to limit the amount of recursion levels: |
||
| 511 | * recursion is only triggered when executing a Traversable |
||
| 512 | * Node's downstream Nodes while every consecutive exec |
||
| 513 | * Nodes are executed within a while loop. |
||
| 514 | * And recursion keeps the size of the recursion context |
||
| 515 | * to a minimum as pretty much everything is done by the |
||
| 516 | * iterating instance |
||
| 517 | * |
||
| 518 | * @param mixed $param |
||
| 519 | * @param int $nodeIdx |
||
| 520 | * |
||
| 521 | * @return mixed the last value returned by the last |
||
| 522 | * returning value Node in the flow |
||
| 523 | */ |
||
| 524 | protected function recurse($param = null, $nodeIdx = 0) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * KISS helper to trigger Callback slots |
||
| 608 | * |
||
| 609 | * @param string $which |
||
| 610 | * @param null|NodeInterface $node |
||
| 611 | * |
||
| 612 | * @return $this |
||
| 613 | */ |
||
| 614 | protected function triggerCallback($which, NodeInterface $node = null) |
||
| 622 | } |
||
| 623 |