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() |
||
307 | |||
308 | /** |
||
309 | * Start a workflow based on a particular definition for a particular object. |
||
310 | * |
||
311 | * The object is optional; if not specified, it is assumed that this workflow |
||
312 | * is simply a task based checklist type of workflow. |
||
313 | * |
||
314 | * @param WorkflowDefinition $definition |
||
315 | * @param DataObject $for |
||
316 | */ |
||
317 | public function beginWorkflow(WorkflowDefinition $definition, DataObject $for = null) |
||
349 | |||
350 | /** |
||
351 | * Execute this workflow. In rare cases this will actually execute all actions, |
||
352 | * but typically, it will stop and wait for the user to input something |
||
353 | * |
||
354 | * The basic process is to get the current action, and see whether it has been finished |
||
355 | * by some process, if not it attempts to execute it. |
||
356 | * |
||
357 | * If it has been finished, we check to see if there's some transitions to follow. If there's |
||
358 | * only one transition, then we execute that immediately. |
||
359 | * |
||
360 | * If there's multiple transitions, we just stop and wait for the user to manually |
||
361 | * trigger a transition. |
||
362 | * |
||
363 | * If there's no transitions, we make the assumption that we've finished the workflow and |
||
364 | * mark it as such. |
||
365 | * |
||
366 | * |
||
367 | */ |
||
368 | public function execute() |
||
416 | |||
417 | /** |
||
418 | * Evaluate all the transitions of an action and determine whether we should |
||
419 | * follow any of them yet. |
||
420 | * |
||
421 | * @param WorkflowActionInstance $action |
||
422 | * @return WorkflowTransition |
||
423 | */ |
||
424 | protected function checkTransitions(WorkflowActionInstance $action) |
||
433 | |||
434 | /** |
||
435 | * Transitions a workflow to the next step defined by the given transition. |
||
436 | * |
||
437 | * After transitioning, the action is 'executed', and next steps |
||
438 | * determined. |
||
439 | * |
||
440 | * @param WorkflowTransition $transition |
||
441 | */ |
||
442 | public function performTransition(WorkflowTransition $transition) |
||
474 | |||
475 | /** |
||
476 | * Returns a list of all Members that are assigned to this instance, either directly or via a group. |
||
477 | * |
||
478 | * @todo This could be made more efficient. |
||
479 | * @return ArrayList |
||
480 | */ |
||
481 | View Code Duplication | public function getAssignedMembers() |
|
495 | |||
496 | /** |
||
497 | * |
||
498 | * @param Member $member |
||
499 | * @return boolean |
||
500 | */ |
||
501 | public function canView($member = null) |
||
522 | |||
523 | /** |
||
524 | * |
||
525 | * @param Member $member |
||
526 | * @return boolean |
||
527 | */ |
||
528 | public function canEdit($member = null) |
||
537 | |||
538 | /** |
||
539 | * |
||
540 | * @param Member $member |
||
541 | * @return boolean |
||
542 | */ |
||
543 | public function canDelete($member = null) |
||
555 | |||
556 | /** |
||
557 | * Checks whether the given user is in the list of users assigned to this |
||
558 | * workflow |
||
559 | * |
||
560 | * @param Member $member |
||
561 | */ |
||
562 | protected function userHasAccess($member) |
||
589 | |||
590 | /** |
||
591 | * Can documents in the current workflow state be edited? |
||
592 | */ |
||
593 | public function canEditTarget() |
||
599 | |||
600 | /** |
||
601 | * Does this action restrict viewing of the document? |
||
602 | * |
||
603 | * @return boolean |
||
604 | */ |
||
605 | public function canViewTarget() |
||
613 | |||
614 | /** |
||
615 | * Does this action restrict the publishing of a document? |
||
616 | * |
||
617 | * @return boolean |
||
618 | */ |
||
619 | public function canPublishTarget() |
||
625 | |||
626 | /** |
||
627 | * Get the current set of transitions that are valid for the current workflow state, |
||
628 | * and are available to the current user. |
||
629 | * |
||
630 | * @return array |
||
631 | */ |
||
632 | public function validTransitions() |
||
642 | |||
643 | /* UI RELATED METHODS */ |
||
644 | |||
645 | /** |
||
646 | * Gets fields for managing this workflow instance in its current step |
||
647 | * |
||
648 | * @return FieldList |
||
649 | */ |
||
650 | public function getWorkflowFields() |
||
666 | |||
667 | /** |
||
668 | * Gets Front-End form fields from current Action |
||
669 | * |
||
670 | * @return FieldList |
||
671 | */ |
||
672 | public function getFrontEndWorkflowFields() |
||
681 | |||
682 | /** |
||
683 | * Gets Transitions for display as Front-End Form Actions |
||
684 | * |
||
685 | * @return FieldList |
||
686 | */ |
||
687 | public function getFrontEndWorkflowActions() |
||
720 | |||
721 | /** |
||
722 | * Gets Front-End DataObject |
||
723 | * |
||
724 | * @return DataObject |
||
725 | */ |
||
726 | public function getFrontEndDataObject() |
||
733 | |||
734 | /** |
||
735 | * Gets Front-End DataObject |
||
736 | * |
||
737 | * @return DataObject |
||
738 | */ |
||
739 | public function getFrontEndRequiredFields() |
||
746 | |||
747 | public function setFrontendFormRequirements() |
||
752 | |||
753 | public function doFrontEndAction(array $data, Form $form, HTTPRequest $request) |
||
758 | |||
759 | /** |
||
760 | * We need a way to "associate" an author with this WorkflowInstance and its Target() to see if she is "allowed" |
||
761 | * to view WorkflowInstances within GridFields |
||
762 | * @see {@link $this->userHasAccess()} |
||
763 | * |
||
764 | * @param number $recordID |
||
765 | * @param number $userID |
||
766 | * @param number $wasPublished |
||
767 | * @return boolean |
||
768 | */ |
||
769 | public function getVersionedConnection($recordID, $userID, $wasPublished = 0) |
||
782 | |||
783 | /** |
||
784 | * Simple method to retrieve the current action, on the current WorkflowInstance |
||
785 | */ |
||
786 | public function getCurrentAction() |
||
799 | |||
800 | /** |
||
801 | * Tells us if $member has had permissions over some part of the current WorkflowInstance. |
||
802 | * |
||
803 | * @param $member |
||
804 | * @return WorkflowAction|boolean |
||
805 | */ |
||
806 | public function getMostRecentActionForUser($member = null) |
||
836 | } |
||
837 |