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:
| 1 | <?php |
||
| 29 | class WorkflowAction extends DataObject |
||
| 30 | { |
||
| 31 | private static $db = array( |
||
|
|
|||
| 32 | 'Title' => 'Varchar(255)', |
||
| 33 | 'Comment' => 'Text', |
||
| 34 | 'Type' => "Enum('Dynamic,Manual','Manual')", // is this used? |
||
| 35 | 'Executed' => 'Boolean', |
||
| 36 | 'AllowEditing' => "Enum('By Assignees,Content Settings,No','No')", // can this item be edited? |
||
| 37 | 'Sort' => 'Int', |
||
| 38 | 'AllowCommenting' => 'Boolean' |
||
| 39 | ); |
||
| 40 | |||
| 41 | private static $defaults = array( |
||
| 42 | 'AllowCommenting' => '1', |
||
| 43 | ); |
||
| 44 | |||
| 45 | private static $default_sort = 'Sort'; |
||
| 46 | |||
| 47 | private static $has_one = array( |
||
| 48 | 'WorkflowDef' => WorkflowDefinition::class, |
||
| 49 | 'Member' => Member::class |
||
| 50 | ); |
||
| 51 | |||
| 52 | private static $has_many = array( |
||
| 53 | 'Transitions' => WorkflowTransition::class . '.Action' |
||
| 54 | ); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The type of class to use for instances of this workflow action that are used for storing the |
||
| 58 | * data of the instance. |
||
| 59 | * |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | private static $instance_class = WorkflowActionInstance::class; |
||
| 63 | |||
| 64 | private static $icon = 'symbiote/silverstripe-advancedworkflow:images/action.png'; |
||
| 65 | |||
| 66 | private static $table_name = 'WorkflowAction'; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Can documents in the current workflow state be edited? |
||
| 70 | * |
||
| 71 | * Only return true or false if this is an absolute value; the WorkflowActionInstance |
||
| 72 | * will try and figure out an appropriate value for the actively running workflow |
||
| 73 | * if null is returned from this method. |
||
| 74 | * |
||
| 75 | * Admin level users can always edit. |
||
| 76 | * |
||
| 77 | * @param DataObject $target |
||
| 78 | * @return bool |
||
| 79 | */ |
||
| 80 | View Code Duplication | public function canEditTarget(DataObject $target) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Does this action restrict viewing of the document? |
||
| 92 | * |
||
| 93 | * @param DataObject $target |
||
| 94 | * @return bool |
||
| 95 | */ |
||
| 96 | public function canViewTarget(DataObject $target) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Does this action restrict the publishing of a document? |
||
| 103 | * |
||
| 104 | * @param DataObject $target |
||
| 105 | * @return bool |
||
| 106 | */ |
||
| 107 | public function canPublishTarget(DataObject $target) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Allows users who have permission to create a WorkflowDefinition, to create actions on it too. |
||
| 114 | * |
||
| 115 | * @param Member $member |
||
| 116 | * @param array $context |
||
| 117 | * @return bool |
||
| 118 | */ |
||
| 119 | public function canCreate($member = null, $context = array()) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param Member $member |
||
| 126 | * @return bool |
||
| 127 | */ |
||
| 128 | public function canEdit($member = null) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param Member $member |
||
| 135 | * @return bool |
||
| 136 | */ |
||
| 137 | public function canDelete($member = null) |
||
| 141 | |||
| 142 | /* |
||
| 143 | * If there is only a single action defined for a workflow, there's no sense |
||
| 144 | * in allowing users to add a transition to it (and causing errors). |
||
| 145 | * Hide the "Add Transition" button in this case |
||
| 146 | * |
||
| 147 | * @return boolean true if we should disable the button, false otherwise |
||
| 148 | */ |
||
| 149 | public function canAddTransition() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Gets an object that is used for saving the actual state of things during |
||
| 156 | * a running workflow. It still uses the workflow action def for managing the |
||
| 157 | * functional execution, however if you need to store additional data for |
||
| 158 | * the state, you can specify your own WorkflowActionInstance instead of |
||
| 159 | * the default to capture these elements |
||
| 160 | * |
||
| 161 | * @return WorkflowActionInstance |
||
| 162 | */ |
||
| 163 | public function getInstanceForWorkflow() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Perform whatever needs to be done for this action. If this action can be considered executed, then |
||
| 173 | * return true - if not (ie it needs some user input first), return false and 'execute' will be triggered |
||
| 174 | * again at a later point in time after the user has provided more data, either directly or indirectly. |
||
| 175 | * |
||
| 176 | * @param WorkflowInstance $workflow |
||
| 177 | * @return bool Returns true if this action has finished. |
||
| 178 | */ |
||
| 179 | public function execute(WorkflowInstance $workflow) |
||
| 183 | |||
| 184 | public function onBeforeWrite() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * When deleting an action from a workflow definition, make sure that workflows currently paused on that action |
||
| 195 | * are deleted |
||
| 196 | * Also removes all outbound transitions |
||
| 197 | */ |
||
| 198 | public function onAfterDelete() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Called when the current target of the workflow has been updated |
||
| 225 | */ |
||
| 226 | public function targetUpdated(WorkflowInstance $workflow) |
||
| 229 | |||
| 230 | /* CMS RELATED FUNCTIONALITY... */ |
||
| 231 | |||
| 232 | |||
| 233 | public function numChildren() |
||
| 237 | |||
| 238 | public function getCMSFields() |
||
| 270 | |||
| 271 | public function getValidator() |
||
| 275 | |||
| 276 | public function summaryFields() |
||
| 283 | |||
| 284 | public function fieldLabels($includerelations = true) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Used for Front End Workflows |
||
| 300 | */ |
||
| 301 | public function updateFrontendWorkflowFields($fields, $workflow) |
||
| 304 | |||
| 305 | public function Icon() |
||
| 310 | } |
||
| 311 |