Conditions | 6 |
Paths | 52 |
Total Lines | 101 |
Code Lines | 58 |
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 namespace Comodojo\Extender\Task; |
||
73 | public function run(Request $request) { |
||
74 | |||
75 | $name = $request->getName(); |
||
76 | $task = $request->getTask(); |
||
77 | $uid = $request->getUid(); |
||
78 | $jid = $request->getJid(); |
||
79 | $puid = $request->getParentUid(); |
||
80 | $parameters = $request->getParameters(); |
||
81 | |||
82 | ob_start(); |
||
83 | |||
84 | try { |
||
85 | |||
86 | $this->stopwatch->start(); |
||
87 | |||
88 | $this->logger->notice("Starting new task $task ($name)"); |
||
89 | |||
90 | $thetask = $this->table->get($task)->getInstance($name, $parameters); |
||
91 | |||
92 | $this->events->emit( new TaskEvent('start', $thetask) ); |
||
93 | |||
94 | $pid = $thetask->getPid(); |
||
95 | |||
96 | $this->openWorklog( |
||
97 | $uid, |
||
98 | $puid, |
||
99 | $pid, |
||
100 | $name, |
||
101 | $jid, |
||
102 | $task, |
||
103 | $parameters, |
||
104 | $this->stopwatch->getStartTime() |
||
105 | ); |
||
106 | |||
107 | $this->installErrorHandler(); |
||
108 | |||
109 | try { |
||
110 | |||
111 | $result = $thetask->run(); |
||
112 | |||
113 | $status = Worklog::STATUS_COMPLETE; |
||
114 | |||
115 | $this->events->emit( new TaskEvent('complete', $thetask) ); |
||
116 | |||
117 | } catch (TaskException $te) { |
||
118 | |||
119 | $status = Worklog::STATUS_ABORT; |
||
120 | |||
121 | $result = $te->getMessage(); |
||
122 | |||
123 | $this->events->emit( new TaskEvent('abort', $thetask) ); |
||
124 | |||
125 | } catch (Exception $e) { |
||
126 | |||
127 | $status = Worklog::STATUS_ERROR; |
||
128 | |||
129 | $result = $e->getMessage(); |
||
130 | |||
131 | $this->events->emit( new TaskEvent('error', $thetask) ); |
||
132 | |||
133 | } |
||
134 | |||
135 | $this->restoreErrorHandler(); |
||
136 | |||
137 | $this->events->emit( new TaskEvent('stop', $thetask) ); |
||
138 | |||
139 | $this->stopwatch->stop(); |
||
140 | |||
141 | $this->closeWorklog($status, $result, $this->stopwatch->getStopTime()); |
||
142 | |||
143 | $drift = $this->stopwatch->getDrift()->format('%s'); |
||
144 | |||
145 | $this->logger->notice("Task $name ($task) pid $pid ends in ".($status === Worklog::STATUS_COMPLETE ? 'success' : 'error')." in $drift secs"); |
||
146 | |||
147 | } catch (Exception $e) { |
||
148 | |||
149 | ob_end_clean(); |
||
150 | |||
151 | throw $e; |
||
152 | |||
153 | } |
||
154 | |||
155 | $result = new Result([ |
||
156 | $uid, |
||
157 | $pid, |
||
158 | $jid, |
||
159 | $name, |
||
160 | $status === Worklog::STATUS_COMPLETE ? true : false, |
||
161 | $this->stopwatch->getStartTime(), |
||
162 | $this->stopwatch->getStopTime(), |
||
163 | $result, |
||
164 | $this->worklog_id |
||
165 | ]); |
||
166 | |||
167 | $this->stopwatch->clear(); |
||
168 | |||
169 | ob_end_clean(); |
||
170 | |||
171 | $this->events->emit( new TaskEvent(self::statusToEvent($status), $thetask, $result) ); |
||
|
|||
172 | |||
173 | return $result; |
||
174 | |||
299 |