Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 25 | class NodalFlow implements FlowInterface |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Flow steps triggering callbacks |
||
| 29 | */ |
||
| 30 | const FLOW_START = 'start'; |
||
| 31 | const FLOW_PROGRESS = 'progress'; |
||
| 32 | const FLOW_SUCCESS = 'success'; |
||
| 33 | const FLOW_FAIL = 'fail'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The parent Flow, only set when branched |
||
| 37 | * |
||
| 38 | * @var FlowInterface |
||
| 39 | */ |
||
| 40 | public $parent; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * This Flow id |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $id; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The underlying node structure |
||
| 51 | * |
||
| 52 | * @var NodeInterface[] |
||
| 53 | */ |
||
| 54 | protected $nodes = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The current Node index |
||
| 58 | * |
||
| 59 | * @var int |
||
| 60 | */ |
||
| 61 | protected $nodeIdx = 0; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The last index value |
||
| 65 | * |
||
| 66 | * @var int |
||
| 67 | */ |
||
| 68 | protected $lastIdx = 0; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The number of Node in this Flow |
||
| 72 | * |
||
| 73 | * @var int |
||
| 74 | */ |
||
| 75 | protected $nodeCount = 0; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * The number of iteration within this Flow |
||
| 79 | * |
||
| 80 | * @var int |
||
| 81 | */ |
||
| 82 | protected $numIterate = 0; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * The current registered Callback class if any |
||
| 86 | * |
||
| 87 | * @var CallbackInterface|null |
||
| 88 | */ |
||
| 89 | protected $callBack; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Progress modulo to apply |
||
| 93 | * Set to x if you want to trigger |
||
| 94 | * progress every x iterations in flow |
||
| 95 | * |
||
| 96 | * @var int |
||
| 97 | */ |
||
| 98 | protected $progressMod = 1024; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Continue flag |
||
| 102 | * |
||
| 103 | * @var bool |
||
| 104 | */ |
||
| 105 | protected $continue = false; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Break Flag |
||
| 109 | * |
||
| 110 | * @var bool |
||
| 111 | */ |
||
| 112 | protected $break = false; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Current Flow Status |
||
| 116 | * |
||
| 117 | * @var FlowStatusInterface |
||
| 118 | */ |
||
| 119 | protected $flowStatus; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var FlowMapInterface |
||
| 123 | */ |
||
| 124 | protected $flowMap; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var string|bool |
||
| 128 | */ |
||
| 129 | protected $interruptNodeId; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Current nonce |
||
| 133 | * |
||
| 134 | * @var int |
||
| 135 | */ |
||
| 136 | private static $nonce = 0; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Instantiate a Flow |
||
| 140 | */ |
||
| 141 | public function __construct() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Adds a Node to the flow |
||
| 149 | * |
||
| 150 | * @param NodeInterface $node |
||
| 151 | * |
||
| 152 | * @throws NodalFlowException |
||
| 153 | * |
||
| 154 | * @return $this |
||
| 155 | */ |
||
| 156 | public function add(NodeInterface $node) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Adds a Payload Node to the Flow |
||
| 174 | * |
||
| 175 | * @param callable $payload |
||
| 176 | * @param mixed $isAReturningVal |
||
| 177 | * @param mixed $isATraversable |
||
| 178 | * |
||
| 179 | * @return $this |
||
| 180 | */ |
||
| 181 | public function addPayload(callable $payload, $isAReturningVal, $isATraversable = false) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Register callback class |
||
| 192 | * |
||
| 193 | * @param CallbackInterface $callBack |
||
| 194 | * |
||
| 195 | * @return $this |
||
| 196 | */ |
||
| 197 | public function setCallBack(CallbackInterface $callBack) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Used to set the eventual Node Target of an Interrupt signal |
||
| 206 | * set to : |
||
| 207 | * - A node hash to target |
||
| 208 | * - true to interrupt every upstream nodes |
||
| 209 | * in this Flow |
||
| 210 | * - false to only interrupt up to the first |
||
| 211 | * upstream Traversable in this Flow |
||
| 212 | * |
||
| 213 | * @param string|bool $interruptNodeId |
||
| 214 | * |
||
| 215 | * @return $this |
||
| 216 | */ |
||
| 217 | public function setInterruptNodeId($interruptNodeId) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Set parent Flow, happens only when branched |
||
| 226 | * |
||
| 227 | * @param FlowInterface $flow |
||
| 228 | * |
||
| 229 | * @return $this |
||
| 230 | */ |
||
| 231 | public function setParent(FlowInterface $flow) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get eventual parent Flow |
||
| 240 | * |
||
| 241 | * @return FlowInterface |
||
| 242 | */ |
||
| 243 | public function getParent() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Tells if this flow has a parent |
||
| 250 | * |
||
| 251 | * @return bool |
||
| 252 | */ |
||
| 253 | public function hasParent() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Generates a truly unique id for the Flow context |
||
| 260 | * |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | public function uniqId() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Execute the flow |
||
| 272 | * |
||
| 273 | * @param null|mixed $param The eventual init argument to the first node |
||
| 274 | * or, in case of a branch, the last relevant |
||
| 275 | * argument from upstream Flow |
||
| 276 | * |
||
| 277 | * @throws NodalFlowException |
||
| 278 | * |
||
| 279 | * @return mixed the last result of the |
||
| 280 | * last returning value node |
||
| 281 | */ |
||
| 282 | public function exec($param = null) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Computes a human readable duration string from floating seconds |
||
| 316 | * |
||
| 317 | * @param float $seconds |
||
| 318 | * |
||
| 319 | * @return array<string,integer|string> |
||
| 320 | */ |
||
| 321 | public function duration($seconds) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Get the stats array with latest Node stats |
||
| 344 | * |
||
| 345 | * @return array |
||
| 346 | */ |
||
| 347 | public function getStats() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Return the Flow id as set during instantiation |
||
| 354 | * |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | public function getId() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * getId() alias for backward compatibility |
||
| 364 | * |
||
| 365 | * @deprecated |
||
| 366 | * |
||
| 367 | * @return string |
||
| 368 | */ |
||
| 369 | public function getFlowId() |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Get the Node array |
||
| 376 | * |
||
| 377 | * @return NodeInterface[] |
||
| 378 | */ |
||
| 379 | public function getNodes() |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Get/Generate Node Map |
||
| 386 | * |
||
| 387 | * @return array |
||
| 388 | */ |
||
| 389 | public function getNodeMap() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Rewinds the Flow |
||
| 396 | * |
||
| 397 | * @return $this |
||
| 398 | */ |
||
| 399 | public function rewind() |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Define the progress modulo, Progress Callback will be |
||
| 412 | * triggered upon each iteration in the flow modulo $progressMod |
||
| 413 | * |
||
| 414 | * @param int $progressMod |
||
| 415 | * |
||
| 416 | * @return $this |
||
| 417 | */ |
||
| 418 | public function setProgressMod($progressMod) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Get current $progressMod |
||
| 427 | * |
||
| 428 | * @return int |
||
| 429 | */ |
||
| 430 | public function getProgressMod() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * The Flow status can either indicate be: |
||
| 437 | * - clean (isClean()): everything went well |
||
| 438 | * - dirty (isDirty()): one Node broke the flow |
||
| 439 | * - exception (isException()): an exception was raised during the flow |
||
| 440 | * |
||
| 441 | * @return FlowStatusInterface |
||
| 442 | */ |
||
| 443 | public function getFlowStatus() |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Break the flow's execution, conceptually similar to breaking |
||
| 450 | * a regular loop |
||
| 451 | * |
||
| 452 | * @param InterrupterInterface|null $flowInterrupt |
||
| 453 | * |
||
| 454 | * @return $this |
||
| 455 | */ |
||
| 456 | public function breakFlow(InterrupterInterface $flowInterrupt = null) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Continue the flow's execution, conceptually similar to continuing |
||
| 463 | * a regular loop |
||
| 464 | * |
||
| 465 | * @param InterrupterInterface|null $flowInterrupt |
||
| 466 | * |
||
| 467 | * @return $this |
||
| 468 | */ |
||
| 469 | public function continueFlow(InterrupterInterface $flowInterrupt = null) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @param string $interruptType |
||
| 476 | * @param InterrupterInterface|null $flowInterrupt |
||
| 477 | * |
||
| 478 | * @throws NodalFlowException |
||
| 479 | * |
||
| 480 | * @return $this |
||
| 481 | */ |
||
| 482 | public function interruptFlow($interruptType, InterrupterInterface $flowInterrupt = null) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @param NodeInterface $node |
||
| 507 | * |
||
| 508 | * @return bool |
||
| 509 | */ |
||
| 510 | protected function interruptNode(NodeInterface $node) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Triggered just before the flow starts |
||
| 519 | * |
||
| 520 | * @return $this |
||
| 521 | */ |
||
| 522 | protected function flowStart() |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Triggered right after the flow stops |
||
| 535 | * |
||
| 536 | * @return $this |
||
| 537 | */ |
||
| 538 | protected function flowEnd() |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Return a simple nonce, fully valid within any flow |
||
| 549 | * |
||
| 550 | * @return int |
||
| 551 | */ |
||
| 552 | protected function getNonce() |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Recurse over nodes which may as well be Flows and |
||
| 559 | * Traversable ... |
||
| 560 | * Welcome to the abysses of recursion or iter-recursion ^^ |
||
| 561 | * |
||
| 562 | * `recurse` perform kind of an hybrid recursion as the |
||
| 563 | * Flow is effectively iterating and recurring over its |
||
| 564 | * Nodes, which may as well be seen as over itself |
||
| 565 | * |
||
| 566 | * Iterating tends to limit the amount of recursion levels: |
||
| 567 | * recursion is only triggered when executing a Traversable |
||
| 568 | * Node's downstream Nodes while every consecutive exec |
||
| 569 | * Nodes are executed within a while loop. |
||
| 570 | * And recursion keeps the size of the recursion context |
||
| 571 | * to a minimum as pretty much everything is done by the |
||
| 572 | * iterating instance |
||
| 573 | * |
||
| 574 | * @param mixed $param |
||
| 575 | * @param int $nodeIdx |
||
| 576 | * |
||
| 577 | * @return mixed the last value returned by the last |
||
| 578 | * returning value Node in the flow |
||
| 579 | */ |
||
| 580 | protected function recurse($param = null, $nodeIdx = 0) |
||
| 658 | |||
| 659 | /** |
||
| 660 | * KISS helper to trigger Callback slots |
||
| 661 | * |
||
| 662 | * @param string $which |
||
| 663 | * @param null|NodeInterface $node |
||
| 664 | * |
||
| 665 | * @return $this |
||
| 666 | */ |
||
| 667 | protected function triggerCallback($which, NodeInterface $node = null) |
||
| 675 | } |
||
| 676 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: