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.
Test Failed
Push — master ( 31f734...513d45 )
by Anton
06:23
created

TaskCommand   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 165
Duplicated Lines 4.24 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 7
loc 165
ccs 0
cts 95
cp 0
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 50 1
C execute() 7 62 11
A parseOptions() 0 8 2
A castValueToPhpType() 0 11 3

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\Console;
9
10
use Deployer\Deployer;
11
use Deployer\Exception\Exception;
12
use Deployer\Exception\GracefulShutdownException;
13
use Deployer\Executor\ExecutorInterface;
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface as Input;
17
use Symfony\Component\Console\Input\InputOption as Option;
18
use Symfony\Component\Console\Output\OutputInterface as Output;
19
20
class TaskCommand extends Command
21
{
22
    /**
23
     * @var Deployer
24
     */
25
    private $deployer;
26
27
    /**
28
     * @var ExecutorInterface
29
     */
30
    public $executor;
31
32
    /**
33
     * @param string $name
34
     * @param string $description
35
     * @param Deployer $deployer
36
     */
37
    public function __construct($name, $description, Deployer $deployer)
38
    {
39
        parent::__construct($name);
40
        $this->setDescription($description);
41
        $this->deployer = $deployer;
42
    }
43
44
    /**
45
     * Configures the command
46
     */
47
    protected function configure()
48
    {
49
        $this->addArgument(
50
            'stage',
51
            InputArgument::OPTIONAL,
52
            'Stage or hostname'
53
        );
54
        $this->addOption(
55
            'parallel',
56
            'p',
57
            Option::VALUE_NONE,
58
            'Run tasks in parallel'
59
        );
60
        $this->addOption(
61
            'limit',
62
            'l',
63
            Option::VALUE_REQUIRED,
64
            'How many host to run in parallel?'
65
        );
66
        $this->addOption(
67
            'no-hooks',
68
            null,
69
            Option::VALUE_NONE,
70
            'Run task without after/before hooks'
71
        );
72
        $this->addOption(
73
            'log',
74
            null,
75
            Option::VALUE_REQUIRED,
76
            'Log to file'
77
        );
78
        $this->addOption(
79
            'roles',
80
            null,
81
            Option::VALUE_REQUIRED,
82
            'Roles to deploy'
83
        );
84
        $this->addOption(
85
            'hosts',
86
            null,
87
            Option::VALUE_REQUIRED,
88
            'Host to deploy, comma separated, supports ranges [:]'
89
        );
90
        $this->addOption(
91
            'option',
92
            'o',
93
            Option::VALUE_REQUIRED | Option::VALUE_IS_ARRAY,
94
            'Sets configuration option'
95
        );
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    protected function execute(Input $input, Output $output)
102
    {
103
        $stage = $input->hasArgument('stage') ? $input->getArgument('stage') : null;
104
        $roles = $input->getOption('roles');
105
        $hosts = $input->getOption('hosts');
106
        $this->parseOptions($input->getOption('option'));
107
108
        $hooksEnabled = !$input->getOption('no-hooks');
109
        if (!empty($input->getOption('log'))) {
110
            $this->deployer->config['log_file'] = $input->getOption('log');
111
        }
112
113 View Code Duplication
        if (!empty($hosts)) {
114
            $hosts = $this->deployer->hostSelector->getByHostnames($hosts);
115
        } elseif (!empty($roles)) {
116
            $hosts = $this->deployer->hostSelector->getByRoles($roles);
117
        } else {
118
            $hosts = $this->deployer->hostSelector->getHosts($stage);
0 ignored issues
show
Bug introduced by
It seems like $stage defined by $input->hasArgument('sta...rgument('stage') : null on line 103 can also be of type array<integer,string> or null; however, Deployer\Host\HostSelector::getHosts() 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...
119
        }
120
121
        if (empty($hosts)) {
122
            throw new Exception('No host selected');
123
        }
124
125
        $tasks = $this->deployer->scriptManager->getTasks(
126
            $this->getName(),
127
            $hosts,
128
            $hooksEnabled
129
        );
130
131
        if (empty($tasks)) {
132
            throw new Exception('No task will be executed, because the selected hosts do not meet the conditions of the tasks');
133
        }
134
135
        if ($input->getOption('parallel')) {
136
            $executor = $this->deployer->parallelExecutor;
137
        } else {
138
            $executor = $this->deployer->seriesExecutor;
139
        }
140
141
        try {
142
            $executor->run($tasks, $hosts);
143
        } catch (\Throwable $exception) {
144
            $this->deployer->logger->log('['. get_class($exception) .'] '. $exception->getMessage());
145
            $this->deployer->logger->log($exception->getTraceAsString());
146
147
            if ($exception instanceof GracefulShutdownException) {
148
                throw $exception;
149
            } else {
150
                // Check if we have tasks to execute on failure
151
                if ($this->deployer['fail']->has($this->getName())) {
152
                    $taskName = $this->deployer['fail']->get($this->getName());
153
                    $tasks = $this->deployer->scriptManager->getTasks($taskName, $hosts, $hooksEnabled);
154
155
                    $executor->run($tasks, $hosts);
156
                }
157
                throw $exception;
158
            }
159
        }
160
161
        return 0;
162
    }
163
164
    private function parseOptions(array $options)
165
    {
166
        foreach ($options as $option) {
167
            list($name, $value) = explode('=', $option);
168
            $value = $this->castValueToPhpType($value);
169
            $this->deployer->config->set($name, $value);
170
        }
171
    }
172
173
    private function castValueToPhpType($value)
174
    {
175
        switch ($value) {
176
            case 'true':
177
                return true;
178
            case 'false':
179
                return false;
180
            default:
181
                return $value;
182
        }
183
    }
184
}
185