| Conditions | 10 |
| Paths | 14 |
| Total Lines | 43 |
| Code Lines | 26 |
| 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 |
||
| 51 | public function wait(ProcessCollection $processCollection) { |
||
| 52 | $output = []; |
||
| 53 | $passes = 1; |
||
| 54 | $processes = $processCollection->toArray(); |
||
| 55 | |||
| 56 | while (count($processes)) { |
||
| 57 | /** @var Process $process */ |
||
| 58 | foreach ($processes as $key => $process) { |
||
| 59 | $processStatus = pcntl_waitpid($process->getPid(), $status, WNOHANG | WUNTRACED); |
||
| 60 | |||
| 61 | if ($processStatus == $process->getPid()) { |
||
| 62 | $output[] = unserialize(socket_read($process->getSocket(), 4096)); |
||
| 63 | socket_close($process->getSocket()); |
||
| 64 | |||
| 65 | $success = $process->getSuccess(); |
||
| 66 | if ($success) { |
||
| 67 | call_user_func_array($success, [$process]); |
||
| 68 | } |
||
| 69 | |||
| 70 | unset($processes[$key]); |
||
| 71 | } else if ($processStatus == 0) { |
||
| 72 | if ($process->getStartTime() + $process->getMaxRunTime() < time() || pcntl_wifstopped($status)) { |
||
| 73 | if (!posix_kill($process->getPid(), SIGKILL)) { |
||
| 74 | throw new \Exception('Failed to kill ' . $process->getPid() . ': ' . posix_strerror(posix_get_last_error()), E_USER_WARNING); |
||
| 75 | } |
||
| 76 | |||
| 77 | unset($processes[$key]); |
||
| 78 | } |
||
| 79 | } else { |
||
| 80 | trigger_error('Something went terribly wrong with process ' . $process->getPid(), E_USER_WARNING); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | if (!count($processes)) { |
||
| 85 | break; |
||
| 86 | } |
||
| 87 | |||
| 88 | ++$passes; |
||
| 89 | usleep(100000); |
||
| 90 | } |
||
| 91 | |||
| 92 | return $output; |
||
| 93 | } |
||
| 94 | } |
||
| 95 |