These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /* (c) Anton Medvedev <[email protected]> |
||
3 | * |
||
4 | * For the full copyright and license information, please view the LICENSE |
||
5 | * file that was distributed with this source code. |
||
6 | */ |
||
7 | |||
8 | namespace Deployer\Host; |
||
9 | |||
10 | use Deployer\Configuration\Configuration; |
||
11 | use Deployer\Component\Ssh\Arguments; |
||
12 | use Deployer\Deployer; |
||
13 | |||
14 | class Host |
||
15 | { |
||
16 | private $config; |
||
17 | private $sshArguments; |
||
18 | |||
19 | 24 | public function __construct(string $hostname) |
|
20 | { |
||
21 | 24 | $parent = null; |
|
22 | 24 | if (Deployer::get()) { |
|
23 | 22 | $parent = Deployer::get()->config; |
|
24 | } |
||
25 | 24 | $this->config = new Configuration($parent); |
|
26 | 24 | $this->set('alias', $hostname); |
|
27 | 24 | $this->set('hostname', preg_replace('/\/.+$/', '', $hostname)); |
|
28 | 24 | $this->sshArguments = new Arguments(); |
|
29 | 24 | } |
|
30 | |||
31 | 10 | public function getConfig() |
|
32 | { |
||
33 | 10 | return $this->config; |
|
34 | } |
||
35 | |||
36 | 24 | public function set(string $name, $value) |
|
37 | { |
||
38 | 24 | $this->config->set($name, $value); |
|
39 | 24 | return $this; |
|
40 | } |
||
41 | |||
42 | 1 | public function add(string $name, array $value) |
|
43 | { |
||
44 | 1 | $this->config->add($name, $value); |
|
45 | 1 | return $this; |
|
46 | } |
||
47 | |||
48 | public function has(string $name): bool |
||
49 | { |
||
50 | return $this->config->has($name); |
||
51 | } |
||
52 | |||
53 | 17 | public function get(string $name, $default = null) |
|
54 | { |
||
55 | 17 | return $this->config->get($name, $default); |
|
56 | } |
||
57 | |||
58 | 18 | public function getAlias() |
|
59 | { |
||
60 | 18 | return $this->config->get('alias'); |
|
61 | } |
||
62 | |||
63 | public function setTag(string $tag) |
||
64 | { |
||
65 | $this->config->set('tag', $tag); |
||
66 | return $this; |
||
67 | } |
||
68 | |||
69 | 10 | public function getTag(): string |
|
70 | { |
||
71 | 10 | return $this->config->get('tag', $this->generateTag()); |
|
72 | } |
||
73 | |||
74 | 1 | public function setHostname(string $hostname) |
|
75 | { |
||
76 | 1 | $this->config->set('hostname', $hostname); |
|
77 | 1 | return $this; |
|
78 | } |
||
79 | |||
80 | 3 | public function getHostname() |
|
81 | { |
||
82 | 3 | return $this->config->get('hostname'); |
|
83 | } |
||
84 | |||
85 | 1 | public function setRemoteUser($user) |
|
86 | { |
||
87 | 1 | $this->config->set('remote_user', $user); |
|
88 | 1 | return $this; |
|
89 | } |
||
90 | |||
91 | 2 | public function getRemoteUser() |
|
92 | { |
||
93 | 2 | return $this->config->get('remote_user'); |
|
94 | } |
||
95 | |||
96 | 1 | public function setPort(int $port) |
|
97 | { |
||
98 | 1 | $this->config->set('port', $port); |
|
99 | 1 | return $this; |
|
100 | } |
||
101 | |||
102 | 3 | public function getPort() |
|
103 | { |
||
104 | 3 | return $this->config->get('port'); |
|
105 | } |
||
106 | |||
107 | 1 | public function setConfigFile(string $file) |
|
108 | { |
||
109 | 1 | $this->config->set('config_file', $file); |
|
110 | 1 | return $this; |
|
111 | } |
||
112 | |||
113 | 3 | public function getConfigFile() |
|
114 | { |
||
115 | 3 | return $this->config->get('config_file'); |
|
116 | } |
||
117 | |||
118 | 1 | public function setIdentityFile($file) |
|
119 | { |
||
120 | 1 | $this->config->set('identity_file', $file); |
|
121 | 1 | return $this; |
|
122 | } |
||
123 | |||
124 | 4 | public function getIdentityFile() |
|
125 | { |
||
126 | 4 | return $this->config->get('identity_file'); |
|
127 | } |
||
128 | |||
129 | 1 | public function setForwardAgent(bool $on) |
|
130 | { |
||
131 | 1 | $this->config->set('forward_agent', $on); |
|
132 | 1 | return $this; |
|
133 | } |
||
134 | |||
135 | 3 | public function getForwardAgent() |
|
136 | { |
||
137 | 3 | return $this->config->get('forward_agent'); |
|
138 | } |
||
139 | |||
140 | 1 | public function setSshMultiplexing(bool $on) |
|
141 | { |
||
142 | 1 | $this->config->set('ssh_multiplexing', $on); |
|
143 | 1 | return $this; |
|
144 | } |
||
145 | |||
146 | 2 | public function getSshMultiplexing() |
|
147 | { |
||
148 | 2 | return $this->config->get('ssh_multiplexing'); |
|
149 | } |
||
150 | |||
151 | public function setShell(string $command) |
||
152 | { |
||
153 | $this->config->set('shell', $command); |
||
154 | return $this; |
||
155 | } |
||
156 | |||
157 | public function getShell(): string |
||
158 | { |
||
159 | return $this->config->get('shell'); |
||
160 | } |
||
161 | |||
162 | public function setDeployPath(string $path) |
||
163 | { |
||
164 | $this->config->set('deploy_path', $path); |
||
165 | return $this; |
||
166 | } |
||
167 | |||
168 | public function getDeployPath() |
||
169 | { |
||
170 | return $this->config->get('deploy_path'); |
||
171 | } |
||
172 | |||
173 | 2 | public function getConnectionString(): string |
|
174 | { |
||
175 | 2 | if ($this->get('remote_user', '') !== '') { |
|
176 | return $this->get('remote_user') . '@' . $this->get('hostname'); |
||
177 | } |
||
178 | 2 | return $this->get('hostname'); |
|
179 | } |
||
180 | |||
181 | 3 | public function getSshArguments() |
|
182 | { |
||
183 | 3 | $this->initOptions(); |
|
184 | 3 | return $this->sshArguments; |
|
185 | } |
||
186 | |||
187 | // TODO: Migrate to configuration. |
||
188 | |||
189 | 2 | public function setSshOptions(array $options) |
|
190 | { |
||
191 | 2 | $this->sshArguments = $this->sshArguments->withOptions($options); |
|
192 | 2 | return $this; |
|
193 | } |
||
194 | |||
195 | // TODO: Migrate to configuration. |
||
196 | |||
197 | 2 | public function setSshFlags(array $flags) |
|
198 | { |
||
199 | 2 | $this->sshArguments = $this->sshArguments->withFlags($flags); |
|
200 | 2 | return $this; |
|
201 | } |
||
202 | |||
203 | 3 | private function initOptions() |
|
204 | { |
||
205 | 3 | if ($this->getPort()) { |
|
206 | 3 | $this->sshArguments = $this->sshArguments->withFlag('-p', $this->getPort()); |
|
207 | } |
||
208 | |||
209 | 3 | if ($this->getConfigFile()) { |
|
210 | 2 | $this->sshArguments = $this->sshArguments->withFlag('-F', $this->getConfigFile()); |
|
211 | } |
||
212 | |||
213 | 3 | if ($this->getIdentityFile()) { |
|
214 | 2 | $this->sshArguments = $this->sshArguments->withFlag('-i', $this->getIdentityFile()); |
|
215 | } |
||
216 | |||
217 | 3 | if ($this->getForwardAgent()) { |
|
218 | 3 | $this->sshArguments = $this->sshArguments->withFlag('-A'); |
|
219 | } |
||
220 | 3 | } |
|
221 | |||
222 | 10 | private function generateTag() |
|
223 | { |
||
224 | 10 | if (defined('NO_ANSI')) { |
|
225 | 9 | return $this->getAlias(); |
|
226 | } |
||
227 | |||
228 | 1 | if ($this->getAlias() === 'localhost') { |
|
229 | return $this->getAlias(); |
||
230 | } |
||
231 | |||
232 | 1 | if (getenv('COLORTERM') === 'truecolor') { |
|
233 | $hsv = function ($h, $s, $v) { |
||
234 | $r = $g = $b = $i = $f = $p = $q = $t = 0; |
||
0 ignored issues
–
show
$q is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
$p is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
$f is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
$i is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
|
|||
235 | $i = floor($h * 6); |
||
236 | $f = $h * 6 - $i; |
||
237 | $p = $v * (1 - $s); |
||
238 | $q = $v * (1 - $f * $s); |
||
239 | $t = $v * (1 - (1 - $f) * $s); |
||
240 | switch ($i % 6) { |
||
241 | case 0: |
||
242 | $r = $v; |
||
243 | $g = $t; |
||
244 | $b = $p; |
||
245 | break; |
||
246 | case 1: |
||
247 | $r = $q; |
||
248 | $g = $v; |
||
249 | $b = $p; |
||
250 | break; |
||
251 | case 2: |
||
252 | $r = $p; |
||
253 | $g = $v; |
||
254 | $b = $t; |
||
255 | break; |
||
256 | case 3: |
||
257 | $r = $p; |
||
258 | $g = $q; |
||
259 | $b = $v; |
||
260 | break; |
||
261 | case 4: |
||
262 | $r = $t; |
||
263 | $g = $p; |
||
264 | $b = $v; |
||
265 | break; |
||
266 | case 5: |
||
267 | $r = $v; |
||
268 | $g = $p; |
||
269 | $b = $q; |
||
270 | break; |
||
271 | } |
||
272 | $r = round($r * 255); |
||
273 | $g = round($g * 255); |
||
274 | $b = round($b * 255); |
||
275 | return "\x1b[38;2;{$r};{$g};{$b}m"; |
||
276 | }; |
||
277 | |||
278 | $total = 100; |
||
279 | $colors = []; |
||
280 | for ($i = 0; $i < $total; $i++) { |
||
281 | $colors[] = $hsv($i / $total, 1, .9); |
||
282 | } |
||
283 | |||
284 | $alias = $this->getAlias(); |
||
285 | $tag = $colors[abs(crc32($alias)) % count($colors)]; |
||
286 | |||
287 | return "{$tag}{$alias}\x1b[0m"; |
||
288 | } |
||
289 | |||
290 | |||
291 | $colors = [ |
||
292 | 1 | 'fg=cyan;options=bold', |
|
293 | 'fg=green;options=bold', |
||
294 | 'fg=yellow;options=bold', |
||
295 | 'fg=cyan', |
||
296 | 'fg=blue', |
||
297 | 'fg=yellow', |
||
298 | 'fg=magenta', |
||
299 | 'fg=blue;options=bold', |
||
300 | 'fg=green', |
||
301 | 'fg=magenta;options=bold', |
||
302 | 'fg=red;options=bold', |
||
303 | ]; |
||
304 | 1 | $alias = $this->getAlias(); |
|
305 | 1 | $tag = $colors[abs(crc32($alias)) % count($colors)]; |
|
306 | |||
307 | 1 | return "<{$tag}>{$alias}</>"; |
|
308 | } |
||
309 | } |
||
310 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.