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 | /** |
||
26 | * The workflow five states. |
||
27 | * |
||
28 | * @var int |
||
29 | */ |
||
30 | const OSWF_CREATED = 1; |
||
31 | const OSWF_ACTIVATED = 2; |
||
32 | const OSWF_SUSPENDED = 3; |
||
33 | const OSWF_COMPLETED = 4; |
||
34 | const OSWF_KILLED = 5; |
||
35 | |||
36 | /** |
||
37 | * The workflow instance object. |
||
38 | * |
||
39 | * @var App\Workflow\Eloquent\Entry |
||
40 | */ |
||
41 | protected $entry; |
||
42 | |||
43 | /** |
||
44 | * The workflow config description. |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $wf_config; |
||
49 | |||
50 | /** |
||
51 | * workflow options |
||
52 | * |
||
53 | * @var array |
||
54 | */ |
||
55 | protected $options = []; |
||
56 | |||
57 | /** |
||
58 | * workflow constructor |
||
59 | * |
||
60 | * @param string $entry_id |
||
61 | * @return void |
||
|
|||
62 | */ |
||
63 | public function __construct($entry_id) |
||
86 | |||
87 | /** |
||
88 | * create workflow. |
||
89 | * |
||
90 | * @param string $definition_id |
||
91 | * @param string $caller |
||
92 | * @return string |
||
93 | */ |
||
94 | public static function createInstance($definition_id, $caller) |
||
103 | |||
104 | /** |
||
105 | * get entry id. |
||
106 | * |
||
107 | * @return string |
||
108 | */ |
||
109 | public function getEntryId() |
||
113 | |||
114 | /** |
||
115 | * check action is available |
||
116 | * |
||
117 | * @param array $action_descriptor |
||
118 | * @return boolean |
||
119 | */ |
||
120 | private function isActionAvailable($action_descriptor) |
||
129 | |||
130 | /** |
||
131 | * initialize workflow. |
||
132 | * |
||
133 | * @param array options |
||
134 | * @return void |
||
135 | */ |
||
136 | public function start($options=[]) |
||
179 | |||
180 | /** |
||
181 | * get workflow state. |
||
182 | * |
||
183 | * @return string |
||
184 | */ |
||
185 | public function getEntryState() |
||
189 | |||
190 | /** |
||
191 | * change workflow state. |
||
192 | * |
||
193 | * @param string $new_state |
||
194 | * @return void |
||
195 | */ |
||
196 | public function changeEntryState($new_state) |
||
202 | |||
203 | /** |
||
204 | * complete workflow. |
||
205 | * |
||
206 | * @param string $entry_id |
||
207 | * @return void |
||
208 | */ |
||
209 | protected function completeEntry($entry_id) |
||
213 | |||
214 | /** |
||
215 | * get current steps for workflow. |
||
216 | * |
||
217 | * @return array |
||
218 | */ |
||
219 | public function getCurrentSteps() |
||
223 | |||
224 | /** |
||
225 | * get step meta. |
||
226 | * |
||
227 | * @param string $step_id |
||
228 | * @return array |
||
229 | */ |
||
230 | public function getStepMeta($step_id, $name='') |
||
238 | |||
239 | /** |
||
240 | * move workflow step to history |
||
241 | * |
||
242 | * @param App\Workflow\Eloquent\CurrentStep $current_step |
||
243 | * @param int $action_id |
||
244 | * @return string previous_id |
||
245 | */ |
||
246 | private function moveToHistory($current_step, $action_id) |
||
261 | |||
262 | /** |
||
263 | * create new workflow step. |
||
264 | * |
||
265 | * @param array $result_descriptor |
||
266 | * @param int $action_id |
||
267 | * @param string $previous_id |
||
268 | * @return void |
||
269 | */ |
||
270 | private function createNewCurrentStep($result_descriptor, $action_id, $previous_id='') |
||
304 | |||
305 | /** |
||
306 | * transfer workflow step. |
||
307 | * |
||
308 | * @param array $current_steps |
||
309 | * @param int $action; |
||
310 | * @return void |
||
311 | */ |
||
312 | private function transitionWorkflow($current_steps, $action_id) |
||
400 | |||
401 | /** |
||
402 | * check if the join is completed |
||
403 | */ |
||
404 | private function isJoinCompleted() |
||
408 | |||
409 | /** |
||
410 | * execute action |
||
411 | * |
||
412 | * @param string $action_id |
||
413 | * @param array $options; |
||
414 | * @return string |
||
415 | */ |
||
416 | public function doAction($action_id, $options=[]) |
||
433 | |||
434 | /** |
||
435 | * get join descriptor from list. |
||
436 | * |
||
437 | * @param string $join_id |
||
438 | * @return array |
||
439 | */ |
||
440 | private function getJoinDescriptor($join_id) |
||
450 | |||
451 | /** |
||
452 | * get split descriptor from list. |
||
453 | * |
||
454 | * @param string $split_id |
||
455 | * @return array |
||
456 | */ |
||
457 | private function getSplitDescriptor($split_id) |
||
467 | |||
468 | /** |
||
469 | * get action descriptor from list. |
||
470 | * |
||
471 | * @param array $actions |
||
472 | * @param string $action_id |
||
473 | * @return array |
||
474 | */ |
||
475 | private function getActionDescriptor($actions, $action_id) |
||
487 | |||
488 | /** |
||
489 | * get step configuration. |
||
490 | * |
||
491 | * @param array $steps |
||
492 | * @param string $step_id |
||
493 | * @return array |
||
494 | */ |
||
495 | private function getStepDescriptor($step_id) |
||
505 | |||
506 | /** |
||
507 | * save workflow configuration info. |
||
508 | * |
||
509 | * @param array $info |
||
510 | * @return void |
||
511 | */ |
||
512 | public static function saveWorkflowDefinition($info) |
||
518 | |||
519 | /** |
||
520 | * remove configuration info. |
||
521 | * |
||
522 | * @param string $definition_id |
||
523 | * @return void |
||
524 | */ |
||
525 | public static function removeWorkflowDefinition($definition_id) |
||
529 | |||
530 | /** |
||
531 | * get all available actions |
||
532 | * |
||
533 | * @param array $info |
||
534 | * @param bool $dest_state added for kanban dnd, is not common param. |
||
535 | * @return array |
||
536 | */ |
||
537 | public function getAvailableActions($options=[], $dest_state = false) |
||
552 | |||
553 | /** |
||
554 | * get available actions for step |
||
555 | * |
||
556 | * @param string $step_id |
||
557 | * @param bool $dest_state added for kanban dnd, is not common param. |
||
558 | * @return array |
||
559 | */ |
||
560 | private function getAvailableActionsFromStep($step_id, $dest_state = false) |
||
596 | |||
597 | /** |
||
598 | * get available result from result-list |
||
599 | * |
||
600 | * @param array $results_descriptor |
||
601 | * @return array |
||
602 | */ |
||
603 | public function getAvailableResult($results_descriptor) |
||
623 | |||
624 | /** |
||
625 | * check conditions is passed |
||
626 | * |
||
627 | * @param array $conditions |
||
628 | * @return boolean |
||
629 | */ |
||
630 | private function passesConditions($conditions) |
||
651 | |||
652 | /** |
||
653 | * check condition is passed |
||
654 | * |
||
655 | * @param array $condition |
||
656 | * @return boolean |
||
657 | */ |
||
658 | private function passesCondition($condition) |
||
662 | |||
663 | /** |
||
664 | * execute functions |
||
665 | * |
||
666 | * @param array function |
||
667 | * @return void |
||
668 | */ |
||
669 | private function executeFunctions($functions) |
||
682 | |||
683 | /** |
||
684 | * execute function |
||
685 | * |
||
686 | * @param array $function |
||
687 | * @return mixed |
||
688 | */ |
||
689 | private function executeFunction($function) |
||
705 | |||
706 | /** |
||
707 | * get all workflows' name. |
||
708 | * |
||
709 | * @return array |
||
710 | */ |
||
711 | public static function getWorkflowNames() |
||
715 | |||
716 | /** |
||
717 | * generate temporary variable. |
||
718 | * |
||
719 | * @return array |
||
720 | */ |
||
721 | private function genTmpVars($args=[]) |
||
732 | |||
733 | /** |
||
734 | * get property set |
||
735 | * |
||
736 | * @return mixed |
||
737 | */ |
||
738 | public function getPropertySet($key) |
||
742 | |||
743 | /** |
||
744 | * add property set |
||
745 | * |
||
746 | * @return void |
||
747 | */ |
||
748 | public function setPropertySet($key, $val) |
||
753 | |||
754 | /** |
||
755 | * remove property set |
||
756 | * |
||
757 | * @return void |
||
758 | */ |
||
759 | public function removePropertySet($key) |
||
763 | |||
764 | /** |
||
765 | * get used states in the workflow |
||
766 | * |
||
767 | * @return array |
||
768 | */ |
||
769 | public static function getStates($contents) |
||
779 | |||
780 | /** |
||
781 | * get used screens in the workflow |
||
782 | * |
||
783 | * @return array |
||
784 | */ |
||
785 | public static function getScreens($contents) |
||
804 | |||
805 | /** |
||
806 | * get step num |
||
807 | * |
||
808 | * @return int |
||
809 | */ |
||
810 | public static function getStepNum($contents) |
||
815 | |||
816 | /** |
||
817 | * fake new workflow step. |
||
818 | * |
||
819 | * @param array $result_descriptor |
||
820 | * @param array $caller |
||
821 | * @return void |
||
822 | */ |
||
823 | public function fakeNewCurrentStep($result_descriptor, $caller) |
||
833 | } |
||
834 |
Adding a
@return
annotation 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.