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