Completed
Pull Request — master (#345)
by Enrico
05:30
created

CheckCommand   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 11

Test Coverage

Coverage 80.52%

Importance

Changes 0
Metric Value
dl 0
loc 127
ccs 62
cts 77
cp 0.8052
rs 10
c 0
b 0
f 0
wmc 14
lcom 2
cbo 11

2 Methods

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