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 |
||
17 | class WorkflowInstance extends DataObject |
||
|
|||
18 | { |
||
19 | |||
20 | private static $db = array( |
||
21 | 'Title' => 'Varchar(128)', |
||
22 | 'WorkflowStatus' => "Enum('Active,Paused,Complete,Cancelled','Active')", |
||
23 | 'TargetClass' => 'Varchar(64)', |
||
24 | 'TargetID' => 'Int', |
||
25 | ); |
||
26 | |||
27 | private static $has_one = array( |
||
28 | 'Definition' => 'WorkflowDefinition', |
||
29 | 'CurrentAction' => 'WorkflowActionInstance', |
||
30 | 'Initiator' => 'Member', |
||
31 | ); |
||
32 | |||
33 | private static $has_many = array( |
||
34 | 'Actions' => 'WorkflowActionInstance', |
||
35 | ); |
||
36 | |||
37 | /** |
||
38 | * The list of users who are responsible for performing the current WorkflowAction |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | private static $many_many = array( |
||
43 | 'Users' => 'Member', |
||
44 | 'Groups' => 'Group' |
||
45 | ); |
||
46 | |||
47 | private static $summary_fields = array( |
||
48 | 'Title', |
||
49 | 'WorkflowStatus', |
||
50 | 'Created' |
||
51 | ); |
||
52 | |||
53 | /** |
||
54 | * Get the CMS view of the instance. This is used to display the log of |
||
55 | * this workflow, and options to reassign if the workflow hasn't been |
||
56 | * finished yet |
||
57 | * |
||
58 | * @return \FieldList |
||
59 | */ |
||
60 | public function getCMSFields() |
||
104 | |||
105 | public function fieldLabels($includerelations = true) |
||
115 | |||
116 | /** |
||
117 | * See if we've been saved in context of managing the workflow directly |
||
118 | */ |
||
119 | public function onBeforeWrite() |
||
131 | |||
132 | /** |
||
133 | * Update the current state of the workflow |
||
134 | * |
||
135 | * Typically, this is triggered by someone modifiying the workflow instance via the modeladmin form |
||
136 | * side of things when administering things, such as re-assigning or manually approving a stuck workflow |
||
137 | * |
||
138 | * Note that this is VERY similar to AdvancedWorkflowExtension::updateworkflow |
||
139 | * but without the formy bits. These two implementations should PROBABLY |
||
140 | * be merged |
||
141 | * |
||
142 | * @todo refactor with AdvancedWorkflowExtension |
||
143 | * |
||
144 | * @param type $data |
||
145 | * @return |
||
146 | */ |
||
147 | public function updateWorkflow($data) |
||
172 | |||
173 | /** |
||
174 | * Get the target-object that this WorkflowInstance "points" to. |
||
175 | * |
||
176 | * Workflows are not restricted to being active on SiteTree objects, |
||
177 | * so we need to account for being attached to anything. |
||
178 | * |
||
179 | * Sets Versioned::set_reading_mode() to allow fetching of Draft _and_ Published |
||
180 | * content. |
||
181 | * |
||
182 | * @return (null | DataObject) |
||
183 | */ |
||
184 | public function getTarget() |
||
196 | |||
197 | /** |
||
198 | * |
||
199 | * @see {@link {$this->getTarget()} |
||
200 | * @return (null | DataObject) |
||
201 | */ |
||
202 | public function Target() |
||
206 | |||
207 | /** |
||
208 | * Start a workflow based on a particular definition for a particular object. |
||
209 | * |
||
210 | * The object is optional; if not specified, it is assumed that this workflow |
||
211 | * is simply a task based checklist type of workflow. |
||
212 | * |
||
213 | * @param WorkflowDefinition $definition |
||
214 | * @param DataObject $for |
||
215 | */ |
||
216 | public function beginWorkflow(WorkflowDefinition $definition, DataObject $for=null) |
||
245 | |||
246 | /** |
||
247 | * Execute this workflow. In rare cases this will actually execute all actions, |
||
248 | * but typically, it will stop and wait for the user to input something |
||
249 | * |
||
250 | * The basic process is to get the current action, and see whether it has been finished |
||
251 | * by some process, if not it attempts to execute it. |
||
252 | * |
||
253 | * If it has been finished, we check to see if there's some transitions to follow. If there's |
||
254 | * only one transition, then we execute that immediately. |
||
255 | * |
||
256 | * If there's multiple transitions, we just stop and wait for the user to manually |
||
257 | * trigger a transition. |
||
258 | * |
||
259 | * If there's no transitions, we make the assumption that we've finished the workflow and |
||
260 | * mark it as such. |
||
261 | * |
||
262 | * |
||
263 | */ |
||
264 | public function execute() |
||
309 | |||
310 | /** |
||
311 | * Evaluate all the transitions of an action and determine whether we should |
||
312 | * follow any of them yet. |
||
313 | * |
||
314 | * @param WorkflowActionInstance $action |
||
315 | * @return WorkflowTransition |
||
316 | */ |
||
317 | protected function checkTransitions(WorkflowActionInstance $action) |
||
326 | |||
327 | /** |
||
328 | * Transitions a workflow to the next step defined by the given transition. |
||
329 | * |
||
330 | * After transitioning, the action is 'executed', and next steps |
||
331 | * determined. |
||
332 | * |
||
333 | * @param WorkflowTransition $transition |
||
334 | */ |
||
335 | public function performTransition(WorkflowTransition $transition) |
||
364 | |||
365 | /** |
||
366 | * Returns a list of all Members that are assigned to this instance, either directly or via a group. |
||
367 | * |
||
368 | * @todo This could be made more efficient. |
||
369 | * @return ArrayList |
||
370 | */ |
||
371 | View Code Duplication | public function getAssignedMembers() |
|
385 | |||
386 | /** |
||
387 | * |
||
388 | * @param \Member $member |
||
389 | * @return boolean |
||
390 | */ |
||
391 | public function canView($member=null) |
||
411 | |||
412 | /** |
||
413 | * |
||
414 | * @param \Member $member |
||
415 | * @return boolean |
||
416 | */ |
||
417 | public function canEdit($member = null) |
||
426 | |||
427 | /** |
||
428 | * |
||
429 | * @param \Member $member |
||
430 | * @return boolean |
||
431 | */ |
||
432 | public function canDelete($member = null) |
||
444 | |||
445 | /** |
||
446 | * Checks whether the given user is in the list of users assigned to this |
||
447 | * workflow |
||
448 | * |
||
449 | * @param $memberID |
||
450 | */ |
||
451 | protected function userHasAccess($member) |
||
476 | |||
477 | /** |
||
478 | * Can documents in the current workflow state be edited? |
||
479 | */ |
||
480 | public function canEditTarget() |
||
486 | |||
487 | /** |
||
488 | * Does this action restrict viewing of the document? |
||
489 | * |
||
490 | * @return boolean |
||
491 | */ |
||
492 | public function canViewTarget() |
||
500 | |||
501 | /** |
||
502 | * Does this action restrict the publishing of a document? |
||
503 | * |
||
504 | * @return boolean |
||
505 | */ |
||
506 | public function canPublishTarget() |
||
512 | |||
513 | /** |
||
514 | * Get the current set of transitions that are valid for the current workflow state, |
||
515 | * and are available to the current user. |
||
516 | * |
||
517 | * @return array |
||
518 | */ |
||
519 | public function validTransitions() |
||
530 | |||
531 | /* UI RELATED METHODS */ |
||
532 | |||
533 | /** |
||
534 | * Gets fields for managing this workflow instance in its current step |
||
535 | * |
||
536 | * @return FieldList |
||
537 | */ |
||
538 | public function getWorkflowFields() |
||
554 | |||
555 | /** |
||
556 | * Gets Front-End form fields from current Action |
||
557 | * |
||
558 | * @return FieldList |
||
559 | */ |
||
560 | public function getFrontEndWorkflowFields() |
||
569 | |||
570 | /** |
||
571 | * Gets Transitions for display as Front-End Form Actions |
||
572 | * |
||
573 | * @return FieldList |
||
574 | */ |
||
575 | public function getFrontEndWorkflowActions() |
||
602 | |||
603 | /** |
||
604 | * Gets Front-End DataObject |
||
605 | * |
||
606 | * @return DataObject |
||
607 | */ |
||
608 | public function getFrontEndDataObject() |
||
615 | |||
616 | /** |
||
617 | * Gets Front-End DataObject |
||
618 | * |
||
619 | * @return DataObject |
||
620 | */ |
||
621 | public function getFrontEndRequiredFields() |
||
628 | |||
629 | public function setFrontendFormRequirements() |
||
634 | |||
635 | public function doFrontEndAction(array $data, Form $form, SS_HTTPRequest $request) |
||
640 | |||
641 | /* |
||
642 | * We need a way to "associate" an author with this WorkflowInstance and its Target() to see if she is "allowed" to view WorkflowInstances within GridFields |
||
643 | * @see {@link $this->userHasAccess()} |
||
644 | * |
||
645 | * @param number $recordID |
||
646 | * @param number $userID |
||
647 | * @param number $wasPublished |
||
648 | * @return boolean |
||
649 | */ |
||
650 | public function getVersionedConnection($recordID, $userID, $wasPublished = 0) |
||
663 | |||
664 | /* |
||
665 | * Simple method to retrieve the current action, on the current WorkflowInstance |
||
666 | */ |
||
667 | public function getCurrentAction() |
||
679 | |||
680 | /** |
||
681 | * Tells us if $member has had permissions over some part of the current WorkflowInstance. |
||
682 | * |
||
683 | * @param $member |
||
684 | * @return \WorkflowAction | boolean |
||
685 | */ |
||
686 | public function getMostRecentActionForUser($member = null) |
||
715 | } |
||
716 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.