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

CompileCommand   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Test Coverage

Coverage 12.07%

Importance

Changes 0
Metric Value
dl 0
loc 101
ccs 7
cts 58
cp 0.1207
rs 10
c 0
b 0
f 0
wmc 12
lcom 2
cbo 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
C execute() 0 83 11
1
<?php
2
3
namespace PHPSA\Command;
4
5
use PhpParser\ParserFactory;
6
use PHPSA\Application;
7
use PHPSA\Compiler;
8
use PHPSA\Context;
9
use PHPSA\Definition\FileParser;
10
use RecursiveDirectoryIterator;
11
use RecursiveIteratorIterator;
12
use SplFileInfo;
13
use FilesystemIterator;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Webiny\Component\EventManager\EventManager;
19
20
/**
21
 * Command to run compiler on files (no analyzer)
22
 */
23
class CompileCommand extends AbstractCommand
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28 18
    protected function configure()
29
    {
30 18
        $this
31 18
            ->setName('compile')
32 18
            ->setDescription('Runs compiler on all files in path')
33 18
            ->addOption('config-file', null, InputOption::VALUE_REQUIRED, 'Path to the configuration file.')
34 18
            ->addArgument('path', InputArgument::OPTIONAL, 'Path to check file or directory', '.');
35 18
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    protected function execute(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Complexity introduced by
This operation has 300 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...
41
    {
42
        $output->writeln('');
43
44
        if (extension_loaded('xdebug')) {
45
            /**
46
             * This will disable only showing stack traces on error conditions.
47
             */
48
            if (function_exists('xdebug_disable')) {
49
                xdebug_disable();
50
            }
51
52
            $output->writeln('<error>It is highly recommended to disable the XDebug extension before invoking this command.</error>');
53
        }
54
55
        /** @var Application $application */
56
        $application = $this->getApplication();
57
        $application->compiler = new Compiler();
58
59
        $configFile = $input->getOption('config-file') ?: '.phpsa.yml';
60
        $configDir = realpath($input->getArgument('path'));
61
        $application->configuration = $this->loadConfiguration($configFile, $configDir);
62
63
        $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...
64
65
        $output->writeln('Used config file: ' . $application->configuration->getPath());
66
67
        $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...
68
        $context = new Context($output, $application, $em);
69
70
        $fileParser = new FileParser(
71
            $parser,
72
            $application->compiler
73
        );
74
75
        $path = $input->getArgument('path');
76
        if (is_dir($path)) {
77
            $directoryIterator = new RecursiveIteratorIterator(
78
                new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)
79
            );
80
            $output->writeln('Scanning directory <info>' . $path . '</info>');
81
82
            $count = 0;
83
84
            /** @var SplFileInfo $file */
85
            foreach ($directoryIterator as $file) {
86
                if ($file->getExtension() !== 'php') {
87
                    continue;
88
                }
89
90
                $context->debug($file->getPathname());
91
                $count++;
92
            }
93
94
            $output->writeln("Found <info>{$count} files</info>");
95
96
            if ($count > 100) {
97
                $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>');
98
            }
99
100
            $output->writeln('');
101
102
            /** @var SplFileInfo $file */
103
            foreach ($directoryIterator as $file) {
104
                if ($file->getExtension() !== 'php') {
105
                    continue;
106
                }
107
108
                $fileParser->parserFile($file->getPathname(), $context);
109
            }
110
        } elseif (is_file($path)) {
111
            $fileParser->parserFile($path, $context);
112
        }
113
114
115
        /**
116
         * Step 2 Recursive check ...
117
         */
118
        $application->compiler->compile($context);
119
120
        $output->writeln('');
121
        $output->writeln('Memory usage: ' . $this->getMemoryUsage(false) . ' (peak: ' . $this->getMemoryUsage(true) . ') MB');
122
    }
123
}
124