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
Push — master ( 99078a...9605c1 )
by Oanh
02:49
created

PhpSecLib   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 143
Duplicated Lines 22.38 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 4.84%

Importance

Changes 4
Bugs 3 Features 0
Metric Value
wmc 18
c 4
b 3
f 0
lcom 1
cbo 1
dl 32
loc 143
ccs 3
cts 62
cp 0.0484
rs 10

7 Methods

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