Complex classes like WorkflowApplicable 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 WorkflowApplicable, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class WorkflowApplicable extends DataExtension |
||
| 40 | { |
||
| 41 | private static $has_one = [ |
||
| 42 | 'WorkflowDefinition' => WorkflowDefinition::class, |
||
| 43 | ]; |
||
| 44 | |||
| 45 | private static $many_many = [ |
||
| 46 | 'AdditionalWorkflowDefinitions' => WorkflowDefinition::class |
||
| 47 | ]; |
||
| 48 | |||
| 49 | private static $dependencies = [ |
||
| 50 | 'workflowService' => '%$' . WorkflowService::class, |
||
| 51 | ]; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * |
||
| 55 | * Used to flag to this extension if there's a WorkflowPublishTargetJob running. |
||
| 56 | * @var boolean |
||
| 57 | */ |
||
| 58 | public $isPublishJobRunning = false; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * |
||
| 62 | * @param boolean $truth |
||
| 63 | */ |
||
| 64 | public function setIsPublishJobRunning($truth) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * |
||
| 71 | * @return boolean |
||
| 72 | */ |
||
| 73 | public function getIsPublishJobRunning() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * |
||
| 80 | * @see {@link $this->isPublishJobRunning} |
||
| 81 | * @return boolean |
||
| 82 | */ |
||
| 83 | public function isPublishJobRunning() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var WorkflowService |
||
| 91 | */ |
||
| 92 | public $workflowService; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * |
||
| 96 | * A cache var for the current workflow instance |
||
| 97 | * |
||
| 98 | * @var WorkflowInstance |
||
| 99 | */ |
||
| 100 | protected $currentInstance; |
||
| 101 | |||
| 102 | public function updateSettingsFields(FieldList $fields) |
||
| 106 | |||
| 107 | public function updateCMSFields(FieldList $fields) |
||
| 120 | |||
| 121 | public function updateFields(FieldList $fields) |
||
| 179 | |||
| 180 | public function updateCMSActions(FieldList $actions) |
||
| 181 | { |
||
| 182 | $active = $this->getWorkflowService()->getWorkflowFor($this->owner); |
||
| 183 | $c = Controller::curr(); |
||
| 184 | if ($c && $c->hasExtension(AdvancedWorkflowExtension::class) && !$this->owner->isArchived()) { |
||
| 185 | if ($active) { |
||
| 186 | if ($this->canEditWorkflow()) { |
||
| 187 | $workflowOptions = new Tab( |
||
| 188 | 'WorkflowOptions', |
||
| 189 | _t( |
||
| 190 | 'SiteTree.WorkflowOptions', |
||
| 191 | 'Workflow options', |
||
| 192 | 'Expands a view for workflow specific buttons' |
||
| 193 | ) |
||
| 194 | ); |
||
| 195 | |||
| 196 | $menu = $actions->fieldByName('ActionMenus'); |
||
| 197 | if (!$menu) { |
||
| 198 | // create the menu for adding to any arbitrary non-sitetree object |
||
| 199 | $menu = $this->createActionMenu(); |
||
| 200 | $actions->push($menu); |
||
| 201 | } |
||
| 202 | |||
| 203 | if (!$actions->fieldByName('ActionMenus.WorkflowOptions')) { |
||
| 204 | $menu->push($workflowOptions); |
||
| 205 | } |
||
| 206 | |||
| 207 | $transitions = $active->CurrentAction()->getValidTransitions(); |
||
| 208 | |||
| 209 | foreach ($transitions as $transition) { |
||
| 210 | if ($transition->canExecute($active)) { |
||
| 211 | $action = FormAction::create('updateworkflow-' . $transition->ID, $transition->Title) |
||
| 212 | ->setAttribute('data-transitionid', $transition->ID); |
||
| 213 | $workflowOptions->push($action); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | // $action = FormAction::create('updateworkflow', $active->CurrentAction() ? |
||
| 218 | // $active->CurrentAction()->Title : |
||
| 219 | // _t('WorkflowApplicable.UPDATE_WORKFLOW', 'Update Workflow')) |
||
| 220 | // ->setAttribute('data-icon', 'navigation'); |
||
| 221 | |||
| 222 | // $actions->fieldByName('MajorActions') ? |
||
| 223 | // $actions->fieldByName('MajorActions')->push($action) : |
||
| 224 | // $actions->push($action); |
||
| 225 | } |
||
| 226 | } else { |
||
| 227 | // Instantiate the workflow definition initial actions. |
||
| 228 | $definitions = $this->getWorkflowService()->getDefinitionsFor($this->owner); |
||
| 229 | if ($definitions) { |
||
| 230 | $menu = $actions->fieldByName('ActionMenus'); |
||
| 231 | if (is_null($menu)) { |
||
| 232 | // Instantiate a new action menu for any data objects. |
||
| 233 | |||
| 234 | $menu = $this->createActionMenu(); |
||
| 235 | $actions->push($menu); |
||
| 236 | } |
||
| 237 | $tab = Tab::create( |
||
| 238 | 'AdditionalWorkflows' |
||
| 239 | ); |
||
| 240 | $addedFirst = false; |
||
| 241 | foreach ($definitions as $definition) { |
||
| 242 | if ($definition->getInitialAction() && $this->owner->canEdit()) { |
||
| 243 | $action = FormAction::create( |
||
| 244 | "startworkflow-{$definition->ID}", |
||
| 245 | $definition->InitialActionButtonText ? |
||
| 246 | $definition->InitialActionButtonText : |
||
| 247 | $definition->getInitialAction()->Title |
||
| 248 | ) |
||
| 249 | ->addExtraClass('start-workflow') |
||
| 250 | ->setAttribute('data-workflow', $definition->ID) |
||
| 251 | ->addExtraClass('btn-primary'); |
||
| 252 | |||
| 253 | // The first element is the main workflow definition, |
||
| 254 | // and will be displayed as a major action. |
||
| 255 | if (!$addedFirst) { |
||
| 256 | $addedFirst = true; |
||
| 257 | $action->setAttribute('data-icon', 'navigation'); |
||
| 258 | $majorActions = $actions->fieldByName('MajorActions'); |
||
| 259 | $majorActions ? $majorActions->push($action) : $actions->push($action); |
||
| 260 | } else { |
||
| 261 | $tab->push($action); |
||
| 262 | } |
||
| 263 | } |
||
| 264 | } |
||
| 265 | // Only display menu if actions pushed to it |
||
| 266 | if ($tab->Fields()->exists()) { |
||
| 267 | $menu->insertBefore($tab, 'MoreOptions'); |
||
| 268 | } |
||
| 269 | } |
||
| 270 | } |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | protected function createActionMenu() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Included in CMS-generated email templates for a NotifyUsersWorkflowAction. |
||
| 283 | * Returns an absolute link to the CMS UI for a Page object |
||
| 284 | * |
||
| 285 | * @return string|null |
||
| 286 | */ |
||
| 287 | public function AbsoluteEditLink() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Included in CMS-generated email templates for a NotifyUsersWorkflowAction. |
||
| 306 | * Allows users to select a link in an email for direct access to the transition-selection dropdown in the CMS UI. |
||
| 307 | * |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | public function LinkToPendingItems() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * After a workflow item is written, we notify the |
||
| 320 | * workflow so that it can take action if needbe |
||
| 321 | */ |
||
| 322 | public function onAfterWrite() |
||
| 329 | |||
| 330 | public function WorkflowInstances() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Gets the current instance of workflow |
||
| 340 | * |
||
| 341 | * @return WorkflowInstance |
||
| 342 | */ |
||
| 343 | public function getWorkflowInstance() |
||
| 351 | |||
| 352 | |||
| 353 | /** |
||
| 354 | * Gets the history of a workflow instance |
||
| 355 | * |
||
| 356 | * @return DataList |
||
| 357 | */ |
||
| 358 | public function getWorkflowHistory($limit = null) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Check all recent WorkflowActionIntances and return the most recent one with a Comment |
||
| 365 | * |
||
| 366 | * @param int $limit |
||
| 367 | * @return WorkflowActionInstance|null |
||
| 368 | */ |
||
| 369 | public function RecentWorkflowComment($limit = 10) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Content can never be directly publishable if there's a workflow applied. |
||
| 382 | * |
||
| 383 | * If there's an active instance, then it 'might' be publishable |
||
| 384 | */ |
||
| 385 | public function canPublish() |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Can only edit content that's NOT in another person's content changeset |
||
| 416 | * |
||
| 417 | * @return bool |
||
| 418 | */ |
||
| 419 | public function canEdit($member) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Can a user edit the current workflow attached to this item? |
||
| 433 | * |
||
| 434 | * @return bool |
||
| 435 | */ |
||
| 436 | public function canEditWorkflow() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @param WorkflowService $workflowService |
||
| 447 | * @return $this |
||
| 448 | */ |
||
| 449 | public function setWorkflowService(WorkflowService $workflowService) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @return WorkflowService |
||
| 457 | */ |
||
| 458 | public function getWorkflowService() |
||
| 462 | } |
||
| 463 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.