Completed
Pull Request — master (#139)
by Kévin
03:44
created

CheckCommand::execute()   D

Complexity

Conditions 13
Paths 96

Size

Total Lines 107
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
cc 13
eloc 56
nc 96
nop 2
dl 0
loc 107
ccs 0
cts 69
cp 0
crap 182
rs 4.9922
c 0
b 0
f 0

How to fix   Long Method    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
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\Command;
7
8
use PhpParser\ParserFactory;
9
use PHPSA\Analyzer;
10
use PHPSA\Application;
11
use PHPSA\Compiler;
12
use PHPSA\Configuration;
13
use PHPSA\ConfigurationLoader;
14
use PHPSA\Context;
15
use PHPSA\Definition\FileParser;
16
use RecursiveDirectoryIterator;
17
use RecursiveIteratorIterator;
18
use SplFileInfo;
19
use FilesystemIterator;
20
use Symfony\Component\Config\FileLocator;
21
use Symfony\Component\Console\Command\Command;
22
use Symfony\Component\Console\Input\InputArgument;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Input\InputOption;
25
use Symfony\Component\Console\Output\OutputInterface;
26
use Webiny\Component\EventManager\EventManager;
27
use PHPSA\Analyzer\Pass as AnalyzerPass;
28
29
/**
30
 * Class CheckCommand
31
 * @package PHPSA\Command
32
 *
33
 * @method Application getApplication();
34
 */
35
class CheckCommand extends Command
36
{
37
38
    /**
39
     * Configures the command.
40
     */
41 747
    protected function configure()
42
    {
43 747
        $this
44 747
            ->setName('check')
45 747
            ->setDescription('SPA')
46 747
            ->addOption('blame', null, InputOption::VALUE_NONE, 'Git blame author for bad code ;)')
47 747
            ->addOption('config-file', null, InputOption::VALUE_REQUIRED, 'Path to the configuration file.')
48 747
            ->addArgument('path', InputArgument::OPTIONAL, 'Path to check file or directory', '.')
49 747
            ->addOption(
50 747
                'report-json',
51 747
                null,
52 747
                InputOption::VALUE_REQUIRED,
53
                'Path to save detailed report in JSON format. Example: /tmp/report.json'
54 747
            );
55 747
    }
56
57
    /**
58
     * Executes the command.
59
     */
60
    protected function execute(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Complexity introduced by
This operation has 1200 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
61
    {
62
        $output->writeln('');
63
64
        if (extension_loaded('xdebug')) {
65
            /**
66
             * This will disable only showing stack traces on error conditions.
67
             */
68
            if (function_exists('xdebug_disable')) {
69
                xdebug_disable();
70
            }
71
72
            $output->writeln('<error>It is highly recommended to disable the XDebug extension before invoking this command.</error>');
73
        }
74
75
        $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7, new \PhpParser\Lexer\Emulative([
76
            'usedAttributes' => [
77
                'comments',
78
                'startLine',
79
                'endLine',
80
                'startTokenPos',
81
                'endTokenPos'
82
            ]
83
        ]));
84
85
        /** @var Application $application */
86
        $application = $this->getApplication();
87
        $application->compiler = new Compiler();
88
89
        $configFile = $input->getOption('config-file') ?: '.phpsa.yml';
90
        $configDir = realpath($input->getArgument('path'));
91
        $application->configuration = $this->loadConfiguration($configFile, $configDir);
92
93
        $em = EventManager::getInstance();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $em. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
94
        Analyzer\Factory::factory($em, $application->configuration);
95
        $context = new Context($output, $application, $em);
96
97
        /**
98
         * Store option's in application's configuration
99
         */
100
        if ($input->getOption('blame')) {
101
            $application->configuration->setValue('blame', true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
102
        }
103
104
        $fileParser = new FileParser(
105
            $parser,
106
            $this->getCompiler()
107
        );
108
109
        $path = $input->getArgument('path');
110
        if (is_dir($path)) {
111
            $directoryIterator = new RecursiveIteratorIterator(
112
                new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)
113
            );
114
            $output->writeln('Scanning directory <info>' . $path . '</info>');
115
116
            $count = 0;
117
118
            /** @var SplFileInfo $file */
119
            foreach ($directoryIterator as $file) {
120
                if ($file->getExtension() !== 'php') {
121
                    continue;
122
                }
123
124
                $context->debug($file->getPathname());
125
                $count++;
126
            }
127
128
            $output->writeln("Found <info>{$count} files</info>");
129
130
            if ($count > 100) {
131
                $output->writeln('<comment>Caution: You are trying to scan a lot of files; this might be slow. For bigger libraries, consider setting up a dedicated platform or using ci.lowl.io.</comment>');
132
            }
133
134
            $output->writeln('');
135
136
            /** @var SplFileInfo $file */
137
            foreach ($directoryIterator as $file) {
138
                if ($file->getExtension() !== 'php') {
139
                    continue;
140
                }
141
142
                $fileParser->parserFile($file->getPathname(), $context);
143
            }
144
        } elseif (is_file($path)) {
145
            $fileParser->parserFile($path, $context);
146
        }
147
148
149
        /**
150
         * Step 2 Recursive check ...
151
         */
152
        $application->compiler->compile($context);
153
154
        $jsonReport = $input->getOption('report-json');
155
        if ($jsonReport) {
156
            file_put_contents(
157
                $jsonReport,
158
                json_encode(
159
                    $this->getApplication()->getIssuesCollector()->getIssues()
160
                )
161
            );
162
        }
163
164
        $output->writeln('');
165
        $output->writeln('Memory usage: ' . $this->getMemoryUsage(false) . ' (peak: ' . $this->getMemoryUsage(true) . ') MB');
166
    }
167
168
    /**
169
     * @param boolean $type
170
     * @return float
171
     */
172
    protected function getMemoryUsage($type)
173
    {
174
        return round(memory_get_usage($type) / 1024 / 1024, 2);
175
    }
176
177
    /**
178
     * @return Compiler
179
     */
180
    protected function getCompiler()
181
    {
182
        return $this->getApplication()->compiler;
183
    }
184
185
    /**
186
     * @param string $configFile
187
     * @param string $configurationDirectory
188
     *
189
     * @return Configuration
190
     */
191
    protected function loadConfiguration($configFile, $configurationDirectory)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $configurationDirectory exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
192
    {
193
        $loader = new ConfigurationLoader(new FileLocator([
194
            getcwd(),
195
            $configurationDirectory
196
        ]));
197
198
        return new Configuration(
199
            $loader->load($configFile),
200
            Analyzer\Factory::getPassesConfigurations()
201
        );
202
    }
203
}
204