|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Patsura Dmitry https://github.com/ovr <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace PHPSA\Command; |
|
7
|
|
|
|
|
8
|
|
|
use PhpParser\NodeTraverser; |
|
9
|
|
|
use PhpParser\ParserFactory; |
|
10
|
|
|
use PHPSA\AliasManager; |
|
11
|
|
|
use PHPSA\Analyzer\EventListener\ExpressionListener; |
|
12
|
|
|
use PHPSA\Application; |
|
13
|
|
|
use PHPSA\Compiler; |
|
14
|
|
|
use PHPSA\Context; |
|
15
|
|
|
use PHPSA\Definition\ClassDefinition; |
|
16
|
|
|
use PHPSA\Definition\ClassMethod; |
|
17
|
|
|
use PHPSA\Definition\FunctionDefinition; |
|
18
|
|
|
use RecursiveDirectoryIterator; |
|
19
|
|
|
use RecursiveIteratorIterator; |
|
20
|
|
|
use RuntimeException; |
|
21
|
|
|
use SplFileInfo; |
|
22
|
|
|
use Exception; |
|
23
|
|
|
use FilesystemIterator; |
|
24
|
|
|
use PhpParser\Node; |
|
25
|
|
|
use PhpParser\Parser; |
|
26
|
|
|
use Symfony\Component\Console\Command\Command; |
|
27
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
28
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
29
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
30
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
31
|
|
|
use Webiny\Component\EventManager\EventManager; |
|
32
|
|
|
use PHPSA\Analyzer\Pass as AnalyzerPass; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Class CheckCommand |
|
36
|
|
|
* @package PHPSA\Command |
|
37
|
|
|
* |
|
38
|
|
|
* @method Application getApplication(); |
|
39
|
|
|
*/ |
|
40
|
|
|
class CheckCommand extends Command |
|
41
|
|
|
{ |
|
42
|
1 |
|
protected function configure() |
|
43
|
|
|
{ |
|
44
|
1 |
|
$this |
|
45
|
1 |
|
->setName('check') |
|
46
|
1 |
|
->setDescription('SPA') |
|
47
|
1 |
|
->addOption('blame', null, InputOption::VALUE_OPTIONAL, 'Git blame author for bad code ;)', false) |
|
48
|
1 |
|
->addArgument('path', InputArgument::OPTIONAL, 'Path to check file or directory', '.') |
|
49
|
1 |
|
->addOption( |
|
50
|
1 |
|
'report-json', |
|
51
|
1 |
|
null, |
|
52
|
1 |
|
InputOption::VALUE_REQUIRED, |
|
53
|
|
|
'Path to save detailed report in JSON format. Example: /tmp/report.json' |
|
54
|
1 |
|
); |
|
55
|
1 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
58
|
|
|
{ |
|
59
|
|
|
$output->writeln(''); |
|
60
|
|
|
|
|
61
|
|
|
if (extension_loaded('xdebug')) { |
|
62
|
|
|
$output->writeln('<error>It is highly recommended to disable the XDebug extension before invoking this command.</error>'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7, new \PhpParser\Lexer\Emulative( |
|
66
|
|
|
array( |
|
67
|
|
|
'usedAttributes' => array( |
|
68
|
|
|
'comments', |
|
69
|
|
|
'startLine', |
|
70
|
|
|
'endLine', |
|
71
|
|
|
'startTokenPos', |
|
72
|
|
|
'endTokenPos' |
|
73
|
|
|
) |
|
74
|
|
|
) |
|
75
|
|
|
)); |
|
76
|
|
|
|
|
77
|
|
|
/** @var Application $application */ |
|
78
|
|
|
$application = $this->getApplication(); |
|
79
|
|
|
$application->compiler = new Compiler(); |
|
80
|
|
|
|
|
81
|
|
|
$em = EventManager::getInstance(); |
|
|
|
|
|
|
82
|
|
|
$em->listen(Compiler\Event\ExpressionBeforeCompile::EVENT_NAME) |
|
83
|
|
|
->handler( |
|
84
|
|
|
new ExpressionListener( |
|
85
|
|
|
[ |
|
86
|
|
|
new AnalyzerPass\FunctionCall\AliasCheck(), |
|
87
|
|
|
new AnalyzerPass\FunctionCall\ArrayShortDefinition(), |
|
88
|
|
|
new AnalyzerPass\FunctionCall\DebugCode(), |
|
89
|
|
|
new AnalyzerPass\FunctionCall\RandomApiMigration(), |
|
90
|
|
|
new AnalyzerPass\FunctionCall\UseCast(), |
|
91
|
|
|
] |
|
92
|
|
|
) |
|
93
|
|
|
) |
|
94
|
|
|
->method('beforeCompile'); |
|
95
|
|
|
|
|
96
|
|
|
$context = new Context($output, $application, $em); |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Store option's in application's configuration |
|
100
|
|
|
*/ |
|
101
|
|
|
$application->getConfiguration()->setValue('blame', $input->getOption('blame')); |
|
102
|
|
|
|
|
103
|
|
|
$astTraverser = new \PhpParser\NodeTraverser(); |
|
104
|
|
|
$astTraverser->addVisitor(new \PhpParser\NodeVisitor\NameResolver); |
|
105
|
|
|
|
|
106
|
|
|
$path = $input->getArgument('path'); |
|
107
|
|
|
if (is_dir($path)) { |
|
108
|
|
|
$directoryIterator = new RecursiveIteratorIterator( |
|
109
|
|
|
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS) |
|
110
|
|
|
); |
|
111
|
|
|
$output->writeln('Scanning directory <info>' . $path . '</info>'); |
|
112
|
|
|
|
|
113
|
|
|
$count = 0; |
|
114
|
|
|
|
|
115
|
|
|
/** @var SplFileInfo $file */ |
|
116
|
|
|
foreach ($directoryIterator as $file) { |
|
117
|
|
|
if ($file->getExtension() != 'php') { |
|
118
|
|
|
continue; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$context->debug($file->getPathname()); |
|
122
|
|
|
$count++; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$output->writeln("Found <info>{$count} files</info>"); |
|
126
|
|
|
|
|
127
|
|
|
if ($count > 100) { |
|
128
|
|
|
$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>'); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$output->writeln(''); |
|
132
|
|
|
|
|
133
|
|
|
/** @var SplFileInfo $file */ |
|
134
|
|
|
foreach ($directoryIterator as $file) { |
|
135
|
|
|
if ($file->getExtension() != 'php') { |
|
136
|
|
|
continue; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$this->parserFile($file->getPathname(), $parser, $astTraverser, $context); |
|
140
|
|
|
} |
|
141
|
|
|
} elseif (is_file($path)) { |
|
142
|
|
|
$this->parserFile($path, $parser, $astTraverser, $context); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Step 2 Recursive check ... |
|
148
|
|
|
*/ |
|
149
|
|
|
$application->compiler->compile($context); |
|
150
|
|
|
|
|
151
|
|
|
$jsonReport = $input->getOption('report-json'); |
|
152
|
|
|
if ($jsonReport) { |
|
153
|
|
|
file_put_contents( |
|
154
|
|
|
$jsonReport, |
|
155
|
|
|
json_encode( |
|
156
|
|
|
$this->getApplication()->getIssuesCollector()->getIssues() |
|
157
|
|
|
) |
|
158
|
|
|
); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$output->writeln(''); |
|
162
|
|
|
$output->writeln('Memory usage: ' . $this->getMemoryUsage(false) . ' (peak: ' . $this->getMemoryUsage(true) . ') MB'); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param boolean $type |
|
167
|
|
|
* @return float |
|
168
|
|
|
*/ |
|
169
|
|
|
protected function getMemoryUsage($type) |
|
170
|
|
|
{ |
|
171
|
|
|
return round(memory_get_usage($type) / 1024 / 1024, 2); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @return Compiler |
|
176
|
|
|
*/ |
|
177
|
|
|
protected function getCompiler() |
|
178
|
|
|
{ |
|
179
|
|
|
return $this->getApplication()->compiler; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param string $filepath |
|
184
|
|
|
* @param Parser $parser |
|
185
|
|
|
* @param NodeTraverser $nodeTraverser |
|
186
|
|
|
* @param Context $context |
|
187
|
|
|
*/ |
|
188
|
|
|
protected function parserFile($filepath, Parser $parser, NodeTraverser $nodeTraverser, Context $context) |
|
189
|
|
|
{ |
|
190
|
|
|
$context->setFilepath($filepath); |
|
191
|
|
|
|
|
192
|
|
|
try { |
|
193
|
|
|
if (!is_readable($filepath)) { |
|
194
|
|
|
throw new RuntimeException('File ' . $filepath . ' is not readable'); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
$context->debug('<comment>Precompile: ' . $filepath . '.</comment>'); |
|
198
|
|
|
|
|
199
|
|
|
$code = file_get_contents($filepath); |
|
200
|
|
|
$astTree = $parser->parse($code); |
|
201
|
|
|
|
|
202
|
|
|
$nodeTraverser->traverse($astTree); |
|
|
|
|
|
|
203
|
|
|
|
|
204
|
|
|
$context->aliasManager = new AliasManager(); |
|
205
|
|
|
$namespace = null; |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Step 1 Precompile |
|
209
|
|
|
*/ |
|
210
|
|
|
foreach ($astTree as $topStatement) { |
|
|
|
|
|
|
211
|
|
|
if ($topStatement instanceof Node\Stmt\Namespace_) { |
|
212
|
|
|
/** |
|
213
|
|
|
* Namespace block can be created without NS name |
|
214
|
|
|
*/ |
|
215
|
|
|
if ($topStatement->name) { |
|
216
|
|
|
$namespace = $topStatement->name->toString(); |
|
217
|
|
|
$context->aliasManager->setNamespace($namespace); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
if ($topStatement->stmts) { |
|
|
|
|
|
|
221
|
|
|
$this->parseTopDefinitions($topStatement->stmts, $context->aliasManager, $filepath); |
|
|
|
|
|
|
222
|
|
|
} |
|
223
|
|
|
} else { |
|
224
|
|
|
$this->parseTopDefinitions($topStatement, $context->aliasManager, $filepath); |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
$context->clear(); |
|
229
|
|
|
} catch (\PhpParser\Error $e) { |
|
230
|
|
|
$context->sytaxError($e, $filepath); |
|
231
|
|
|
} catch (Exception $e) { |
|
232
|
|
|
$context->output->writeln("<error>{$e->getMessage()}</error>"); |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @param Node\Stmt $topStatement |
|
238
|
|
|
* @param AliasManager $aliasManager |
|
239
|
|
|
* @param string $filepath |
|
240
|
|
|
*/ |
|
241
|
|
|
protected function parseTopDefinitions($topStatement, AliasManager $aliasManager, $filepath) |
|
242
|
|
|
{ |
|
243
|
|
|
foreach ($topStatement as $statement) { |
|
|
|
|
|
|
244
|
|
|
if ($statement instanceof Node\Stmt\Use_) { |
|
245
|
|
|
if (count($statement->uses) > 0) { |
|
246
|
|
|
foreach ($statement->uses as $use) { |
|
247
|
|
|
$aliasManager->add($use->name->parts); |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
} elseif ($statement instanceof Node\Stmt\Class_) { |
|
251
|
|
|
$definition = new ClassDefinition($statement->name, $statement->type); |
|
252
|
|
|
$definition->setFilepath($filepath); |
|
253
|
|
|
$definition->setNamespace($aliasManager->getNamespace()); |
|
254
|
|
|
|
|
255
|
|
|
if ($statement->extends) { |
|
256
|
|
|
$definition->setExtendsClass($statement->extends->toString()); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
if ($statement->implements) { |
|
260
|
|
|
foreach ($statement->implements as $interface) { |
|
261
|
|
|
$definition->addInterface($interface->toString()); |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
foreach ($statement->stmts as $stmt) { |
|
266
|
|
|
if ($stmt instanceof Node\Stmt\ClassMethod) { |
|
267
|
|
|
$method = new ClassMethod($stmt->name, $stmt, $stmt->type); |
|
268
|
|
|
|
|
269
|
|
|
$definition->addMethod($method); |
|
270
|
|
|
} elseif ($stmt instanceof Node\Stmt\Property) { |
|
271
|
|
|
$definition->addProperty($stmt); |
|
272
|
|
|
} elseif ($stmt instanceof Node\Stmt\ClassConst) { |
|
273
|
|
|
$definition->addConst($stmt); |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
$this->getCompiler()->addClass($definition); |
|
278
|
|
|
} elseif ($statement instanceof Node\Stmt\Function_) { |
|
279
|
|
|
$definition = new FunctionDefinition($statement->name, $statement); |
|
280
|
|
|
$definition->setFilepath($filepath); |
|
281
|
|
|
$definition->setNamespace($aliasManager->getNamespace()); |
|
282
|
|
|
|
|
283
|
|
|
$this->getCompiler()->addFunction($definition); |
|
284
|
|
|
} |
|
285
|
|
|
} |
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
|
|
|
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.