| Conditions | 8 |
| Paths | 4 |
| Total Lines | 61 |
| Code Lines | 29 |
| Lines | 12 |
| Ratio | 19.67 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 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 |
||
| 122 | public function stopProcess($pid, $grace = 5) |
||
| 123 | { |
||
| 124 | // what are we doing? |
||
| 125 | $log = usingLog()->startAction("stop process '{$pid}' on host '{$this->args[0]}'"); |
||
| 126 | |||
| 127 | // is the process running at all? |
||
| 128 | if (!fromHost($this->args[0])->getPidIsRunning($pid)) { |
||
| 129 | $log->endAction("process is not running"); |
||
| 130 | return; |
||
| 131 | } |
||
| 132 | |||
| 133 | // yes it is, so stop it |
||
| 134 | // send a TERM signal to the screen session |
||
| 135 | $log->addStep("send SIGTERM to process '{$pid}'", function() use ($pid) { |
||
| 136 | View Code Duplication | if ($this->getIsLocalhost()) { |
|
| 137 | posix_kill($pid, SIGTERM); |
||
| 138 | } |
||
| 139 | else { |
||
| 140 | usingHost($this->args[0])->runCommand("kill {$pid}"); |
||
| 141 | } |
||
| 142 | }); |
||
| 143 | |||
| 144 | // has this worked? |
||
| 145 | $isStopped = $log->addStep("wait for process to terminate", function() use($pid, $grace, $log) { |
||
| 146 | for($i = 0; $i < $grace; $i++) { |
||
| 147 | if (!fromHost($this->args[0])->getPidIsRunning($pid)) { |
||
| 148 | return true; |
||
| 149 | } |
||
| 150 | |||
| 151 | // process still exists |
||
| 152 | sleep(1); |
||
| 153 | } |
||
| 154 | |||
| 155 | return false; |
||
| 156 | }); |
||
| 157 | |||
| 158 | // did the process stop? |
||
| 159 | if ($isStopped) { |
||
| 160 | $log->endAction(); |
||
| 161 | return; |
||
| 162 | } |
||
| 163 | |||
| 164 | $log->addStep("send SIGKILL to process '{$pid}'", function() use($pid) { |
||
| 165 | View Code Duplication | if ($this->getIsLocalhost()) { |
|
| 166 | posix_kill($pid, SIGKILL); |
||
| 167 | } |
||
| 168 | else { |
||
| 169 | usingHost($this->args[0])->runCommand("kill -9 {$pid}"); |
||
| 170 | } |
||
| 171 | sleep(1); |
||
| 172 | }); |
||
| 173 | |||
| 174 | // success? |
||
| 175 | if (fromHost($this->args[0])->getProcessIsRunning($pid)) { |
||
| 176 | $log->endAction("process is still running :("); |
||
| 177 | throw Exceptions::newActionFailedException(__METHOD__); |
||
| 178 | } |
||
| 179 | |||
| 180 | // all done |
||
| 181 | $log->endAction("process has finished"); |
||
| 182 | } |
||
| 183 | |||
| 192 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.