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 ( 31d2dd...b0fd65 )
by Anton
02:15
created

NativeSsh::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 Symfony\Component\Process\Process;
13
14
class NativeSsh implements ServerInterface
15
{
16
    /**
17
     * @var Configuration
18
     */
19
    private $configuration;
20
21
    /**
22
     * @param Configuration $configuration
23
     */
24
    public function __construct(Configuration $configuration)
25
    {
26
        $this->configuration = $configuration;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function connect()
33
    {
34
        /* no persistent connection is used */
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function run($command)
41
    {
42
        $serverConfig = $this->getConfiguration();
43
        $sshOptions = ['-A'];
44
45
        $username = $serverConfig->getUser() ? $serverConfig->getUser() : null;
46
        if (!empty($username)) {
47
            $username = $username . '@';
48
        }
49
        $hostname = $serverConfig->getHost();
50
51
        if ($serverConfig->getPort()) {
52
            $sshOptions[] = '-p ' . escapeshellarg($serverConfig->getPort());
53
        }
54
55
        if ($serverConfig->getPrivateKey()) {
56
            $sshOptions[] = '-i ' . escapeshellarg($serverConfig->getPrivateKey());
57
        }
58
59
        $sshCommand = 'ssh ' . implode(' ', $sshOptions) . ' ' . escapeshellarg($username . $hostname) . ' ' . escapeshellarg($command);
60
61
        $process = new Process($sshCommand);
62
        $process
63
            ->setTimeout(null)
64
            ->setIdleTimeout(null)
65
            ->mustRun();
66
67
        return $process->getOutput();
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 View Code Duplication
    public function upload($local, $remote)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
74
    {
75
        $serverConfig = $this->getConfiguration();
76
77
        $username = $serverConfig->getUser() ? $serverConfig->getUser() : null;
78
        $hostname = $serverConfig->getHost();
79
80
        return $this->scpCopy($local, (!empty($username) ? $username . '@' : '') . $hostname . ':' . $remote);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 View Code Duplication
    public function download($local, $remote)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
87
    {
88
        $serverConfig = $this->getConfiguration();
89
90
        $username = $serverConfig->getUser() ? $serverConfig->getUser() : null;
91
        $hostname = $serverConfig->getHost();
92
93
        return $this->scpCopy((!empty($username) ? $username . '@' : '') . $hostname . ':' . $remote, $local);
94
    }
95
96
    /**
97
     * Copy file from target1 to target 2 via scp
98
     * @param string $target
99
     * @param string $target2
100
     */
101
    public function scpCopy($target, $target2)
102
    {
103
        $serverConfig = $this->getConfiguration();
104
105
        $scpOptions = [];
106
107
        if ($serverConfig->getPort()) {
108
            $scpOptions[] = '-P ' . escapeshellarg($serverConfig->getPort());
109
        }
110
111
        if ($serverConfig->getPrivateKey()) {
112
            $sshOptions[] = '-i ' . escapeshellarg($serverConfig->getPrivateKey());
0 ignored issues
show
Coding Style Comprehensibility introduced by
$sshOptions was never initialized. Although not strictly required by PHP, it is generally a good practice to add $sshOptions = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
113
        }
114
115
        $scpCommand = 'scp ' . implode(' ', $scpOptions) . ' ' . escapeshellarg($target) . ' ' . escapeshellarg($target2);
116
117
        $process = new Process($scpCommand);
118
        $process
119
            ->setTimeout(null)
120
            ->setIdleTimeout(null)
121
            ->mustRun();
122
123
        return $process->getOutput();
124
    }
125
126
    /**
127
     * @return Configuration
128
     */
129
    public function getConfiguration()
130
    {
131
        return $this->configuration;
132
    }
133
}
134