Conditions | 7 |
Paths | 68 |
Total Lines | 85 |
Code Lines | 47 |
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; |
||
74 | public function run( |
||
75 | $name, |
||
76 | $task, |
||
77 | $jid = null, |
||
78 | TaskParameters $parameters = null |
||
79 | ) { |
||
80 | |||
81 | if ( is_null($parameters) ) $parameters = new TaskParameters(); |
||
82 | |||
83 | try { |
||
84 | |||
85 | $this->stopwatch->start(); |
||
86 | |||
87 | $this->logger->info("Starting new task $task ($name)"); |
||
88 | |||
89 | $thetask = $this->table->get($task)->getInstance($name, $parameters); |
||
90 | |||
91 | $this->events->emit( new TaskEvent('start', $thetask) ); |
||
92 | |||
93 | $pid = $thetask->getPid(); |
||
94 | |||
95 | $this->openWorklog( |
||
96 | $pid, |
||
97 | $name, |
||
98 | $jid, |
||
99 | $task, |
||
100 | $parameters, |
||
101 | $this->stopwatch->getStartTime() |
||
102 | ); |
||
103 | |||
104 | $this->events->emit( new TaskStatusEvent('start', $thetask) ); |
||
105 | |||
106 | try { |
||
107 | |||
108 | $result = $thetask->run(); |
||
109 | |||
110 | $status = Worklog::STATUS_FINISHED; |
||
111 | |||
112 | } catch (TaskException $te) { |
||
113 | |||
114 | $status = Worklog::STATUS_ABORTED; |
||
115 | |||
116 | $result = $te->getMessage(); |
||
117 | |||
118 | } catch (Exception $e) { |
||
119 | |||
120 | $status = Worklog::STATUS_ERROR; |
||
121 | |||
122 | $result = $e->getMessage(); |
||
123 | |||
124 | } |
||
125 | |||
126 | $this->events->emit( new TaskStatusEvent($status ? 'success' : 'error', $thetask) ); |
||
127 | |||
128 | $this->events->emit( new TaskStatusEvent('stop', $thetask) ); |
||
129 | |||
130 | $this->events->emit( new TaskEvent('stop', $thetask) ); |
||
131 | |||
132 | $this->stopwatch->stop(); |
||
133 | |||
134 | $this->closeWorklog($status, $result, $this->stopwatch->getStopTime()); |
||
135 | |||
136 | $drift = $this->stopwatch->getDrift()->format('%s'); |
||
137 | |||
138 | $this->logger->notice("Task $name ($task) with pid $pid ends in ".($status ? 'success' : 'error')." in $drift secs"); |
||
139 | |||
140 | } catch (Exception $e) { |
||
141 | |||
142 | throw $e; |
||
143 | |||
144 | } |
||
145 | |||
146 | return new Result( |
||
147 | array ( |
||
148 | $pid, |
||
149 | $name, |
||
150 | $status, |
||
151 | $this->stopwatch->getStartTime(), |
||
152 | $this->stopwatch->getStopTime(), |
||
153 | $result, |
||
154 | $this->worklog->getId() |
||
155 | ) |
||
156 | ); |
||
157 | |||
158 | } |
||
159 | |||
200 |