Completed
Pull Request — master (#266)
by Enrico
10:12
created

CompileCommand   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 10

Test Coverage

Coverage 10.17%

Importance

Changes 0
Metric Value
dl 0
loc 108
ccs 6
cts 59
cp 0.1017
rs 10
c 0
b 0
f 0
wmc 12
lcom 0
cbo 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
C execute() 0 82 10
A getCompiler() 0 4 1
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\Config\FileLocator;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Webiny\Component\EventManager\EventManager;
21
22
class CompileCommand extends Command
23
{
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 872
    protected function configure()
29
    {
30 872
        $this
31 872
            ->setName('compile')
32 872
            ->setDescription('Runs compiler on all files in path')
33 872
            ->addArgument('path', InputArgument::OPTIONAL, 'Path to check file or directory', '.');
34 872
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    protected function execute(InputInterface $input, OutputInterface $output)
40
    {
41
        $output->writeln('');
42
43
        if (extension_loaded('xdebug')) {
44
            /**
45
             * This will disable only showing stack traces on error conditions.
46
             */
47
            if (function_exists('xdebug_disable')) {
48
                xdebug_disable();
49
            }
50
51
            $output->writeln('<error>It is highly recommended to disable the XDebug extension before invoking this command.</error>');
52
        }
53
54
        $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7, new \PhpParser\Lexer\Emulative([
55
            'usedAttributes' => [
56
                'comments',
57
                'startLine',
58
                'endLine',
59
                'startTokenPos',
60
                'endTokenPos'
61
            ]
62
        ]));
63
64
        /** @var Application $application */
65
        $application = $this->getApplication();
66
        $application->compiler = new Compiler();
67
68
        $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...
69
        $context = new Context($output, $application, $em);
70
71
        $fileParser = new FileParser(
72
            $parser,
73
            $this->getCompiler()
74
        );
75
76
        $path = $input->getArgument('path');
77
        if (is_dir($path)) {
78
            $directoryIterator = new RecursiveIteratorIterator(
79
                new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)
80
            );
81
            $output->writeln('Scanning directory <info>' . $path . '</info>');
82
83
            $count = 0;
84
85
            /** @var SplFileInfo $file */
86
            foreach ($directoryIterator as $file) {
87
                if ($file->getExtension() !== 'php') {
88
                    continue;
89
                }
90
91
                $context->debug($file->getPathname());
92
                $count++;
93
            }
94
95
            $output->writeln("Found <info>{$count} files</info>");
96
97
            if ($count > 100) {
98
                $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>');
99
            }
100
101
            $output->writeln('');
102
103
            /** @var SplFileInfo $file */
104
            foreach ($directoryIterator as $file) {
105
                if ($file->getExtension() !== 'php') {
106
                    continue;
107
                }
108
109
                $fileParser->parserFile($file->getPathname(), $context);
110
            }
111
        } elseif (is_file($path)) {
112
            $fileParser->parserFile($path, $context);
113
        }
114
115
116
        /**
117
         * Step 2 Recursive check ...
118
         */
119
        $application->compiler->compile($context);
120
    }
121
122
    /**
123
     * @return Compiler
124
     */
125
    protected function getCompiler()
126
    {
127
        return $this->getApplication()->compiler;
0 ignored issues
show
Bug introduced by
The property compiler does not seem to exist in Symfony\Component\Console\Application.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
128
    }
129
}
130