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 (#639)
by
unknown
03:21
created

PhpSecLib   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 153
Duplicated Lines 26.8 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 4.41%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 19
c 3
b 2
f 0
lcom 1
cbo 1
dl 41
loc 153
ccs 3
cts 68
cp 0.0441
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B connect() 25 56 7
A checkConnection() 0 6 2
A run() 0 13 3
A upload() 16 16 3
A download() 0 8 2
A getConfiguration() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Server\Remote;
9
10
use Deployer\Server\Configuration;
11
use Deployer\Server\ServerInterface;
12
use phpseclib\Crypt\RSA;
13
use phpseclib\Net\SFTP;
14
use phpseclib\System\SSH\Agent;
15
use RuntimeException;
16
17
class PhpSecLib implements ServerInterface
18
{
19
    /**
20
     * @var Configuration
21
     */
22
    private $configuration;
23
24
    /**
25
     * @var SFTP
26
     */
27
    private $sftp;
28
29
    /**
30
     * Array of created directories during upload.
31
     * @var array
32
     */
33
    private $directories = [];
34
35
    /**
36
     * @param Configuration $configuration
37
     */
38 2
    public function __construct(Configuration $configuration)
39
    {
40 2
        $this->configuration = $configuration;
41 2
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function connect()
47
    {
48
        $serverConfig = $this->getConfiguration();
49
        $this->sftp = new SFTP($serverConfig->getHost(), $serverConfig->getPort(), 3600);
50
51
        switch ($serverConfig->getAuthenticationMethod()) {
52
            case Configuration::AUTH_BY_PASSWORD:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
53
54
                $result = $this->sftp->login($serverConfig->getUser(), $serverConfig->getPassword());
55
56
                break;
57
58 View Code Duplication
            case Configuration::AUTH_BY_IDENTITY_FILE:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
59
60
                $key = new RSA();
61
                $key->setPassword($serverConfig->getPassPhrase());
62
                $key->loadKey(file_get_contents($serverConfig->getPrivateKey()));
63
64
                $result = $this->sftp->login($serverConfig->getUser(), $key);
65
66
                break;
67
68 View Code Duplication
            case Configuration::AUTH_BY_PEM_FILE:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
69
70
                $key = new RSA();
71
                $key->loadKey(file_get_contents($serverConfig->getPemFile()));
72
                $result = $this->sftp->login($serverConfig->getUser(), $key);
73
74
                break;
75
76
            case Configuration::AUTH_BY_AGENT:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
77
78
                $key = new Agent();
79
                $key->startSSHForwarding(null);
80
                $result = $this->sftp->login($serverConfig->getUser(), $key);
81
82
                break;
83
84 View Code Duplication
            case Configuration::AUTH_BY_IDENTITY_FILE_AND_PASSWORD:
2 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
85
86
                $key = new RSA();
87
                $key->setPassword($serverConfig->getPassPhrase());
88
                $key->loadKey(file_get_contents($serverConfig->getPrivateKey()));
89
90
                $result = $this->sftp->login($serverConfig->getUser(), $key, $serverConfig->getPassword());
91
92
                break;
93
94
            default:
95
                throw new RuntimeException('You need to specify authentication method.');
96
        }
97
98
        if (!$result) {
99
            throw new RuntimeException('Unable to login with the provided credentials.');
100
        }
101
    }
102
103
    /**
104
     * Check if not connected and connect.
105
     */
106
    public function checkConnection()
107
    {
108
        if (null === $this->sftp) {
109
            $this->connect();
110
        }
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function run($command)
117
    {
118
        $this->checkConnection();
119
120
        $result = $this->sftp->exec($command);
121
122
        if ($this->sftp->getExitStatus() !== 0) {
123
            $output = $this->sftp->getStdError() ?: $result;
124
            throw new \RuntimeException($output);
125
        }
126
127
        return $result;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 View Code Duplication
    public function upload($local, $remote)
134
    {
135
        $this->checkConnection();
136
137
        $remote = str_replace(DIRECTORY_SEPARATOR, '/', $remote);
138
        $dir = str_replace(DIRECTORY_SEPARATOR, '/', dirname($remote));
139
140
        if (!isset($this->directories[$dir])) {
141
            $this->sftp->mkdir($dir, -1, true);
142
            $this->directories[$dir] = true;
143
        }
144
145
        if (!$this->sftp->put($remote, $local, SFTP::SOURCE_LOCAL_FILE)) {
146
            throw new \RuntimeException(implode($this->sftp->getSFTPErrors(), "\n"));
147
        }
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function download($local, $remote)
154
    {
155
        $this->checkConnection();
156
157
        if (!$this->sftp->get($remote, $local)) {
158
            throw new \RuntimeException(implode($this->sftp->getSFTPErrors(), "\n"));
159
        }
160
    }
161
162
    /**
163
     * @return Configuration
164
     */
165
    public function getConfiguration()
166
    {
167
        return $this->configuration;
168
    }
169
}
170