Completed
Pull Request — master (#354)
by Strahinja
05:29 queued 01:12
created

CheckCommand   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 11

Test Coverage

Coverage 85.53%

Importance

Changes 0
Metric Value
dl 0
loc 146
ccs 65
cts 76
cp 0.8553
rs 10
c 0
b 0
f 0
wmc 21
lcom 2
cbo 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 14 1
D execute() 0 121 20
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 297
    protected function configure()
37
    {
38
        $this
39 297
            ->setName('check')
40 297
            ->setDescription('Runs compiler and analyzers on all files in path')
41 297
            ->addOption('config-file', null, InputOption::VALUE_REQUIRED, 'Path to the configuration file.')
42 297
            ->addArgument('path', InputArgument::OPTIONAL, 'Path to check file or directory', '.')
43 297
            ->addOption(
44 297
                'report-json',
45 297
                null,
46 297
                InputOption::VALUE_REQUIRED,
47 297
                'Path to save detailed report in JSON format. Example: /tmp/report.json'
48
            );
49 297
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 1
    protected function execute(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Complexity introduced by
This operation has 15420 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 1
        $output->writeln('');
57
58 1
        if (extension_loaded('xdebug')) {
59
            /**
60
             * This will disable only showing stack traces on error conditions.
61
             */
62 1
            if (function_exists('xdebug_disable')) {
63 1
                xdebug_disable();
64
            }
65
66 1
            $output->writeln('<error>It is highly recommended to disable the XDebug extension before invoking this command.</error>');
67
        }
68
69
        /** @var Application $application */
70 1
        $application = $this->getApplication();
71 1
        $application->compiler = new Compiler();
72
73 1
        $configFile = $input->getOption('config-file') ?: '.phpsa.yml';
74 1
        $configDir = realpath($input->getArgument('path'));
75 1
        $application->configuration = $this->loadConfiguration($configFile, $configDir);
76
77 1
        $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 1
        $output->writeln('Used config file: ' . $application->configuration->getPath());
80
81 1
        $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 1
        Analyzer\Factory::factory($em, $application->configuration);
83 1
        $context = new Context($output, $application, $em);
84
85 1
        $fileParser = new FileParser(
86 1
            $parser,
87 1
            $application->compiler
88
        );
89
90 1
        $path = $input->getArgument('path');
91 1
        if (is_dir($path)) {
92 1
            $directoryIterator = new RecursiveIteratorIterator(
93 1
                new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)
94
            );
95 1
            $output->writeln('Scanning directory <info>' . $path . '</info>');
96
97 1
            $count = 0;
98
99 1
            $ignore =  $application->configuration->getValue('ignore');
100
            /** @var SplFileInfo $file */
101 1
            foreach ($directoryIterator as $file) {
102 1
                $skip = 0;
103 1
                foreach ($ignore as $item) {
104
                    // its root dir or file
105 1
                    if ($item[0] == "/") {
106 1
                        $item = preg_replace('#/+#', '/', ($path . $item));
107
                    }
108
109 1
                    if (preg_match("#$item#", $file->getPathname())) {
110
                        $skip = 1;
111 1
                        break;
112
                    }
113
                }
114
115 1
                if ($file->getExtension() !== 'php' || $skip) {
116
                    continue;
117
                }
118
119 1
                $context->debug($file->getPathname());
120 1
                $count++;
121
            }
122
123 1
            $output->writeln("Found <info>{$count} files</info>");
124
125 1
            if ($count > 100) {
126
                $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>');
127
            }
128
129 1
            $output->writeln('');
130
131
            /** @var SplFileInfo $file */
132 1
            foreach ($directoryIterator as $file) {
133 1
                $skip = 0;
134 1
                foreach ($ignore as $item) {
135
                    // its root dir or file
136 1
                    if ($item[0] == "/") {
137 1
                        $item = preg_replace('#/+#', '/', ($path . $item));
138
                    }
139
140 1
                    if (preg_match("#$item#", $file->getPathname())) {
141
                        $skip = 1;
142 1
                        break;
143
                    }
144
                }
145
146 1
                if ($file->getExtension() !== 'php' || $skip) {
147
                    continue;
148
                }
149
150 1
                $fileParser->parserFile($file->getPathname(), $context);
151
            }
152
        } elseif (is_file($path)) {
153
            $fileParser->parserFile($path, $context);
154
        }
155
156
157
        /**
158
         * Step 2 Recursive check ...
159
         */
160 1
        $application->compiler->compile($context);
161
162 1
        $jsonReport = $input->getOption('report-json');
163 1
        if ($jsonReport) {
164
            file_put_contents(
165
                $jsonReport,
166
                json_encode(
167
                    $this->getApplication()->getIssuesCollector()->getIssues()
168
                )
169
            );
170
        }
171
172 1
        $output->writeln('');
173 1
        $output->writeln('Memory usage: ' . $this->getMemoryUsage(false) . ' (peak: ' . $this->getMemoryUsage(true) . ') MB');
174 1
    }
175
}
176