| Conditions | 9 |
| Paths | 11 |
| Total Lines | 71 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 55 | public function process() |
||
| 56 | { |
||
| 57 | $sent = 0; |
||
| 58 | $filter = [ |
||
| 59 | 'WorkflowStatus' => ['Active', 'Paused'], |
||
| 60 | 'Definition.RemindDays:GreaterThan' => 0 |
||
| 61 | ]; |
||
| 62 | |||
| 63 | $active = WorkflowInstance::get()->filter($filter); |
||
| 64 | |||
| 65 | foreach ($active as $instance) { |
||
| 66 | $edited = strtotime($instance->LastEdited); |
||
| 67 | $days = $instance->Definition()->RemindDays; |
||
| 68 | |||
| 69 | if ($edited + ($days * 3600 * 24) > time()) { |
||
| 70 | continue; |
||
| 71 | } |
||
| 72 | |||
| 73 | $email = Email::create(); |
||
| 74 | $bcc = ''; |
||
|
|
|||
| 75 | $members = $instance->getAssignedMembers(); |
||
| 76 | $target = $instance->getTarget(); |
||
| 77 | |||
| 78 | if (!$members || !$members->exists()) { |
||
| 79 | continue; |
||
| 80 | } |
||
| 81 | |||
| 82 | $email->setSubject("Workflow Reminder: $instance->Title"); |
||
| 83 | $email->setBcc(implode(', ', $members->column('Email'))); |
||
| 84 | $email->setHTMLTemplate('WorkflowReminderEmail'); |
||
| 85 | $email->setData(array( |
||
| 86 | 'Instance' => $instance, |
||
| 87 | 'Link' => $target instanceof SiteTree ? "admin/show/$target->ID" : '' |
||
| 88 | )); |
||
| 89 | |||
| 90 | try { |
||
| 91 | $email->send(); |
||
| 92 | } catch (Exception $ex) { |
||
| 93 | Injector::inst()->get(LoggerInterface::class)->warning($ex->getMessage()); |
||
| 94 | } |
||
| 95 | |||
| 96 | $sent++; |
||
| 97 | |||
| 98 | // add a comment to the workflow if possible |
||
| 99 | $action = $instance->CurrentAction(); |
||
| 100 | |||
| 101 | $currentComment = $action->Comment; |
||
| 102 | $action->Comment = sprintf(_t( |
||
| 103 | 'AdvancedWorkflow.JOB_REMINDER_COMMENT', |
||
| 104 | '%s: Reminder email sent\n\n' |
||
| 105 | ), date('Y-m-d H:i:s')) . $currentComment; |
||
| 106 | try { |
||
| 107 | $action->write(); |
||
| 108 | } catch (Exception $ex) { |
||
| 109 | Injector::inst()->get(LoggerInterface::class)->warning($ex->getMessage()); |
||
| 110 | } |
||
| 111 | |||
| 112 | $instance->LastEdited = time(); |
||
| 113 | try { |
||
| 114 | $instance->write(); |
||
| 115 | } catch (Exception $ex) { |
||
| 116 | Injector::inst()->get(LoggerInterface::class)->warning($ex->getMessage()); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | $this->currentStep = 2; |
||
| 121 | $this->isComplete = true; |
||
| 122 | |||
| 123 | $nextDate = date('Y-m-d H:i:s', time() + $this->repeatInterval); |
||
| 124 | $this->queuedJobService->queueJob(new WorkflowReminderJob($this->repeatInterval), $nextDate); |
||
| 125 | } |
||
| 126 | } |
||
| 127 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.