Completed
Push — master ( ded049...28d1f7 )
by Дмитрий
02:21
created

CheckCommand::execute()   C

Complexity

Conditions 12
Paths 48

Size

Total Lines 94
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
cc 12
eloc 49
nc 48
nop 2
dl 0
loc 94
ccs 0
cts 61
cp 0
crap 156
rs 5.034
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 PHPSA\Analyzer;
9
use PHPSA\Application;
10
use PHPSA\Compiler;
11
use PHPSA\Context;
12
use PHPSA\Definition\FileParser;
13
use RecursiveDirectoryIterator;
14
use RecursiveIteratorIterator;
15
use SplFileInfo;
16
use FilesystemIterator;
17
use Symfony\Component\Config\FileLocator;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Webiny\Component\EventManager\EventManager;
23
24
/**
25
 * Command to run compiler and analyzers on files
26
 *
27
 * @package PHPSA\Command
28
 * @method Application getApplication();
29
 */
30
class CheckCommand extends AbstractCommand
31
{
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 18
    protected function configure()
37
    {
38 18
        $this
39 18
            ->setName('check')
40 18
            ->setDescription('Runs compiler and analyzers on all files in path')
41 18
            ->addOption('config-file', null, InputOption::VALUE_REQUIRED, 'Path to the configuration file.')
42 18
            ->addArgument('path', InputArgument::OPTIONAL, 'Path to check file or directory', '.')
43 18
            ->addOption(
44 18
                'report-json',
45 18
                null,
46 18
                InputOption::VALUE_REQUIRED,
47
                'Path to save detailed report in JSON format. Example: /tmp/report.json'
48 18
            );
49 18
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    protected function execute(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Complexity introduced by
This operation has 600 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...
55
    {
56
        $output->writeln('');
57
58
        if (extension_loaded('xdebug')) {
59
            /**
60
             * This will disable only showing stack traces on error conditions.
61
             */
62
            if (function_exists('xdebug_disable')) {
63
                xdebug_disable();
64
            }
65
66
            $output->writeln('<error>It is highly recommended to disable the XDebug extension before invoking this command.</error>');
67
        }
68
69
        /** @var Application $application */
70
        $application = $this->getApplication();
71
        $application->compiler = new Compiler();
72
73
        $configFile = $input->getOption('config-file') ?: '.phpsa.yml';
74
        $configDir = realpath($input->getArgument('path'));
75
        $application->configuration = $this->loadConfiguration($configFile, $configDir);
76
77
        $parser = $this->createParser($application);
0 ignored issues
show
Documentation introduced by
$application is of type object<PHPSA\Application>, 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...
78
79
        $output->writeln('Used config file: ' . $application->configuration->getPath());
80
81
        $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...
82
        Analyzer\Factory::factory($em, $application->configuration);
83
        $context = new Context($output, $application, $em);
84
85
        $fileParser = new FileParser(
86
            $parser,
87
            $application->compiler
88
        );
89
90
        $path = $input->getArgument('path');
91
        if (is_dir($path)) {
92
            $directoryIterator = new RecursiveIteratorIterator(
93
                new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)
94
            );
95
            $output->writeln('Scanning directory <info>' . $path . '</info>');
96
97
            $count = 0;
98
99
            /** @var SplFileInfo $file */
100
            foreach ($directoryIterator as $file) {
101
                if ($file->getExtension() !== 'php') {
102
                    continue;
103
                }
104
105
                $context->debug($file->getPathname());
106
                $count++;
107
            }
108
109
            $output->writeln("Found <info>{$count} files</info>");
110
111
            if ($count > 100) {
112
                $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>');
113
            }
114
115
            $output->writeln('');
116
117
            /** @var SplFileInfo $file */
118
            foreach ($directoryIterator as $file) {
119
                if ($file->getExtension() !== 'php') {
120
                    continue;
121
                }
122
123
                $fileParser->parserFile($file->getPathname(), $context);
124
            }
125
        } elseif (is_file($path)) {
126
            $fileParser->parserFile($path, $context);
127
        }
128
129
130
        /**
131
         * Step 2 Recursive check ...
132
         */
133
        $application->compiler->compile($context);
134
135
        $jsonReport = $input->getOption('report-json');
136
        if ($jsonReport) {
137
            file_put_contents(
138
                $jsonReport,
139
                json_encode(
140
                    $this->getApplication()->getIssuesCollector()->getIssues()
141
                )
142
            );
143
        }
144
145
        $output->writeln('');
146
        $output->writeln('Memory usage: ' . $this->getMemoryUsage(false) . ' (peak: ' . $this->getMemoryUsage(true) . ') MB');
147
    }
148
}
149