Passed
Push — master ( 9ce3f7...9fd858 )
by Anton
03:51
created

src/Console/SshCommand.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

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\Console;
9
10
use Deployer\Deployer;
11
use Deployer\Host\Localhost;
12
use Deployer\Task\Context;
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Question\ChoiceQuestion;
18
19
/**
20
 * @codeCoverageIgnore
21
 */
22
class SshCommand extends Command
23
{
24
    /**
25
     * @var Deployer
26
     */
27
    private $deployer;
28
29
    /**
30
     * SshCommand constructor.
31
     * @param Deployer $deployer
32
     */
33
    public function __construct(Deployer $deployer)
34
    {
35
        parent::__construct('ssh');
36
        $this->setDescription('Connect to host through ssh');
37
        $this->deployer = $deployer;
38
    }
39
40
    /**
41
     * Configures the command
42
     */
43
    protected function configure()
44
    {
45
        $this->addArgument(
46
            'hostname',
47
            InputArgument::OPTIONAL,
48
            'Hostname'
49
        );
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    protected function execute(InputInterface $input, OutputInterface $output)
56
    {
57
        $hostname = $input->getArgument('hostname');
58
        if (!empty($hostname)) {
59
            $host = $this->deployer->hosts->get($hostname);
0 ignored issues
show
It seems like $hostname defined by $input->getArgument('hostname') on line 57 can also be of type array<integer,string>; however, Deployer\Collection\Collection::get() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
60
        } else {
61
            $hosts = $this->deployer->hosts->select(function ($host) {
62
                return !($host instanceof Localhost);
63
            });
64
65
            if (count($hosts) === 0) {
66
                $output->writeln('No remote hosts.');
67
                return; // Because there are no hosts.
68
            } elseif (count($hosts) === 1) {
69
                $host = array_shift($hosts);
70
            } else {
71
                $helper = $this->getHelper('question');
72
                $question = new ChoiceQuestion(
73
                    'Select host:',
74
                    $hosts
75
                );
76
                $question->setErrorMessage('There is no "%s" host.');
77
78
                $hostname = $helper->ask($input, $output, $question);
79
                $host = $this->deployer->hosts->get($hostname);
80
            }
81
        }
82
83
        $shell_path = 'exec $SHELL -l';
84
        if ($host->has('shell_path')) {
85
            $shell_path = 'exec '.$host->get('shell_path').' -l';
86
        }
87
88
        Context::push(new Context($host, $input, $output));
89
        $options = $host->getSshArguments();
90
        $deployPath = $host->get('deploy_path', '~');
91
92
        passthru("ssh -t $options $host 'cd '''$deployPath/current'''; $shell_path'");
93
    }
94
}
95