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 ( 64f5bb...5f2d07 )
by Anton
02:44
created

InitCommand::execute()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 76
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 54
nc 4
nop 2
dl 0
loc 76
ccs 0
cts 0
cp 0
crap 12
rs 8.9667
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\Console;
9
10
use Deployer\Initializer\Initializer;
11
use Deployer\Initializer\Template\CakeTemplate;
12
use Deployer\Initializer\Template\CodeIgniterTemplate;
13
use Deployer\Initializer\Template\CommonTemplate;
14
use Deployer\Initializer\Template\DrupalTemplate;
15
use Deployer\Initializer\Template\LaravelTemplate;
16
use Deployer\Initializer\Template\SymfonyTemplate;
17
use Deployer\Initializer\Template\Yii2AdvancedAppTemplate;
18
use Deployer\Initializer\Template\Yii2BasicAppTemplate;
19
use Deployer\Initializer\Template\YiiTemplate;
20
use Deployer\Initializer\Template\ZendTemplate;
21
use Symfony\Component\Console\Command\Command;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Input\InputOption;
24
use Symfony\Component\Console\Output\OutputInterface;
25
use Symfony\Component\Console\Style\SymfonyStyle;
26
use Symfony\Component\Process\Exception\RuntimeException;
27
use Symfony\Component\Process\Process;
28
29
/**
30
 * The command for initialize Deployer system in your project
31
 *
32
 * @author Vitaliy Zhuk <[email protected]>
33
 */
34
class InitCommand extends Command
35
{
36
    /**
37
     * @var Initializer
38
     */
39
    private $initializer;
40
41
    /**
42
     * @var array
43
     */
44
    private $availableTemplates;
45
46
    /**
47
     * Construct
48
     *
49
     * @param string $name
50
     */
51 8
    public function __construct($name = null)
52
    {
53 8
        $this->initializer = $this->createInitializer();
54 8
        $this->availableTemplates = $this->initializer->getTemplateNames();
55
56 8
        parent::__construct($name);
57 8
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62 8
    protected function configure()
63
    {
64
        $this
65 8
            ->setName('init')
66 8
            ->setDescription('Initialize deployer system in your project')
67 8
            ->addOption('template', 't', InputOption::VALUE_OPTIONAL, 'The template of you project. Available templates: ' . implode(', ', $this->availableTemplates))
68 8
            ->addOption('directory', null, InputOption::VALUE_OPTIONAL, 'The directory for create "deploy.php" file', getcwd())
69 8
            ->addOption('filename', null, InputOption::VALUE_OPTIONAL, 'The file name. Default "deploy.php"', 'deploy.php');
70 8
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    protected function execute(InputInterface $input, OutputInterface $output)
1 ignored issue
show
Coding Style introduced by
execute uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
76
    {
77
        $template = $input->getOption('template');
78
        $directory = $input->getOption('directory');
79
        $file = $input->getOption('filename');
80
        $params = [];
81
82
        if ($template === null) {
83
            $io = new SymfonyStyle($input, $output);
84
            $helper = $this->getHelper('question');
0 ignored issues
show
Unused Code introduced by
$helper is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
85
            $formatter = $this->getHelper('formatter');
86
87
            // Welcome message
88
            $output->writeln([
89
                '',
90
                $formatter->formatBlock('Welcome to the Deployer config generator', 'bg=blue;fg=white', true),
91
                '',
92
            ]);
93
94
            $io->text([
95
                'This utility will walk you through creating a deploy.php file.',
96
                'It only covers the most common items, and tries to guess sensible defaults.',
97
                '',
98
                'Press ^C at any time to quit.',
99
            ]);
100
101
            // Project type
102
            $template = $io->choice('Please select your project type', $this->availableTemplates, 'Common');
103
104
            // Repo
105
            $default = false;
106
            try {
107
                $default = (new Process('git remote get-url origin'))
108
                    ->mustRun()
109
                    ->getOutput();
110
                $default = trim($default);
111
            } catch (RuntimeException $e) {
112
                // pass
113
            }
114
            $params['repository'] = $io->ask('Repository', $default);
115
116
            // Privacy
117
            $io->text([
118
                'Contribute to the Deployer Development',
119
                '',
120
                'In order to help development and improve Deployer features in,',
121
                'Deployer has a setting for collection of usage data. This function',
122
                'collects anonymous usage data and sends it to Deployer. The data is',
123
                'used in Deployer development to get reliable statistics on which',
124
                'features are used (or not used). The information is not traceable',
125
                'to any individual or organization. Participation is voluntary,',
126
                'and you can change your mind at any time.',
127
                '',
128
                'Anonymous usage data contains Deployer version, php version, os type,',
129
                'name of the command being executed and whether it was successful or not,',
130
                'exception class name, count of hosts and anonymized project hash.',
131
                '',
132
                'If you would like to allow us to gather this information and help',
133
                'us develop a better tool, please add the code below.',
134
                '',
135
                "    <fg=white>set(<fg=cyan>'allow_anonymous_stats'</fg=cyan>, <fg=magenta;options=bold>true</fg=magenta;options=bold>);</fg=white>",
136
                '',
137
                'This function will not affect the performance of Deployer as',
138
                'the data is insignificant and transmitted in separate process.',
139
            ]);
140
141
            $params['allow_anonymous_stats'] = $GLOBALS['allow_anonymous_stats'] = $io->confirm('Do you confirm?');
142
        }
143
144
        $filePath = $this->initializer->initialize($template, $directory, $file, $params);
145
146
        $output->writeln(sprintf(
147
            '<info>Successfully created:</info> <comment>%s</comment>',
148
            $filePath
149
        ));
150
    }
151
152
    /**
153
     * Create a initializer system
154
     *
155
     * @return Initializer
156
     */
157 8
    private function createInitializer()
158
    {
159 8
        $initializer = new Initializer();
160
161 8
        $initializer->addTemplate('Common', new CommonTemplate());
162 8
        $initializer->addTemplate('Laravel', new LaravelTemplate());
163 8
        $initializer->addTemplate('Symfony', new SymfonyTemplate());
164 8
        $initializer->addTemplate('Yii', new YiiTemplate());
165 8
        $initializer->addTemplate('Yii2 Basic App', new Yii2BasicAppTemplate());
166 8
        $initializer->addTemplate('Yii2 Advanced App', new Yii2AdvancedAppTemplate());
167 8
        $initializer->addTemplate('Zend Framework', new ZendTemplate());
168 8
        $initializer->addTemplate('CakePHP', new CakeTemplate());
169 8
        $initializer->addTemplate('CodeIgniter', new CodeIgniterTemplate());
170 8
        $initializer->addTemplate('Drupal', new DrupalTemplate());
171
172 8
        return $initializer;
173
    }
174
}
175