| Conditions | 10 |
| Paths | 12 |
| Total Lines | 71 |
| Code Lines | 46 |
| 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 |
||
| 35 | public function run(Host $host, string $command, array $config = []): string |
||
| 36 | { |
||
| 37 | $defaults = [ |
||
| 38 | 'timeout' => $host->get('default_timeout', 300), |
||
| 39 | 'idle_timeout' => null, |
||
| 40 | 'real_time_output' => false, |
||
| 41 | 'no_throw' => false, |
||
| 42 | ]; |
||
| 43 | $config = array_merge($defaults, $config); |
||
| 44 | |||
| 45 | $shellId = 'id$' . bin2hex(random_bytes(10)); |
||
| 46 | $shellCommand = $host->getShell(); |
||
| 47 | if ($host->has('become') && !empty($host->get('become'))) { |
||
| 48 | $shellCommand = "sudo -H -u {$host->get('become')} " . $shellCommand; |
||
| 49 | } |
||
| 50 | |||
| 51 | $ssh = array_merge(['ssh'], $host->connectionOptionsArray(), [$host->connectionString(), ": $shellId; $shellCommand"]); |
||
| 52 | |||
| 53 | // -vvv for ssh command |
||
| 54 | if ($this->output->isDebug()) { |
||
| 55 | $sshString = $ssh[0]; |
||
| 56 | for ($i = 1; $i < count($ssh); $i++) { |
||
|
|
|||
| 57 | $sshString .= ' ' . escapeshellarg((string) $ssh[$i]); |
||
| 58 | } |
||
| 59 | $this->output->writeln("[$host] $sshString"); |
||
| 60 | } |
||
| 61 | |||
| 62 | $this->pop->command($host, 'run', $command); |
||
| 63 | $this->logger->log("[{$host->getAlias()}] run $command"); |
||
| 64 | |||
| 65 | $command = str_replace('%secret%', strval($config['secret'] ?? ''), $command); |
||
| 66 | $command = str_replace('%sudo_pass%', strval($config['sudo_pass'] ?? ''), $command); |
||
| 67 | |||
| 68 | $process = new Process($ssh); |
||
| 69 | $process |
||
| 70 | ->setInput($command) |
||
| 71 | ->setTimeout((null === $config['timeout']) ? null : (float) $config['timeout']) |
||
| 72 | ->setIdleTimeout((null === $config['idle_timeout']) ? null : (float) $config['idle_timeout']); |
||
| 73 | |||
| 74 | $callback = function ($type, $buffer) use ($config, $host) { |
||
| 75 | $this->logger->printBuffer($host, $type, $buffer); |
||
| 76 | $this->pop->callback($host, boolval($config['real_time_output']))($type, $buffer); |
||
| 77 | }; |
||
| 78 | |||
| 79 | try { |
||
| 80 | $process->run($callback); |
||
| 81 | } catch (ProcessTimedOutException $exception) { |
||
| 82 | // Let's try to kill all processes started by this command. |
||
| 83 | $pid = $this->run($host, "ps x | grep $shellId | grep -v grep | awk '{print \$1}'"); |
||
| 84 | // Minus before pid means all processes in this group. |
||
| 85 | $this->run($host, "kill -9 -$pid"); |
||
| 86 | throw new TimeoutException( |
||
| 87 | $command, |
||
| 88 | $exception->getExceededTimeout(), |
||
| 89 | ); |
||
| 90 | } |
||
| 91 | |||
| 92 | $output = $process->getOutput(); |
||
| 93 | $exitCode = $process->getExitCode(); |
||
| 94 | |||
| 95 | if ($exitCode !== 0 && !$config['no_throw']) { |
||
| 96 | throw new RunException( |
||
| 97 | $host, |
||
| 98 | $command, |
||
| 99 | $exitCode, |
||
| 100 | $output, |
||
| 101 | $process->getErrorOutput(), |
||
| 102 | ); |
||
| 103 | } |
||
| 104 | |||
| 105 | return $output; |
||
| 106 | } |
||
| 114 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: