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 ( e70437...b53fd1 )
by Anton
03:00
created

Deployer::__construct()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 78
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 3.0797

Importance

Changes 0
Metric Value
cc 3
eloc 36
nc 1
nop 3
dl 0
loc 78
ccs 23
cts 29
cp 0.7931
crap 3.0797
rs 8.9019
c 0
b 0
f 0

How to fix   Long Method   

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;
9
10
use Deployer\Collection\Collection;
11
use Deployer\Console\InitCommand;
12
use Deployer\Console\WorkerCommand;
13
use Deployer\Console\Application;
14
use Deployer\Server;
15
use Deployer\Stage\StageStrategy;
16
use Deployer\Task;
17
use Deployer\Console\TaskCommand;
18
use Monolog\Handler\NullHandler;
19
use Monolog\Handler\StreamHandler;
20
use Monolog\Logger;
21
use Pimple\Container;
22
use Psr\Log\LoggerInterface;
23
use Symfony\Component\Console;
24
25
/**
26
 * @property Task\TaskCollection|Task\Task[] tasks
27
 * @property Server\ServerCollection|Server\ServerInterface[] servers
28
 * @property Server\EnvironmentCollection|Server\Environment[] environments
29
 * @property Collection config
30
 */
31
class Deployer extends Container
32
{
33
    /**
34
     * Global instance of deployer. It's can be accessed only after constructor call.
35
     * @var Deployer
36
     */
37
    private static $instance;
38
39
    /**
40
     * @param Application $console
41
     * @param Console\Input\InputInterface $input
42
     * @param Console\Output\OutputInterface $output
43
     */
44
    public function __construct(Application $console, Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
45
    {
46
        parent::__construct();
47
48
        /******************************
49
         *           Console          *
50
         ******************************/
51
52
        $this['console'] = function () use ($console) {
53
            return $console;
54
        };
55
        $this['input'] = function () use ($input) {
56
            return $input;
57
        };
58
        $this['output'] = function () use ($output) {
59
            return $output;
60
        };
61
62
        /******************************
63
         *           Config           *
64
         ******************************/
65 35
66
        $this['config'] = function () {
67 35
            return new Collection();
68 35
        };
69 35
        $this->config['ssh_type'] = 'phpseclib';
70
        $this->config['default_stage'] = null;
71 35
72 35
        /******************************
73 35
         *            Core            *
74 35
         ******************************/
75 35
76 35
        $this['tasks'] = function () {
77
            return new Task\TaskCollection();
78 35
        };
79
        $this['servers'] = function () {
80 35
            return new Server\ServerCollection();
81 35
        };
82
        $this['environments'] = function () {
83
            return new Server\EnvironmentCollection();
84
        };
85
        $this['scriptManager'] = function ($c) {
86 22
            return new Task\ScriptManager($c['tasks']);
87
        };
88 22
        $this['stageStrategy'] = function ($c) {
89
            return new StageStrategy($c['servers'], $c['environments'], $c['config']['default_stage']);
90
        };
91
        $this['onFailure'] = function () {
92
            return new Collection();
93
        };
94
95
        /******************************
96
         *           Logger           *
97
         ******************************/
98
99
        $this['log_level'] = Logger::DEBUG;
100
        $this['log_handler'] = function () {
101
            return isset($this->config['log_file'])
102
                ? new StreamHandler($this->config['log_file'], $this['log_level'])
103
                : new NullHandler($this['log_level']);
104
        };
105
        $this['log'] = function () {
106
            $name = isset($this->config['log_name']) ? $this->config['log_name'] : 'Deployer';
107 14
            return new Logger($name, [
108
                $this['log_handler']
109 14
            ]);
110
        };
111 14
112 14
        /******************************
113 14
         *        Init command        *
114
         ******************************/
115
116 14
        $this['init_command'] = function () {
117 14
            return new InitCommand();
118 14
        };
119
120
        self::$instance = $this;
121
    }
122
123
    /**
124
     * @return Deployer
125
     */
126
    public static function get()
127
    {
128
        return self::$instance;
129
    }
130
131
    /**
132
     * @param string $name
133
     * @param mixed $value
134
     */
135
    public static function setDefault($name, $value)
136
    {
137
        Deployer::get()->config[$name] = $value;
138
    }
139
140
    /**
141 35
     * @param string $name
142
     * @param mixed $default
143 35
     * @return mixed
144 35
     */
145
    public static function getDefault($name, $default = null)
146 1
    {
147
        return self::hasDefault($name) ? Deployer::get()->config[$name] : $default;
148
    }
149
150
    /**
151
     * @param string $name
152
     * @return boolean
153
     */
154
    public static function hasDefault($name)
155
    {
156
        return isset(Deployer::get()->config[$name]);
157
    }
158
159
    /**
160
     * @param string $name
161
     * @param array $array
162 14
     */
163
    public static function addDefault($name, $array)
164 14
    {
165
        if (self::hasDefault($name)) {
166
            $config = self::getDefault($name);
167
            if (!is_array($config)) {
168
                throw new \RuntimeException("Configuration parameter `$name` isn't array.");
169
            }
170 15
            self::setDefault($name, array_merge($config, $array));
171
        } else {
172 15
            self::setDefault($name, $array);
173
        }
174
    }
175
176
    /**
177
     * Run console application.
178
     */
179
    public function run()
180
    {
181
        $this->addConsoleCommands();
182
183
        $this->getConsole()->add(new WorkerCommand($this));
184
        $this->getConsole()->add($this['init_command']);
185
186
        $this->getConsole()->run($this->input, $this->output);
0 ignored issues
show
Documentation introduced by
The property input does not exist on object<Deployer\Deployer>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property output does not exist on object<Deployer\Deployer>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
187
    }
188
189
    /**
190
     * Transform tasks to console commands.
191
     */
192
    public function addConsoleCommands()
193
    {
194
        $this->getConsole()->addUserArgumentsAndOptions();
195
196
        foreach ($this->tasks as $name => $task) {
197
            if ($task->isPrivate()) {
198
                continue;
199
            }
200
201
            $this->getConsole()->add(new TaskCommand($name, $task->getDescription(), $this));
202
        }
203
    }
204
205
    /**
206
     * @param string $name
207
     * @return mixed
208
     * @throws \InvalidArgumentException
209
     */
210
    public function __get($name)
211
    {
212
        if (isset($this[$name])) {
213
            return $this[$name];
214
        } else {
215
            throw new \InvalidArgumentException("Property \"$name\" does not exist.");
216
        }
217
    }
218
219
    /**
220
     * @return Application
221
     */
222
    public function getConsole()
223
    {
224
        return $this['console'];
225
    }
226
227
    /**
228
     * @return Console\Input\InputInterface
229
     */
230
    public function getInput()
231
    {
232
        return $this['input'];
233
    }
234
235
    /**
236
     * @return Console\Output\OutputInterface
237
     */
238
    public function getOutput()
239
    {
240
        return $this['output'];
241
    }
242
243
    /**
244
     * @param string $name
245
     * @return Console\Helper\HelperInterface
246
     */
247
    public function getHelper($name)
248
    {
249
        return $this->getConsole()->getHelperSet()->get($name);
250
    }
251
252
    /**
253
     * @return StageStrategy
254
     */
255
    public function getStageStrategy()
256
    {
257
        return $this['stageStrategy'];
258
    }
259
260
    /**
261
     * @return Task\ScriptManager
262
     */
263
    public function getScriptManager()
264
    {
265
        return $this['scriptManager'];
266
    }
267
268
    /**
269
     * @return LoggerInterface
270
     */
271
    public function getLogger()
272
    {
273
        return $this['log'];
274
    }
275
}
276