Complex classes like Workflow 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 Workflow, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Workflow { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The workflow five states. |
||
| 26 | * |
||
| 27 | * @var int |
||
| 28 | */ |
||
| 29 | const OSWF_CREATED = 1; |
||
| 30 | const OSWF_ACTIVATED = 2; |
||
| 31 | const OSWF_SUSPENDED = 3; |
||
| 32 | const OSWF_COMPLETED = 4; |
||
| 33 | const OSWF_KILLED = 5; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The workflow instance object. |
||
| 37 | * |
||
| 38 | * @var App\Workflow\Eloquent\Entry |
||
| 39 | */ |
||
| 40 | protected $entry; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The workflow config description. |
||
| 44 | * |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected $wf_config; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * workflow options |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $options = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * workflow constructor |
||
| 58 | * |
||
| 59 | * @param string $entry_id |
||
| 60 | * @return void |
||
|
|
|||
| 61 | */ |
||
| 62 | public function __construct($entry_id) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * create workflow. |
||
| 91 | * |
||
| 92 | * @param string $definition_id |
||
| 93 | * @param string $caller |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | public static function createInstance($definition_id, $caller) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * get entry id. |
||
| 108 | * |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | public function getEntryId() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * check action is available |
||
| 118 | * |
||
| 119 | * @param array $action_descriptor |
||
| 120 | * @return boolean |
||
| 121 | */ |
||
| 122 | private function isActionAvailable($action_descriptor) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * initialize workflow. |
||
| 136 | * |
||
| 137 | * @param array options |
||
| 138 | * @return void |
||
| 139 | */ |
||
| 140 | public function start($options=[]) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * get workflow state. |
||
| 190 | * |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | public function getEntryState() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * change workflow state. |
||
| 200 | * |
||
| 201 | * @param string $new_state |
||
| 202 | * @return void |
||
| 203 | */ |
||
| 204 | public function changeEntryState($new_state) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * complete workflow. |
||
| 213 | * |
||
| 214 | * @param string $entry_id |
||
| 215 | * @return void |
||
| 216 | */ |
||
| 217 | protected function completeEntry($entry_id) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * get current steps for workflow. |
||
| 224 | * |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | public function getCurrentSteps() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * get step meta. |
||
| 234 | * |
||
| 235 | * @param string $step_id |
||
| 236 | * @return array |
||
| 237 | */ |
||
| 238 | public function getStepMeta($step_id, $name='') |
||
| 247 | |||
| 248 | /** |
||
| 249 | * move workflow step to history |
||
| 250 | * |
||
| 251 | * @param App\Workflow\Eloquent\CurrentStep $current_step |
||
| 252 | * @param int $action_id |
||
| 253 | * @return string previous_id |
||
| 254 | */ |
||
| 255 | private function moveToHistory($current_step, $action_id) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * create new workflow step. |
||
| 273 | * |
||
| 274 | * @param array $result_descriptor |
||
| 275 | * @param int $action_id |
||
| 276 | * @param string $previous_id |
||
| 277 | * @return void |
||
| 278 | */ |
||
| 279 | private function createNewCurrentStep($result_descriptor, $action_id, $previous_id='') |
||
| 318 | |||
| 319 | /** |
||
| 320 | * transfer workflow step. |
||
| 321 | * |
||
| 322 | * @param array $current_steps |
||
| 323 | * @param int $action; |
||
| 324 | * @return void |
||
| 325 | */ |
||
| 326 | private function transitionWorkflow($current_steps, $action_id) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * check if the join is completed |
||
| 433 | */ |
||
| 434 | private function isJoinCompleted() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * execute action |
||
| 441 | * |
||
| 442 | * @param string $action_id |
||
| 443 | * @param array $options; |
||
| 444 | * @return string |
||
| 445 | */ |
||
| 446 | public function doAction($action_id, $options=[]) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * get join descriptor from list. |
||
| 468 | * |
||
| 469 | * @param string $join_id |
||
| 470 | * @return array |
||
| 471 | */ |
||
| 472 | private function getJoinDescriptor($join_id) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * get split descriptor from list. |
||
| 486 | * |
||
| 487 | * @param string $split_id |
||
| 488 | * @return array |
||
| 489 | */ |
||
| 490 | private function getSplitDescriptor($split_id) |
||
| 501 | |||
| 502 | /** |
||
| 503 | * get action descriptor from list. |
||
| 504 | * |
||
| 505 | * @param array $actions |
||
| 506 | * @param string $action_id |
||
| 507 | * @return array |
||
| 508 | */ |
||
| 509 | private function getActionDescriptor($actions, $action_id) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * get step configuration. |
||
| 525 | * |
||
| 526 | * @param array $steps |
||
| 527 | * @param string $step_id |
||
| 528 | * @return array |
||
| 529 | */ |
||
| 530 | private function getStepDescriptor($step_id) |
||
| 541 | |||
| 542 | /** |
||
| 543 | * save workflow configuration info. |
||
| 544 | * |
||
| 545 | * @param array $info |
||
| 546 | * @return void |
||
| 547 | */ |
||
| 548 | public static function saveWorkflowDefinition($info) |
||
| 554 | |||
| 555 | /** |
||
| 556 | * remove configuration info. |
||
| 557 | * |
||
| 558 | * @param string $definition_id |
||
| 559 | * @return void |
||
| 560 | */ |
||
| 561 | public static function removeWorkflowDefinition($definition_id) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * get all available actions |
||
| 568 | * |
||
| 569 | * @param array $info |
||
| 570 | * @param bool $dest_state added for kanban dnd, is not common param. |
||
| 571 | * @return array |
||
| 572 | */ |
||
| 573 | public function getAvailableActions($options=[], $dest_state = false) |
||
| 588 | |||
| 589 | /** |
||
| 590 | * get available actions for step |
||
| 591 | * |
||
| 592 | * @param string $step_id |
||
| 593 | * @param bool $dest_state added for kanban dnd, is not common param. |
||
| 594 | * @return array |
||
| 595 | */ |
||
| 596 | private function getAvailableActionsFromStep($step_id, $dest_state = false) |
||
| 638 | |||
| 639 | /** |
||
| 640 | * get available result from result-list |
||
| 641 | * |
||
| 642 | * @param array $results_descriptor |
||
| 643 | * @return array |
||
| 644 | */ |
||
| 645 | public function getAvailableResult($results_descriptor) |
||
| 667 | |||
| 668 | /** |
||
| 669 | * check conditions is passed |
||
| 670 | * |
||
| 671 | * @param array $conditions |
||
| 672 | * @return boolean |
||
| 673 | */ |
||
| 674 | private function passesConditions($conditions) |
||
| 698 | |||
| 699 | /** |
||
| 700 | * check condition is passed |
||
| 701 | * |
||
| 702 | * @param array $condition |
||
| 703 | * @return boolean |
||
| 704 | */ |
||
| 705 | private function passesCondition($condition) |
||
| 709 | |||
| 710 | /** |
||
| 711 | * execute functions |
||
| 712 | * |
||
| 713 | * @param array function |
||
| 714 | * @return void |
||
| 715 | */ |
||
| 716 | private function executeFunctions($functions) |
||
| 731 | |||
| 732 | /** |
||
| 733 | * execute function |
||
| 734 | * |
||
| 735 | * @param array $function |
||
| 736 | * @return mixed |
||
| 737 | */ |
||
| 738 | private function executeFunction($function) |
||
| 755 | |||
| 756 | /** |
||
| 757 | * get all workflows' name. |
||
| 758 | * |
||
| 759 | * @return array |
||
| 760 | */ |
||
| 761 | public static function getWorkflowNames() |
||
| 765 | |||
| 766 | /** |
||
| 767 | * generate temporary variable. |
||
| 768 | * |
||
| 769 | * @return array |
||
| 770 | */ |
||
| 771 | private function genTmpVars($args=[]) |
||
| 782 | |||
| 783 | /** |
||
| 784 | * get property set |
||
| 785 | * |
||
| 786 | * @return mixed |
||
| 787 | */ |
||
| 788 | public function getPropertySet($key) |
||
| 792 | |||
| 793 | /** |
||
| 794 | * add property set |
||
| 795 | * |
||
| 796 | * @return void |
||
| 797 | */ |
||
| 798 | public function setPropertySet($key, $val) |
||
| 803 | |||
| 804 | /** |
||
| 805 | * remove property set |
||
| 806 | * |
||
| 807 | * @return void |
||
| 808 | */ |
||
| 809 | public function removePropertySet($key) |
||
| 813 | |||
| 814 | /** |
||
| 815 | * get used states in the workflow |
||
| 816 | * |
||
| 817 | * @return array |
||
| 818 | */ |
||
| 819 | public static function getStates($contents) |
||
| 829 | |||
| 830 | /** |
||
| 831 | * get used screens in the workflow |
||
| 832 | * |
||
| 833 | * @return array |
||
| 834 | */ |
||
| 835 | public static function getScreens($contents) |
||
| 856 | |||
| 857 | /** |
||
| 858 | * get step num |
||
| 859 | * |
||
| 860 | * @return int |
||
| 861 | */ |
||
| 862 | public static function getStepNum($contents) |
||
| 867 | |||
| 868 | /** |
||
| 869 | * fake new workflow step. |
||
| 870 | * |
||
| 871 | * @param array $result_descriptor |
||
| 872 | * @param array $caller |
||
| 873 | * @return void |
||
| 874 | */ |
||
| 875 | public function fakeNewCurrentStep($result_descriptor, $caller) |
||
| 885 | } |
||
| 886 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.