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) |
||
267 | |||
268 | /** |
||
269 | * |
||
270 | * @param boolean $getLive |
||
271 | * @see {@link {$this->getTarget()} |
||
272 | * @return null|DataObject |
||
273 | */ |
||
274 | public function Target($getLive = false) |
||
278 | |||
279 | /** |
||
280 | * Returns the field differences between the older version and current version of Target |
||
281 | * |
||
282 | * @return ArrayList |
||
283 | */ |
||
284 | public function getTargetDiff() |
||
296 | |||
297 | /** |
||
298 | * Start a workflow based on a particular definition for a particular object. |
||
299 | * |
||
300 | * The object is optional; if not specified, it is assumed that this workflow |
||
301 | * is simply a task based checklist type of workflow. |
||
302 | * |
||
303 | * @param WorkflowDefinition $definition |
||
304 | * @param DataObject $for |
||
305 | */ |
||
306 | public function beginWorkflow(WorkflowDefinition $definition, DataObject $for = null) |
||
338 | |||
339 | /** |
||
340 | * Execute this workflow. In rare cases this will actually execute all actions, |
||
341 | * but typically, it will stop and wait for the user to input something |
||
342 | * |
||
343 | * The basic process is to get the current action, and see whether it has been finished |
||
344 | * by some process, if not it attempts to execute it. |
||
345 | * |
||
346 | * If it has been finished, we check to see if there's some transitions to follow. If there's |
||
347 | * only one transition, then we execute that immediately. |
||
348 | * |
||
349 | * If there's multiple transitions, we just stop and wait for the user to manually |
||
350 | * trigger a transition. |
||
351 | * |
||
352 | * If there's no transitions, we make the assumption that we've finished the workflow and |
||
353 | * mark it as such. |
||
354 | * |
||
355 | * |
||
356 | */ |
||
357 | public function execute() |
||
405 | |||
406 | /** |
||
407 | * Evaluate all the transitions of an action and determine whether we should |
||
408 | * follow any of them yet. |
||
409 | * |
||
410 | * @param WorkflowActionInstance $action |
||
411 | * @return WorkflowTransition |
||
412 | */ |
||
413 | protected function checkTransitions(WorkflowActionInstance $action) |
||
422 | |||
423 | /** |
||
424 | * Transitions a workflow to the next step defined by the given transition. |
||
425 | * |
||
426 | * After transitioning, the action is 'executed', and next steps |
||
427 | * determined. |
||
428 | * |
||
429 | * @param WorkflowTransition $transition |
||
430 | */ |
||
431 | public function performTransition(WorkflowTransition $transition) |
||
463 | |||
464 | /** |
||
465 | * Returns a list of all Members that are assigned to this instance, either directly or via a group. |
||
466 | * |
||
467 | * @todo This could be made more efficient. |
||
468 | * @return ArrayList |
||
469 | */ |
||
470 | View Code Duplication | public function getAssignedMembers() |
|
484 | |||
485 | /** |
||
486 | * |
||
487 | * @param Member $member |
||
488 | * @return boolean |
||
489 | */ |
||
490 | public function canView($member = null) |
||
511 | |||
512 | /** |
||
513 | * |
||
514 | * @param Member $member |
||
515 | * @return boolean |
||
516 | */ |
||
517 | public function canEdit($member = null) |
||
526 | |||
527 | /** |
||
528 | * |
||
529 | * @param Member $member |
||
530 | * @return boolean |
||
531 | */ |
||
532 | public function canDelete($member = null) |
||
544 | |||
545 | /** |
||
546 | * Checks whether the given user is in the list of users assigned to this |
||
547 | * workflow |
||
548 | * |
||
549 | * @param Member $member |
||
550 | */ |
||
551 | protected function userHasAccess($member) |
||
578 | |||
579 | /** |
||
580 | * Can documents in the current workflow state be edited? |
||
581 | */ |
||
582 | public function canEditTarget() |
||
588 | |||
589 | /** |
||
590 | * Does this action restrict viewing of the document? |
||
591 | * |
||
592 | * @return boolean |
||
593 | */ |
||
594 | public function canViewTarget() |
||
602 | |||
603 | /** |
||
604 | * Does this action restrict the publishing of a document? |
||
605 | * |
||
606 | * @return boolean |
||
607 | */ |
||
608 | public function canPublishTarget() |
||
614 | |||
615 | /** |
||
616 | * Get the current set of transitions that are valid for the current workflow state, |
||
617 | * and are available to the current user. |
||
618 | * |
||
619 | * @return array |
||
620 | */ |
||
621 | public function validTransitions() |
||
631 | |||
632 | /* UI RELATED METHODS */ |
||
633 | |||
634 | /** |
||
635 | * Gets fields for managing this workflow instance in its current step |
||
636 | * |
||
637 | * @return FieldList |
||
638 | */ |
||
639 | public function getWorkflowFields() |
||
655 | |||
656 | /** |
||
657 | * Gets Front-End form fields from current Action |
||
658 | * |
||
659 | * @return FieldList |
||
660 | */ |
||
661 | public function getFrontEndWorkflowFields() |
||
670 | |||
671 | /** |
||
672 | * Gets Transitions for display as Front-End Form Actions |
||
673 | * |
||
674 | * @return FieldList |
||
675 | */ |
||
676 | public function getFrontEndWorkflowActions() |
||
709 | |||
710 | /** |
||
711 | * Gets Front-End DataObject |
||
712 | * |
||
713 | * @return DataObject |
||
714 | */ |
||
715 | public function getFrontEndDataObject() |
||
722 | |||
723 | /** |
||
724 | * Gets Front-End DataObject |
||
725 | * |
||
726 | * @return DataObject |
||
727 | */ |
||
728 | public function getFrontEndRequiredFields() |
||
735 | |||
736 | public function setFrontendFormRequirements() |
||
741 | |||
742 | public function doFrontEndAction(array $data, Form $form, HTTPRequest $request) |
||
747 | |||
748 | /** |
||
749 | * We need a way to "associate" an author with this WorkflowInstance and its Target() to see if she is "allowed" |
||
750 | * to view WorkflowInstances within GridFields |
||
751 | * @see {@link $this->userHasAccess()} |
||
752 | * |
||
753 | * @param number $recordID |
||
754 | * @param number $userID |
||
755 | * @param number $wasPublished |
||
756 | * @return boolean |
||
757 | */ |
||
758 | public function getVersionedConnection($recordID, $userID, $wasPublished = 0) |
||
771 | |||
772 | /** |
||
773 | * Simple method to retrieve the current action, on the current WorkflowInstance |
||
774 | */ |
||
775 | public function getCurrentAction() |
||
788 | |||
789 | /** |
||
790 | * Tells us if $member has had permissions over some part of the current WorkflowInstance. |
||
791 | * |
||
792 | * @param $member |
||
793 | * @return WorkflowAction|boolean |
||
794 | */ |
||
795 | public function getMostRecentActionForUser($member = null) |
||
825 | } |
||
826 |