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
|
297 |
|
protected function configure() |
29
|
|
|
{ |
30
|
|
|
$this |
31
|
297 |
|
->setName('compile') |
32
|
297 |
|
->setDescription('Runs compiler on all files in path') |
33
|
297 |
|
->addOption('config-file', null, InputOption::VALUE_REQUIRED, 'Path to the configuration file.') |
34
|
297 |
|
->addArgument('path', InputArgument::OPTIONAL, 'Path to check file or directory', '.'); |
35
|
297 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
41
|
|
|
{ |
42
|
1 |
|
$output->writeln(''); |
43
|
|
|
|
44
|
1 |
|
if (extension_loaded('xdebug')) { |
45
|
|
|
/** |
46
|
|
|
* This will disable only showing stack traces on error conditions. |
47
|
|
|
*/ |
48
|
1 |
|
if (function_exists('xdebug_disable')) { |
49
|
1 |
|
xdebug_disable(); |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
$output->writeln('<error>It is highly recommended to disable the XDebug extension before invoking this command.</error>'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** @var Application $application */ |
56
|
1 |
|
$application = $this->getApplication(); |
57
|
1 |
|
$application->compiler = new Compiler(); |
58
|
|
|
|
59
|
1 |
|
$configFile = $input->getOption('config-file') ?: '.phpsa.yml'; |
60
|
1 |
|
$configDir = realpath($input->getArgument('path')); |
61
|
1 |
|
$application->configuration = $this->loadConfiguration($configFile, $configDir); |
62
|
|
|
|
63
|
1 |
|
$parser = $this->createParser($application); |
|
|
|
|
64
|
|
|
|
65
|
1 |
|
$output->writeln('Used config file: ' . $application->configuration->getPath()); |
66
|
|
|
|
67
|
1 |
|
$em = EventManager::getInstance(); |
|
|
|
|
68
|
1 |
|
$context = new Context($output, $application, $em); |
69
|
|
|
|
70
|
1 |
|
$fileParser = new FileParser( |
71
|
1 |
|
$parser, |
72
|
1 |
|
$application->compiler |
73
|
|
|
); |
74
|
|
|
|
75
|
1 |
|
$path = $input->getArgument('path'); |
76
|
1 |
|
if (is_dir($path)) { |
77
|
1 |
|
$directoryIterator = new RecursiveIteratorIterator( |
78
|
1 |
|
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS) |
79
|
|
|
); |
80
|
1 |
|
$output->writeln('Scanning directory <info>' . $path . '</info>'); |
81
|
|
|
|
82
|
1 |
|
$count = 0; |
83
|
|
|
|
84
|
1 |
|
$ignore = $application->configuration->getValue('ignore'); |
85
|
|
|
/** @var SplFileInfo $file */ |
86
|
1 |
|
foreach ($directoryIterator as $file) { |
87
|
1 |
|
$skip = 0; |
88
|
1 |
|
foreach ($ignore as $item) { |
89
|
1 |
|
$item = preg_replace('#/+#', '/', ($path . $item)); |
90
|
|
|
|
91
|
1 |
|
if (preg_match("#$item#", $file->getPathname())) { |
92
|
|
|
$skip = 1; |
93
|
1 |
|
break; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
if ($file->getExtension() !== 'php' || $skip) { |
98
|
|
|
continue; |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
$context->debug($file->getPathname()); |
102
|
1 |
|
$count++; |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
$output->writeln("Found <info>{$count} files</info>"); |
106
|
|
|
|
107
|
1 |
|
if ($count > 100) { |
108
|
|
|
$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>'); |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
$output->writeln(''); |
112
|
|
|
|
113
|
|
|
/** @var SplFileInfo $file */ |
114
|
1 |
|
foreach ($directoryIterator as $file) { |
115
|
1 |
|
$skip = 0; |
116
|
1 |
|
foreach ($ignore as $item) { |
117
|
1 |
|
$item = preg_replace('#/+#', '/', ($path . $item)); |
118
|
|
|
|
119
|
1 |
|
if (preg_match("#$item#", $file->getPathname())) { |
120
|
|
|
$skip = 1; |
121
|
1 |
|
break; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
if ($file->getExtension() !== 'php' || $skip) { |
126
|
|
|
continue; |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
$fileParser->parserFile($file->getPathname(), $context); |
130
|
|
|
} |
131
|
|
|
} elseif (is_file($path)) { |
132
|
|
|
$fileParser->parserFile($path, $context); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Step 2 Recursive check ... |
138
|
|
|
*/ |
139
|
1 |
|
$application->compiler->compile($context); |
140
|
|
|
|
141
|
1 |
|
$output->writeln(''); |
142
|
1 |
|
$output->writeln('Memory usage: ' . $this->getMemoryUsage(false) . ' (peak: ' . $this->getMemoryUsage(true) . ') MB'); |
143
|
1 |
|
} |
144
|
|
|
} |
145
|
|
|
|
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.