GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#21)
by
unknown
01:30
created

SSH::getCommand()   C

Complexity

Conditions 8
Paths 34

Size

Total Lines 34
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 17
nc 34
nop 1
1
<?php
2
3
/*
4
 * This file is part of rsync-lib
5
 *
6
 * (c) Alberto Fernández <[email protected]>
7
 *
8
 * For the full copyright and license information, please read
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace AFM\Rsync;
13
14
/**
15
 * Abstract SSH connection command. Note that if you
16
 * don't specify a public key, you will be prompted for
17
 * the remote server password
18
 *
19
 * @author Alberto <[email protected]>
20
 */
21
class SSH extends AbstractProtocol
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $executable = "ssh";
27
28
    /**
29
     * @var string
30
     */
31
    protected $host;
32
33
    /**
34
     * @var int
35
     */
36
    protected $port = 22;
37
38
    /**
39
     * @var string
40
     */
41
    protected $username;
42
43
    /**
44
     * @var null
45
     */
46
    protected $publicKey = null;
47
48
    /**
49
     * @var string
50
     */
51
    protected $strictHostKeyCheking = null;
52
53
    /**
54
     * @var string
55
     */
56
    protected $userKnownHostsFile = null;
57
58
    /**
59
     * Injects and validates config
60
     *
61
     * @param array $options
62
     */
63
    public function __construct(array $options = array())
64
    {
65
        $this->setOption($options, 'executable', 'setExecutable');
66
        $this->setOption($options, 'host', 'setHost');
67
        $this->setOption($options, 'port', 'setPort');
68
        $this->setOption($options, 'username', 'setUsername');
69
        $this->setOption($options, 'public_key', 'setPublicKey');
70
        $this->setOption($options, 'strict_host_key_checking', 'setStrictHostKeyChecking');
71
        $this->setOption($options, 'user_known_hosts_file', 'setUserKnownHostFile');
72
    }
73
74
    /**
75
     * @param $host
76
     */
77
    public function setHost($host)
78
    {
79
        $this->host = $host;
80
    }
81
82
    /**
83
     * @return mixed
84
     */
85
    public function getHost()
86
    {
87
        return $this->host;
88
    }
89
90
    /**
91
     * @param $port
92
     *
93
     * @throws \InvalidArgumentException If the port is not numeric
94
     */
95
    public function setPort($port)
96
    {
97
        if (!is_int($port)) {
98
            throw new \InvalidArgumentException("SSH port must be an integer");
99
        }
100
101
        $this->port = $port;
102
    }
103
104
    /**
105
     * @return int
106
     */
107
    public function getPort()
108
    {
109
        return $this->port;
110
    }
111
112
    /**
113
     * @param $publicKey
114
     * @throws \InvalidArgumentException
115
     */
116
    public function setPublicKey($publicKey)
117
    {
118
        if (!is_readable($publicKey)) {
119
            throw new \InvalidArgumentException("SSH public key '" . $publicKey . "' is not readable");
120
        }
121
122
        $this->publicKey = $publicKey;
123
    }
124
125
    /**
126
     * @return null
127
     */
128
    public function getPublicKey()
129
    {
130
        return $this->publicKey;
131
    }
132
133
    /**
134
     * @param $username
135
     */
136
    public function setUsername($username)
137
    {
138
        $this->username = $username;
139
    }
140
141
    /**
142
     * @return mixed
143
     */
144
    public function getUsername()
145
    {
146
        return $this->username;
147
    }
148
149
    /**
150
     * @param $string
151
     * @throws \InvalidArgumentException
152
     */
153
    public function setStrictHostKeyChecking($strictHostKeyCheking)
154
    {
155
        //dd($strictHostKeyCheking);
156
        if ($strictHostKeyCheking !== 'yes' && $strictHostKeyCheking !== 'no') {
157
            throw new \InvalidArgumentException("StrictHostKeyCheking must be set to 'yes' or 'no'");
158
        }
159
        $this->strictHostKeyCheking = "StrictHostKeyChecking={$strictHostKeyCheking}";
160
    }
161
162
    /**
163
     * @return $string
0 ignored issues
show
Documentation introduced by
The doc-type $string could not be parsed: Unknown type name "$string" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
164
     */
165
    public function getStrictHostKeyChecking($strictHostKeyCheking)
0 ignored issues
show
Unused Code introduced by
The parameter $strictHostKeyCheking is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
166
    {
167
        return $this->strictHostKeyCheking;
168
    }
169
170
    /**
171
     * @param $string
172
     */
173
    public function setUserKnownHostFile($userKnownHostsFile)
174
    {
175
        $this->userKnownHostsFile = "UserKnownHostsFile={$userKnownHostsFile}";
176
    }
177
178
    /**
179
     * @return $string
0 ignored issues
show
Documentation introduced by
The doc-type $string could not be parsed: Unknown type name "$string" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
180
     */
181
    public function getUserKnownHostFile($userKnownHostFile)
0 ignored issues
show
Unused Code introduced by
The parameter $userKnownHostFile is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
182
    {
183
        return $this->userKnownHostFile;
0 ignored issues
show
Bug introduced by
The property userKnownHostFile does not seem to exist. Did you mean userKnownHostsFile?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
184
    }
185
186
    /**
187
     * Gets commands for this SSH connection
188
     *
189
     * @param bool $hostConnection
190
     *
191
     * @return string
192
     *
193
     * @throws \InvalidArgumentException If you don't specify a SSH username or host
194
     */
195
    public function getCommand($hostConnection = true)
196
    {
197
        if (is_null($this->username)) {
198
            throw new \InvalidArgumentException("You must specify a SSH username");
199
        }
200
201
        if (is_null($this->host)) {
202
            throw new \InvalidArgumentException("You must specify a SSH host to connect");
203
        }
204
205
        $command = new Command($this->executable);
206
207
        if ($this->port != 22) {
208
            $command->addArgument("p", $this->port);
209
        }
210
211
        if (!is_null($this->publicKey)) {
212
            $command->addArgument("i", $this->publicKey);
213
        }
214
215
        if (!is_null($this->strictHostKeyCheking)) {
216
            $command->addArgument("o", $this->strictHostKeyCheking);
217
        }
218
219
        if (!is_null($this->userKnownHostsFile)) {
220
            $command->addArgument("o", $this->userKnownHostsFile);
221
        }
222
223
        if ($hostConnection) {
224
            $command->addParameter($this->getHostConnection());
225
        }
226
227
        return $command;
228
    }
229
230
    /**
231
     * Gets only connection options, without user@host string
232
     *
233
     * @return string
234
     */
235
    public function getConnectionOptions()
236
    {
237
        return (string) $this->getCommand(false);
238
    }
239
240
    /**
241
     * Gets only host connection, without the rest
242
     * of options
243
     *
244
     * @return string
245
     */
246
    public function getHostConnection()
247
    {
248
        return $this->username . "@" . $this->host;
249
    }
250
251
    /**
252
     * @param $executable
253
     */
254
    public function setExecutable($executable)
255
    {
256
        $this->executable = $executable;
257
    }
258
259
    /**
260
     * @return string
261
     */
262
    public function getExecutable()
263
    {
264
        return $this->executable;
265
    }
266
}
267