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 | private static $db = array( |
||
| 20 | 'Title' => 'Varchar(128)', |
||
| 21 | 'WorkflowStatus' => "Enum('Active,Paused,Complete,Cancelled','Active')", |
||
| 22 | 'TargetClass' => 'Varchar(64)', |
||
| 23 | 'TargetID' => 'Int', |
||
| 24 | ); |
||
| 25 | |||
| 26 | private static $has_one = array( |
||
| 27 | 'Definition' => 'WorkflowDefinition', |
||
| 28 | 'CurrentAction' => 'WorkflowActionInstance', |
||
| 29 | 'Initiator' => 'Member', |
||
| 30 | ); |
||
| 31 | |||
| 32 | private static $has_many = array( |
||
| 33 | 'Actions' => 'WorkflowActionInstance', |
||
| 34 | ); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The list of users who are responsible for performing the current WorkflowAction |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | private static $many_many = array( |
||
| 42 | 'Users' => 'Member', |
||
| 43 | 'Groups' => 'Group' |
||
| 44 | ); |
||
| 45 | |||
| 46 | private static $summary_fields = array( |
||
| 47 | 'Title', |
||
| 48 | 'WorkflowStatus', |
||
| 49 | 'Created' |
||
| 50 | ); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * If set to true, actions that cannot be executed by the user will not show |
||
| 54 | * on the frontend (just like the backend). |
||
| 55 | * |
||
| 56 | * @var boolean |
||
| 57 | */ |
||
| 58 | private static $hide_disabled_actions_on_frontend = false; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Get the CMS view of the instance. This is used to display the log of |
||
| 62 | * this workflow, and options to reassign if the workflow hasn't been |
||
| 63 | * finished yet |
||
| 64 | * |
||
| 65 | * @return \FieldList |
||
| 66 | */ |
||
| 67 | public function getCMSFields() { |
||
| 111 | |||
| 112 | public function fieldLabels($includerelations = true) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * See if we've been saved in context of managing the workflow directly |
||
| 124 | */ |
||
| 125 | public function onBeforeWrite() { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Update the current state of the workflow |
||
| 139 | * |
||
| 140 | * Typically, this is triggered by someone modifiying the workflow instance via the modeladmin form |
||
| 141 | * side of things when administering things, such as re-assigning or manually approving a stuck workflow |
||
| 142 | * |
||
| 143 | * Note that this is VERY similar to AdvancedWorkflowExtension::updateworkflow |
||
| 144 | * but without the formy bits. These two implementations should PROBABLY |
||
| 145 | * be merged |
||
| 146 | * |
||
| 147 | * @todo refactor with AdvancedWorkflowExtension |
||
| 148 | * |
||
| 149 | * @param type $data |
||
| 150 | * @return |
||
| 151 | */ |
||
| 152 | public function updateWorkflow($data) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Get the target-object that this WorkflowInstance "points" to. |
||
| 179 | * |
||
| 180 | * Workflows are not restricted to being active on SiteTree objects, |
||
| 181 | * so we need to account for being attached to anything. |
||
| 182 | * |
||
| 183 | * Sets Versioned::set_reading_mode() to allow fetching of Draft _and_ Published |
||
| 184 | * content. |
||
| 185 | * |
||
| 186 | * @return (null | DataObject) |
||
| 187 | */ |
||
| 188 | public function getTarget() { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * |
||
| 202 | * @see {@link {$this->getTarget()} |
||
| 203 | * @return (null | DataObject) |
||
| 204 | */ |
||
| 205 | public function Target() { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Start a workflow based on a particular definition for a particular object. |
||
| 211 | * |
||
| 212 | * The object is optional; if not specified, it is assumed that this workflow |
||
| 213 | * is simply a task based checklist type of workflow. |
||
| 214 | * |
||
| 215 | * @param WorkflowDefinition $definition |
||
| 216 | * @param DataObject $for |
||
| 217 | */ |
||
| 218 | public function beginWorkflow(WorkflowDefinition $definition, DataObject $for=null) { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Execute this workflow. In rare cases this will actually execute all actions, |
||
| 249 | * but typically, it will stop and wait for the user to input something |
||
| 250 | * |
||
| 251 | * The basic process is to get the current action, and see whether it has been finished |
||
| 252 | * by some process, if not it attempts to execute it. |
||
| 253 | * |
||
| 254 | * If it has been finished, we check to see if there's some transitions to follow. If there's |
||
| 255 | * only one transition, then we execute that immediately. |
||
| 256 | * |
||
| 257 | * If there's multiple transitions, we just stop and wait for the user to manually |
||
| 258 | * trigger a transition. |
||
| 259 | * |
||
| 260 | * If there's no transitions, we make the assumption that we've finished the workflow and |
||
| 261 | * mark it as such. |
||
| 262 | * |
||
| 263 | * |
||
| 264 | */ |
||
| 265 | 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) { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Transitions a workflow to the next step defined by the given transition. |
||
| 328 | * |
||
| 329 | * After transitioning, the action is 'executed', and next steps |
||
| 330 | * determined. |
||
| 331 | * |
||
| 332 | * @param WorkflowTransition $transition |
||
| 333 | */ |
||
| 334 | public function performTransition(WorkflowTransition $transition) { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Returns a list of all Members that are assigned to this instance, either directly or via a group. |
||
| 366 | * |
||
| 367 | * @todo This could be made more efficient. |
||
| 368 | * @return ArrayList |
||
| 369 | */ |
||
| 370 | View Code Duplication | public function getAssignedMembers() { |
|
| 383 | |||
| 384 | /** |
||
| 385 | * |
||
| 386 | * @param \Member $member |
||
| 387 | * @return boolean |
||
| 388 | */ |
||
| 389 | public function canView($member=null) { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * |
||
| 409 | * @param \Member $member |
||
| 410 | * @return boolean |
||
| 411 | */ |
||
| 412 | public function canEdit($member = null) { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * |
||
| 421 | * @param \Member $member |
||
| 422 | * @return boolean |
||
| 423 | */ |
||
| 424 | public function canDelete($member = null) { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Checks whether the given user is in the list of users assigned to this |
||
| 436 | * workflow |
||
| 437 | * |
||
| 438 | * @param $memberID |
||
| 439 | */ |
||
| 440 | protected function userHasAccess($member) { |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Can documents in the current workflow state be edited? |
||
| 467 | */ |
||
| 468 | public function canEditTarget() { |
||
| 469 | if ($this->CurrentActionID && ($target = $this->getTarget())) { |
||
| 470 | return $this->CurrentAction()->canEditTarget($target); |
||
| 471 | } |
||
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Does this action restrict viewing of the document? |
||
| 476 | * |
||
| 477 | * @return boolean |
||
| 478 | */ |
||
| 479 | public function canViewTarget() { |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Does this action restrict the publishing of a document? |
||
| 489 | * |
||
| 490 | * @return boolean |
||
| 491 | */ |
||
| 492 | public function canPublishTarget() { |
||
| 493 | if ($this->CurrentActionID && ($target = $this->getTarget())) { |
||
| 494 | return $this->CurrentAction()->canPublishTarget($target); |
||
| 495 | } |
||
| 496 | } |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Get the current set of transitions that are valid for the current workflow state, |
||
| 500 | * and are available to the current user. |
||
| 501 | * |
||
| 502 | * @return array |
||
| 503 | */ |
||
| 504 | public function validTransitions() { |
||
| 514 | |||
| 515 | /* UI RELATED METHODS */ |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Gets fields for managing this workflow instance in its current step |
||
| 519 | * |
||
| 520 | * @return FieldList |
||
| 521 | */ |
||
| 522 | public function getWorkflowFields() { |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Gets Front-End form fields from current Action |
||
| 540 | * |
||
| 541 | * @return FieldList |
||
| 542 | */ |
||
| 543 | public function getFrontEndWorkflowFields() { |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Gets Transitions for display as Front-End Form Actions |
||
| 554 | * |
||
| 555 | * @return FieldList |
||
| 556 | */ |
||
| 557 | public function getFrontEndWorkflowActions() { |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Gets Front-End DataObject |
||
| 594 | * |
||
| 595 | * @return DataObject |
||
| 596 | */ |
||
| 597 | public function getFrontEndDataObject() { |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Gets Front-End DataObject |
||
| 606 | * |
||
| 607 | * @return DataObject |
||
| 608 | */ |
||
| 609 | public function getFrontEndRequiredFields() { |
||
| 615 | |||
| 616 | public function setFrontendFormRequirements() { |
||
| 620 | |||
| 621 | public function doFrontEndAction(array $data, Form $form, SS_HTTPRequest $request) { |
||
| 625 | |||
| 626 | /* |
||
| 627 | * 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 |
||
| 628 | * @see {@link $this->userHasAccess()} |
||
| 629 | * |
||
| 630 | * @param number $recordID |
||
| 631 | * @param number $userID |
||
| 632 | * @param number $wasPublished |
||
| 633 | * @return boolean |
||
| 634 | */ |
||
| 635 | public function getVersionedConnection($recordID, $userID, $wasPublished = 0) { |
||
| 647 | |||
| 648 | /* |
||
| 649 | * Simple method to retrieve the current action, on the current WorkflowInstance |
||
| 650 | */ |
||
| 651 | public function getCurrentAction() { |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Tells us if $member has had permissions over some part of the current WorkflowInstance. |
||
| 665 | * |
||
| 666 | * @param $member |
||
| 667 | * @return \WorkflowAction | boolean |
||
| 668 | */ |
||
| 669 | public function getMostRecentActionForUser($member = null) { |
||
| 697 | } |
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.