1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Patsura Dmitry https://github.com/ovr <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace PHPSA\Command; |
7
|
|
|
|
8
|
|
|
use PhpParser\ParserFactory; |
9
|
|
|
use PHPSA\Analyzer; |
10
|
|
|
use PHPSA\Application; |
11
|
|
|
use PHPSA\Compiler; |
12
|
|
|
use PHPSA\Configuration; |
13
|
|
|
use PHPSA\ConfigurationLoader; |
14
|
|
|
use PHPSA\Context; |
15
|
|
|
use PHPSA\Definition\FileParser; |
16
|
|
|
use RecursiveDirectoryIterator; |
17
|
|
|
use RecursiveIteratorIterator; |
18
|
|
|
use SplFileInfo; |
19
|
|
|
use FilesystemIterator; |
20
|
|
|
use Symfony\Component\Config\FileLocator; |
21
|
|
|
use Symfony\Component\Console\Command\Command; |
22
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
23
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
24
|
|
|
use Symfony\Component\Console\Input\InputOption; |
25
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
26
|
|
|
use Webiny\Component\EventManager\EventManager; |
27
|
|
|
use PHPSA\Analyzer\Pass as AnalyzerPass; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class CheckCommand |
31
|
|
|
* @package PHPSA\Command |
32
|
|
|
* |
33
|
|
|
* @method Application getApplication(); |
34
|
|
|
*/ |
35
|
|
|
class CheckCommand extends Command |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Configures the command. |
40
|
|
|
*/ |
41
|
747 |
|
protected function configure() |
42
|
|
|
{ |
43
|
747 |
|
$this |
44
|
747 |
|
->setName('check') |
45
|
747 |
|
->setDescription('SPA') |
46
|
747 |
|
->addOption('blame', null, InputOption::VALUE_NONE, 'Git blame author for bad code ;)') |
47
|
747 |
|
->addOption('config-file', null, InputOption::VALUE_REQUIRED, 'Path to the configuration file.') |
48
|
747 |
|
->addArgument('path', InputArgument::OPTIONAL, 'Path to check file or directory', '.') |
49
|
747 |
|
->addOption( |
50
|
747 |
|
'report-json', |
51
|
747 |
|
null, |
52
|
747 |
|
InputOption::VALUE_REQUIRED, |
53
|
|
|
'Path to save detailed report in JSON format. Example: /tmp/report.json' |
54
|
747 |
|
); |
55
|
747 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Executes the command. |
59
|
|
|
*/ |
60
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$output->writeln(''); |
63
|
|
|
|
64
|
|
|
if (extension_loaded('xdebug')) { |
65
|
|
|
/** |
66
|
|
|
* This will disable only showing stack traces on error conditions. |
67
|
|
|
*/ |
68
|
|
|
if (function_exists('xdebug_disable')) { |
69
|
|
|
xdebug_disable(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$output->writeln('<error>It is highly recommended to disable the XDebug extension before invoking this command.</error>'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7, new \PhpParser\Lexer\Emulative([ |
76
|
|
|
'usedAttributes' => [ |
77
|
|
|
'comments', |
78
|
|
|
'startLine', |
79
|
|
|
'endLine', |
80
|
|
|
'startTokenPos', |
81
|
|
|
'endTokenPos' |
82
|
|
|
] |
83
|
|
|
])); |
84
|
|
|
|
85
|
|
|
/** @var Application $application */ |
86
|
|
|
$application = $this->getApplication(); |
87
|
|
|
$application->compiler = new Compiler(); |
88
|
|
|
|
89
|
|
|
$configFile = $input->getOption('config-file') ?: '.phpsa.yml'; |
90
|
|
|
$configDir = realpath($input->getArgument('path')); |
91
|
|
|
$application->configuration = $this->loadConfiguration($configFile, $configDir); |
92
|
|
|
|
93
|
|
|
$em = EventManager::getInstance(); |
|
|
|
|
94
|
|
|
Analyzer\Factory::factory($em, $application->configuration); |
95
|
|
|
$context = new Context($output, $application, $em); |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Store option's in application's configuration |
99
|
|
|
*/ |
100
|
|
|
if ($input->getOption('blame')) { |
101
|
|
|
$application->configuration->setValue('blame', true); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$fileParser = new FileParser( |
105
|
|
|
$parser, |
106
|
|
|
$this->getCompiler() |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
$path = $input->getArgument('path'); |
110
|
|
|
if (is_dir($path)) { |
111
|
|
|
$directoryIterator = new RecursiveIteratorIterator( |
112
|
|
|
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS) |
113
|
|
|
); |
114
|
|
|
$output->writeln('Scanning directory <info>' . $path . '</info>'); |
115
|
|
|
|
116
|
|
|
$count = 0; |
117
|
|
|
|
118
|
|
|
/** @var SplFileInfo $file */ |
119
|
|
|
foreach ($directoryIterator as $file) { |
120
|
|
|
if ($file->getExtension() !== 'php') { |
121
|
|
|
continue; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$context->debug($file->getPathname()); |
125
|
|
|
$count++; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$output->writeln("Found <info>{$count} files</info>"); |
129
|
|
|
|
130
|
|
|
if ($count > 100) { |
131
|
|
|
$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>'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$output->writeln(''); |
135
|
|
|
|
136
|
|
|
/** @var SplFileInfo $file */ |
137
|
|
|
foreach ($directoryIterator as $file) { |
138
|
|
|
if ($file->getExtension() !== 'php') { |
139
|
|
|
continue; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$fileParser->parserFile($file->getPathname(), $context); |
143
|
|
|
} |
144
|
|
|
} elseif (is_file($path)) { |
145
|
|
|
$fileParser->parserFile($path, $context); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Step 2 Recursive check ... |
151
|
|
|
*/ |
152
|
|
|
$application->compiler->compile($context); |
153
|
|
|
|
154
|
|
|
$jsonReport = $input->getOption('report-json'); |
155
|
|
|
if ($jsonReport) { |
156
|
|
|
file_put_contents( |
157
|
|
|
$jsonReport, |
158
|
|
|
json_encode( |
159
|
|
|
$this->getApplication()->getIssuesCollector()->getIssues() |
160
|
|
|
) |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$output->writeln(''); |
165
|
|
|
$output->writeln('Memory usage: ' . $this->getMemoryUsage(false) . ' (peak: ' . $this->getMemoryUsage(true) . ') MB'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param boolean $type |
170
|
|
|
* @return float |
171
|
|
|
*/ |
172
|
|
|
protected function getMemoryUsage($type) |
173
|
|
|
{ |
174
|
|
|
return round(memory_get_usage($type) / 1024 / 1024, 2); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return Compiler |
179
|
|
|
*/ |
180
|
|
|
protected function getCompiler() |
181
|
|
|
{ |
182
|
|
|
return $this->getApplication()->compiler; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param string $configFile |
187
|
|
|
* @param string $configurationDirectory |
188
|
|
|
* |
189
|
|
|
* @return Configuration |
190
|
|
|
*/ |
191
|
|
|
protected function loadConfiguration($configFile, $configurationDirectory) |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
$loader = new ConfigurationLoader(new FileLocator([ |
194
|
|
|
getcwd(), |
195
|
|
|
$configurationDirectory |
196
|
|
|
])); |
197
|
|
|
|
198
|
|
|
return new Configuration( |
199
|
|
|
$loader->load($configFile), |
200
|
|
|
Analyzer\Factory::getPassesConfigurations() |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
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.