Conditions | 12 |
Paths | 96 |
Total Lines | 76 |
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 |
||
37 | public function run(Host $host, string $command, RunParams $params): string |
||
38 | { |
||
39 | $shellId = 'id$' . bin2hex(random_bytes(10)); |
||
40 | $shellCommand = $host->getShell(); |
||
41 | if ($host->has('become') && !empty($host->get('become'))) { |
||
42 | $shellCommand = "sudo -H -u {$host->get('become')} " . $shellCommand; |
||
43 | } |
||
44 | |||
45 | $ssh = array_merge(['ssh'], $host->connectionOptionsArray(), [$host->connectionString(), ": $shellId; $shellCommand"]); |
||
46 | |||
47 | // -vvv for ssh command |
||
48 | if ($this->output->isDebug()) { |
||
49 | $sshString = $ssh[0]; |
||
50 | for ($i = 1; $i < count($ssh); $i++) { |
||
|
|||
51 | $sshString .= ' ' . escapeshellarg((string) $ssh[$i]); |
||
52 | } |
||
53 | $this->output->writeln("[$host] $sshString"); |
||
54 | } |
||
55 | |||
56 | if (!empty($params->cwd)) { |
||
57 | $command = "cd $params->cwd && ($command)"; |
||
58 | } |
||
59 | |||
60 | if (!empty($params->env)) { |
||
61 | $env = env_stringify($params->env); |
||
62 | $command = "export $env; $command"; |
||
63 | } |
||
64 | |||
65 | if (!empty($params->secrets)) { |
||
66 | foreach ($params->secrets as $key => $value) { |
||
67 | $command = str_replace('%' . $key . '%', strval($value), $command); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | $this->pop->command($host, 'run', $command); |
||
72 | $this->logger->log("[{$host->getAlias()}] run $command"); |
||
73 | |||
74 | |||
75 | $process = new Process($ssh); |
||
76 | $process |
||
77 | ->setInput($command) |
||
78 | ->setTimeout($params->timeout) |
||
79 | ->setIdleTimeout($params->idleTimeout); |
||
80 | |||
81 | $callback = function ($type, $buffer) use ($params, $host) { |
||
82 | $this->logger->printBuffer($host, $type, $buffer); |
||
83 | $this->pop->callback($host, $params->forceOutput)($type, $buffer); |
||
84 | }; |
||
85 | |||
86 | try { |
||
87 | $process->run($callback); |
||
88 | } catch (ProcessTimedOutException $exception) { |
||
89 | // Let's try to kill all processes started by this command. |
||
90 | $pid = $this->run($host, "ps x | grep $shellId | grep -v grep | awk '{print \$1}'", $params->with(timeout: 10)); |
||
91 | // Minus before pid means all processes in this group. |
||
92 | $this->run($host, "kill -9 -$pid", $params->with(timeout: 20)); |
||
93 | throw new TimeoutException( |
||
94 | $command, |
||
95 | $exception->getExceededTimeout(), |
||
96 | ); |
||
97 | } |
||
98 | |||
99 | $output = $process->getOutput(); |
||
100 | $exitCode = $process->getExitCode(); |
||
101 | |||
102 | if ($exitCode !== 0 && !$params->nothrow) { |
||
103 | throw new RunException( |
||
104 | $host, |
||
105 | $command, |
||
106 | $exitCode, |
||
107 | $output, |
||
108 | $process->getErrorOutput(), |
||
109 | ); |
||
110 | } |
||
111 | |||
112 | return $output; |
||
113 | } |
||
115 |
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: