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.
Passed
Push — master ( b3cd52...71bd8a )
by Anton
02:19
created

MainCommand::execute()   B

Complexity

Conditions 10
Paths 36

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
nc 36
nop 2
dl 0
loc 48
ccs 0
cts 39
cp 0
crap 110
rs 7.2678
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Exception\Exception;
12
use Deployer\Exception\GracefulShutdownException;
13
use Deployer\Executor\Planner;
14
use Deployer\Host\Host;
15
use Deployer\Host\Localhost;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
18
use Symfony\Component\Console\Helper\QuestionHelper;
19
use Symfony\Component\Console\Helper\Table;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface as Input;
22
use Symfony\Component\Console\Input\InputOption as Option;
23
use Symfony\Component\Console\Output\OutputInterface as Output;
24
use Symfony\Component\Console\Question\ChoiceQuestion;
25
26
class MainCommand extends SelectCommand
27
{
28
    public function __construct(string $name, ?string $description, Deployer $deployer)
29
    {
30
        parent::__construct($name, $deployer);
31
        if ($description) {
32
            $this->setDescription($description);
33
        }
34
    }
35
36
    protected function configure()
37
    {
38
        parent::configure();
39
        $this->addOption(
40
            'limit',
41
            'l',
42
            Option::VALUE_REQUIRED,
43
            'How many tasks to run in parallel?'
44
        );
45
        $this->addOption(
46
            'no-hooks',
47
            null,
48
            Option::VALUE_NONE,
49
            'Run tasks without after/before hooks'
50
        );
51
        $this->addOption(
52
            'plan',
53
            null,
54
            Option::VALUE_NONE,
55
            'Show execution plan'
56
        );
57
        $this->addOption(
58
            'log',
59
            null,
60
            Option::VALUE_REQUIRED,
61
            'Log to file'
62
        );
63
        $this->addOption(
64
            'profile',
65
            null,
66
            Option::VALUE_REQUIRED,
67
            'Writes tasks profile fo PROFILE file'
68
        );
69
    }
70
71
    protected function execute(Input $input, Output $output)
72
    {
73
        $hooksEnabled = !$input->getOption('no-hooks');
74
        $this->deployer->config['log_file'] = $input->getOption('log');
75
        $hosts = $this->selectHosts($input, $output);
76
        $plan = $input->getOption('plan') ? new Planner($output, $hosts) : null;
77
78
        if ($plan === null) {
79
            // Materialize hosts configs
80
            $configDirectory = sprintf('%s/%s', sys_get_temp_dir(), uniqid());
81
            if (!is_dir($configDirectory)) {
82
                mkdir($configDirectory, 0700, true);
83
            }
84
            $this->deployer->config->set('config_directory', $configDirectory);
0 ignored issues
show
Bug introduced by Anton Medvedev
The method set cannot be called on $this->deployer->config (of type array<string,string|arra...string>|boolean|null"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
85
            foreach ($hosts as $alias => $host) {
86
                $host->getConfig()->save();
87
            }
88
        }
89
90
        $this->deployer->scriptManager->setHooksEnabled($hooksEnabled);
91
        $tasks = $this->deployer->scriptManager->getTasks($this->getName());
92
        if (empty($tasks)) {
93
            throw new Exception('No task will be executed, because the selected hosts do not meet the conditions of the tasks');
94
        }
95
96
        $exitCode = $this->deployer->executor->run($tasks, $hosts, $plan);
97
98
        if ($plan) {
99
            $plan->render();
100
            return 0;
101
        }
102
103
        if ($exitCode === 0) {
104
            return 0;
105
        }
106
        if ($exitCode === GracefulShutdownException::EXIT_CODE) {
107
            return 1;
108
        }
109
110
        // Check if we have tasks to execute on failure.
111
        if ($this->deployer['fail']->has($this->getName())) {
112
            $taskName = $this->deployer['fail']->get($this->getName());
113
            $tasks = $this->deployer->scriptManager->getTasks($taskName);
114
            $this->deployer->executor->run($tasks, $hosts);
115
        }
116
117
        return $exitCode;
118
    }
119
120
    private function parseOptions(array $options)
0 ignored issues
show
Bug introduced by Anton Medvedev
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
Unused Code introduced by Anton Medvedev
This method is not used, and could be removed.
Loading history...
121
    {
122
        foreach ($options as $option) {
123
            list($name, $value) = explode('=', $option);
124
            $value = $this->castValueToPhpType($value);
125
            $this->deployer->config->set($name, $value);
126
        }
127
    }
128
129
    private function castValueToPhpType($value)
0 ignored issues
show
Bug introduced by Anton Medvedev
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
130
    {
131
        switch ($value) {
132
            case 'true':
133
                return true;
134
            case 'false':
135
                return false;
136
            default:
137
                return $value;
138
        }
139
    }
140
}
141