| Conditions | 6 |
| Paths | 32 |
| Total Lines | 64 |
| 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 |
||
| 31 | public function run(Host $host, string $command, array $config = []) |
||
| 32 | { |
||
| 33 | $hostname = $host->hostname(); |
||
| 34 | $defaults = [ |
||
| 35 | 'timeout' => $host->get('default_timeout', 300), |
||
| 36 | ]; |
||
| 37 | |||
| 38 | $config = array_merge($defaults, $config); |
||
| 39 | $sshArguments = $host->getSshArguments(); |
||
| 40 | $become = $host->has('become') ? 'sudo -H -u ' . $host->get('become') : ''; |
||
| 41 | |||
| 42 | if ($host->sshMultiplexing()) { |
||
| 43 | $sshArguments = $this->initMultiplexing($host); |
||
| 44 | } |
||
| 45 | |||
| 46 | $shellCommand = $host->shell(); |
||
| 47 | |||
| 48 | if (strtolower(substr(PHP_OS, 0, 3)) === 'win') { |
||
| 49 | $ssh = "ssh $sshArguments $hostname $become \"$shellCommand; printf '[exit_code:%s]' $?;\""; |
||
| 50 | } else { |
||
| 51 | $ssh = "ssh $sshArguments $hostname $become '$shellCommand; printf \"[exit_code:%s]\" $?;'"; |
||
| 52 | } |
||
| 53 | |||
| 54 | // -vvv for ssh command |
||
| 55 | if ($this->output->isDebug()) { |
||
| 56 | $this->pop->writeln(Process::OUT, $host, "$ssh"); |
||
| 57 | } |
||
| 58 | |||
| 59 | $this->pop->command($host, $command); |
||
| 60 | $this->logger->log("[{$host->alias()}] run $command"); |
||
| 61 | |||
| 62 | $terminalOutput = $this->pop->callback($host); |
||
| 63 | $callback = function ($type, $buffer) use ($host, $terminalOutput) { |
||
| 64 | $this->logger->printBuffer($host, $type, $buffer); |
||
| 65 | $terminalOutput($type, $buffer); |
||
| 66 | }; |
||
| 67 | |||
| 68 | |||
| 69 | $process = $this->createProcess($ssh); |
||
| 70 | $process |
||
| 71 | ->setInput($command) |
||
| 72 | ->setTimeout($config['timeout']); |
||
| 73 | |||
| 74 | |||
| 75 | $process->run($callback); |
||
| 76 | |||
| 77 | $output = $this->pop->filterOutput($process->getOutput()); |
||
| 78 | $exitCode = $this->parseExitStatus($process); |
||
| 79 | |||
| 80 | if ($exitCode !== 0) { |
||
| 81 | $trace = debug_backtrace(); |
||
| 82 | throw new RunException( |
||
| 83 | basename($trace[1]['file']), |
||
| 84 | $trace[1]['line'], |
||
| 85 | $hostname, |
||
| 86 | $command, |
||
| 87 | $exitCode, |
||
| 88 | $output, |
||
| 89 | $process->getErrorOutput() |
||
| 90 | ); |
||
| 91 | } |
||
| 92 | |||
| 93 | return $output; |
||
| 94 | } |
||
| 95 | |||
| 178 |