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 WorkflowInstance 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 WorkflowInstance, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class WorkflowInstance extends DataObject |
||
50 | { |
||
51 | private static $db = array( |
||
52 | 'Title' => 'Varchar(128)', |
||
53 | 'WorkflowStatus' => "Enum('Active,Paused,Complete,Cancelled','Active')", |
||
54 | 'TargetClass' => 'Varchar(255)', |
||
55 | 'TargetID' => 'Int', |
||
56 | ); |
||
57 | |||
58 | private static $has_one = array( |
||
59 | 'Definition' => WorkflowDefinition::class, |
||
60 | 'CurrentAction' => WorkflowActionInstance::class, |
||
61 | 'Initiator' => Member::class, |
||
62 | ); |
||
63 | |||
64 | private static $has_many = array( |
||
65 | 'Actions' => WorkflowActionInstance::class, |
||
66 | ); |
||
67 | |||
68 | /** |
||
69 | * The list of users who are responsible for performing the current WorkflowAction |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | private static $many_many = array( |
||
74 | 'Users' => Member::class, |
||
75 | 'Groups' => Group::class, |
||
76 | ); |
||
77 | |||
78 | private static $summary_fields = array( |
||
79 | 'Title', |
||
80 | 'WorkflowStatus', |
||
81 | 'Created' |
||
82 | ); |
||
83 | |||
84 | private static $default_sort = array( |
||
85 | '"Created"' => 'DESC' |
||
86 | ); |
||
87 | |||
88 | /** |
||
89 | * If set to true, actions that cannot be executed by the user will not show |
||
90 | * on the frontend (just like the backend). |
||
91 | * |
||
92 | * @var boolean |
||
93 | */ |
||
94 | private static $hide_disabled_actions_on_frontend = false; |
||
95 | |||
96 | /** |
||
97 | * Fields to ignore when generating a diff for data objects. |
||
98 | */ |
||
99 | private static $diff_ignore_fields = array( |
||
100 | 'LastEdited', |
||
101 | 'Created', |
||
102 | 'workflowService', |
||
103 | 'ParentID', |
||
104 | 'Sort', |
||
105 | 'PublishJobID', |
||
106 | 'UnPublishJobID' |
||
107 | ); |
||
108 | |||
109 | private static $table_name = 'WorkflowInstance'; |
||
110 | |||
111 | /** |
||
112 | * Get the CMS view of the instance. This is used to display the log of |
||
113 | * this workflow, and options to reassign if the workflow hasn't been |
||
114 | * finished yet |
||
115 | * |
||
116 | * @return FieldList |
||
117 | */ |
||
118 | public function getCMSFields() |
||
170 | |||
171 | public function fieldLabels($includerelations = true) |
||
181 | |||
182 | /** |
||
183 | * See if we've been saved in context of managing the workflow directly |
||
184 | */ |
||
185 | public function onBeforeWrite() |
||
197 | |||
198 | /** |
||
199 | * Update the current state of the workflow |
||
200 | * |
||
201 | * Typically, this is triggered by someone modifiying the workflow instance via the modeladmin form |
||
202 | * side of things when administering things, such as re-assigning or manually approving a stuck workflow |
||
203 | * |
||
204 | * Note that this is VERY similar to AdvancedWorkflowExtension::updateworkflow |
||
205 | * but without the formy bits. These two implementations should PROBABLY |
||
206 | * be merged |
||
207 | * |
||
208 | * @todo refactor with AdvancedWorkflowExtension |
||
209 | * |
||
210 | * @param type $data |
||
211 | * @return |
||
212 | */ |
||
213 | public function updateWorkflow($data) |
||
238 | |||
239 | /** |
||
240 | * Get the target-object that this WorkflowInstance "points" to. |
||
241 | * |
||
242 | * Workflows are not restricted to being active on SiteTree objects, |
||
243 | * so we need to account for being attached to anything. |
||
244 | * |
||
245 | * Sets Versioned::set_reading_mode() to allow fetching of Draft _and_ Published |
||
246 | * content. |
||
247 | * |
||
248 | * @param boolean $getLive |
||
249 | * @return null|DataObject |
||
250 | */ |
||
251 | public function getTarget($getLive = false) |
||
273 | |||
274 | /** |
||
275 | * |
||
276 | * @param boolean $getLive |
||
277 | * @see {@link {$this->getTarget()} |
||
278 | * @return null|DataObject |
||
279 | */ |
||
280 | public function Target($getLive = false) |
||
284 | |||
285 | /** |
||
286 | * Returns the field differences between the older version and current version of Target |
||
287 | * |
||
288 | * @return ArrayList |
||
289 | */ |
||
290 | public function getTargetDiff() |
||
302 | |||
303 | /** |
||
304 | * Start a workflow based on a particular definition for a particular object. |
||
305 | * |
||
306 | * The object is optional; if not specified, it is assumed that this workflow |
||
307 | * is simply a task based checklist type of workflow. |
||
308 | * |
||
309 | * @param WorkflowDefinition $definition |
||
310 | * @param DataObject $for |
||
311 | */ |
||
312 | public function beginWorkflow(WorkflowDefinition $definition, DataObject $for = null) |
||
344 | |||
345 | /** |
||
346 | * Execute this workflow. In rare cases this will actually execute all actions, |
||
347 | * but typically, it will stop and wait for the user to input something |
||
348 | * |
||
349 | * The basic process is to get the current action, and see whether it has been finished |
||
350 | * by some process, if not it attempts to execute it. |
||
351 | * |
||
352 | * If it has been finished, we check to see if there's some transitions to follow. If there's |
||
353 | * only one transition, then we execute that immediately. |
||
354 | * |
||
355 | * If there's multiple transitions, we just stop and wait for the user to manually |
||
356 | * trigger a transition. |
||
357 | * |
||
358 | * If there's no transitions, we make the assumption that we've finished the workflow and |
||
359 | * mark it as such. |
||
360 | * |
||
361 | * |
||
362 | */ |
||
363 | public function execute() |
||
411 | |||
412 | /** |
||
413 | * Evaluate all the transitions of an action and determine whether we should |
||
414 | * follow any of them yet. |
||
415 | * |
||
416 | * @param WorkflowActionInstance $action |
||
417 | * @return WorkflowTransition |
||
418 | */ |
||
419 | protected function checkTransitions(WorkflowActionInstance $action) |
||
428 | |||
429 | /** |
||
430 | * Transitions a workflow to the next step defined by the given transition. |
||
431 | * |
||
432 | * After transitioning, the action is 'executed', and next steps |
||
433 | * determined. |
||
434 | * |
||
435 | * @param WorkflowTransition $transition |
||
436 | */ |
||
437 | public function performTransition(WorkflowTransition $transition) |
||
469 | |||
470 | /** |
||
471 | * Returns a list of all Members that are assigned to this instance, either directly or via a group. |
||
472 | * |
||
473 | * @todo This could be made more efficient. |
||
474 | * @return ArrayList |
||
475 | */ |
||
476 | View Code Duplication | public function getAssignedMembers() |
|
490 | |||
491 | /** |
||
492 | * |
||
493 | * @param Member $member |
||
494 | * @return boolean |
||
495 | */ |
||
496 | public function canView($member = null) |
||
517 | |||
518 | /** |
||
519 | * |
||
520 | * @param Member $member |
||
521 | * @return boolean |
||
522 | */ |
||
523 | public function canEdit($member = null) |
||
532 | |||
533 | /** |
||
534 | * |
||
535 | * @param Member $member |
||
536 | * @return boolean |
||
537 | */ |
||
538 | public function canDelete($member = null) |
||
550 | |||
551 | /** |
||
552 | * Checks whether the given user is in the list of users assigned to this |
||
553 | * workflow |
||
554 | * |
||
555 | * @param Member $member |
||
556 | */ |
||
557 | protected function userHasAccess($member) |
||
584 | |||
585 | /** |
||
586 | * Can documents in the current workflow state be edited? |
||
587 | */ |
||
588 | public function canEditTarget() |
||
594 | |||
595 | /** |
||
596 | * Does this action restrict viewing of the document? |
||
597 | * |
||
598 | * @return boolean |
||
599 | */ |
||
600 | public function canViewTarget() |
||
608 | |||
609 | /** |
||
610 | * Does this action restrict the publishing of a document? |
||
611 | * |
||
612 | * @return boolean |
||
613 | */ |
||
614 | public function canPublishTarget() |
||
620 | |||
621 | /** |
||
622 | * Get the current set of transitions that are valid for the current workflow state, |
||
623 | * and are available to the current user. |
||
624 | * |
||
625 | * @return array |
||
626 | */ |
||
627 | public function validTransitions() |
||
637 | |||
638 | /* UI RELATED METHODS */ |
||
639 | |||
640 | /** |
||
641 | * Gets fields for managing this workflow instance in its current step |
||
642 | * |
||
643 | * @return FieldList |
||
644 | */ |
||
645 | public function getWorkflowFields() |
||
661 | |||
662 | /** |
||
663 | * Gets Front-End form fields from current Action |
||
664 | * |
||
665 | * @return FieldList |
||
666 | */ |
||
667 | public function getFrontEndWorkflowFields() |
||
676 | |||
677 | /** |
||
678 | * Gets Transitions for display as Front-End Form Actions |
||
679 | * |
||
680 | * @return FieldList |
||
681 | */ |
||
682 | public function getFrontEndWorkflowActions() |
||
715 | |||
716 | /** |
||
717 | * Gets Front-End DataObject |
||
718 | * |
||
719 | * @return DataObject |
||
720 | */ |
||
721 | public function getFrontEndDataObject() |
||
728 | |||
729 | /** |
||
730 | * Gets Front-End DataObject |
||
731 | * |
||
732 | * @return DataObject |
||
733 | */ |
||
734 | public function getFrontEndRequiredFields() |
||
741 | |||
742 | public function setFrontendFormRequirements() |
||
747 | |||
748 | public function doFrontEndAction(array $data, Form $form, HTTPRequest $request) |
||
753 | |||
754 | /** |
||
755 | * We need a way to "associate" an author with this WorkflowInstance and its Target() to see if she is "allowed" |
||
756 | * to view WorkflowInstances within GridFields |
||
757 | * @see {@link $this->userHasAccess()} |
||
758 | * |
||
759 | * @param number $recordID |
||
760 | * @param number $userID |
||
761 | * @param number $wasPublished |
||
762 | * @return boolean |
||
763 | */ |
||
764 | public function getVersionedConnection($recordID, $userID, $wasPublished = 0) |
||
777 | |||
778 | /** |
||
779 | * Simple method to retrieve the current action, on the current WorkflowInstance |
||
780 | */ |
||
781 | public function getCurrentAction() |
||
794 | |||
795 | /** |
||
796 | * Tells us if $member has had permissions over some part of the current WorkflowInstance. |
||
797 | * |
||
798 | * @param $member |
||
799 | * @return WorkflowAction|boolean |
||
800 | */ |
||
801 | public function getMostRecentActionForUser($member = null) |
||
831 | } |
||
832 |