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
|
|
|
Node\Expr\FuncCall::class => [ |
87
|
|
|
new AnalyzerPass\FunctionCall\AliasCheck(), |
88
|
|
|
new AnalyzerPass\FunctionCall\ArrayShortDefinition(), |
89
|
|
|
new AnalyzerPass\FunctionCall\DebugCode(), |
90
|
|
|
new AnalyzerPass\FunctionCall\RandomApiMigration(), |
91
|
|
|
new AnalyzerPass\FunctionCall\UseCast(), |
92
|
|
|
] |
93
|
|
|
] |
94
|
|
|
) |
95
|
|
|
) |
96
|
|
|
->method('beforeCompile'); |
97
|
|
|
|
98
|
|
|
$context = new Context($output, $application, $em); |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Store option's in application's configuration |
102
|
|
|
*/ |
103
|
|
|
$application->getConfiguration()->setValue('blame', $input->getOption('blame')); |
104
|
|
|
|
105
|
|
|
$astTraverser = new \PhpParser\NodeTraverser(); |
106
|
|
|
$astTraverser->addVisitor(new \PhpParser\NodeVisitor\NameResolver); |
107
|
|
|
|
108
|
|
|
$path = $input->getArgument('path'); |
109
|
|
|
if (is_dir($path)) { |
110
|
|
|
$directoryIterator = new RecursiveIteratorIterator( |
111
|
|
|
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS) |
112
|
|
|
); |
113
|
|
|
$output->writeln('Scanning directory <info>' . $path . '</info>'); |
114
|
|
|
|
115
|
|
|
$count = 0; |
116
|
|
|
|
117
|
|
|
/** @var SplFileInfo $file */ |
118
|
|
|
foreach ($directoryIterator as $file) { |
119
|
|
|
if ($file->getExtension() != 'php') { |
120
|
|
|
continue; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$context->debug($file->getPathname()); |
124
|
|
|
$count++; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$output->writeln("Found <info>{$count} files</info>"); |
128
|
|
|
|
129
|
|
|
if ($count > 100) { |
130
|
|
|
$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>'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$output->writeln(''); |
134
|
|
|
|
135
|
|
|
/** @var SplFileInfo $file */ |
136
|
|
|
foreach ($directoryIterator as $file) { |
137
|
|
|
if ($file->getExtension() != 'php') { |
138
|
|
|
continue; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$this->parserFile($file->getPathname(), $parser, $astTraverser, $context); |
142
|
|
|
} |
143
|
|
|
} elseif (is_file($path)) { |
144
|
|
|
$this->parserFile($path, $parser, $astTraverser, $context); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Step 2 Recursive check ... |
150
|
|
|
*/ |
151
|
|
|
$application->compiler->compile($context); |
152
|
|
|
|
153
|
|
|
$jsonReport = $input->getOption('report-json'); |
154
|
|
|
if ($jsonReport) { |
155
|
|
|
file_put_contents( |
156
|
|
|
$jsonReport, |
157
|
|
|
json_encode( |
158
|
|
|
$this->getApplication()->getIssuesCollector()->getIssues() |
159
|
|
|
) |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$output->writeln(''); |
164
|
|
|
$output->writeln('Memory usage: ' . $this->getMemoryUsage(false) . ' (peak: ' . $this->getMemoryUsage(true) . ') MB'); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param boolean $type |
169
|
|
|
* @return float |
170
|
|
|
*/ |
171
|
|
|
protected function getMemoryUsage($type) |
172
|
|
|
{ |
173
|
|
|
return round(memory_get_usage($type) / 1024 / 1024, 2); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return Compiler |
178
|
|
|
*/ |
179
|
|
|
protected function getCompiler() |
180
|
|
|
{ |
181
|
|
|
return $this->getApplication()->compiler; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param string $filepath |
186
|
|
|
* @param Parser $parser |
187
|
|
|
* @param NodeTraverser $nodeTraverser |
188
|
|
|
* @param Context $context |
189
|
|
|
*/ |
190
|
|
|
protected function parserFile($filepath, Parser $parser, NodeTraverser $nodeTraverser, Context $context) |
191
|
|
|
{ |
192
|
|
|
$context->setFilepath($filepath); |
193
|
|
|
|
194
|
|
|
try { |
195
|
|
|
if (!is_readable($filepath)) { |
196
|
|
|
throw new RuntimeException('File ' . $filepath . ' is not readable'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$context->debug('<comment>Precompile: ' . $filepath . '.</comment>'); |
200
|
|
|
|
201
|
|
|
$code = file_get_contents($filepath); |
202
|
|
|
$astTree = $parser->parse($code); |
203
|
|
|
|
204
|
|
|
$nodeTraverser->traverse($astTree); |
|
|
|
|
205
|
|
|
|
206
|
|
|
$context->aliasManager = new AliasManager(); |
207
|
|
|
$namespace = null; |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Step 1 Precompile |
211
|
|
|
*/ |
212
|
|
|
foreach ($astTree as $topStatement) { |
|
|
|
|
213
|
|
|
if ($topStatement instanceof Node\Stmt\Namespace_) { |
214
|
|
|
/** |
215
|
|
|
* Namespace block can be created without NS name |
216
|
|
|
*/ |
217
|
|
|
if ($topStatement->name) { |
218
|
|
|
$namespace = $topStatement->name->toString(); |
219
|
|
|
$context->aliasManager->setNamespace($namespace); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
if ($topStatement->stmts) { |
|
|
|
|
223
|
|
|
$this->parseTopDefinitions($topStatement->stmts, $context->aliasManager, $filepath); |
|
|
|
|
224
|
|
|
} |
225
|
|
|
} else { |
226
|
|
|
$this->parseTopDefinitions($topStatement, $context->aliasManager, $filepath); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
$context->clear(); |
231
|
|
|
} catch (\PhpParser\Error $e) { |
232
|
|
|
$context->sytaxError($e, $filepath); |
233
|
|
|
} catch (Exception $e) { |
234
|
|
|
$context->output->writeln("<error>{$e->getMessage()}</error>"); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param Node\Stmt $topStatement |
240
|
|
|
* @param AliasManager $aliasManager |
241
|
|
|
* @param string $filepath |
242
|
|
|
*/ |
243
|
|
|
protected function parseTopDefinitions($topStatement, AliasManager $aliasManager, $filepath) |
244
|
|
|
{ |
245
|
|
|
foreach ($topStatement as $statement) { |
|
|
|
|
246
|
|
|
if ($statement instanceof Node\Stmt\Use_) { |
247
|
|
|
if (count($statement->uses) > 0) { |
248
|
|
|
foreach ($statement->uses as $use) { |
249
|
|
|
$aliasManager->add($use->name->parts); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
} elseif ($statement instanceof Node\Stmt\Class_) { |
253
|
|
|
$definition = new ClassDefinition($statement->name, $statement->type); |
254
|
|
|
$definition->setFilepath($filepath); |
255
|
|
|
$definition->setNamespace($aliasManager->getNamespace()); |
256
|
|
|
|
257
|
|
|
if ($statement->extends) { |
258
|
|
|
$definition->setExtendsClass($statement->extends->toString()); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
if ($statement->implements) { |
262
|
|
|
foreach ($statement->implements as $interface) { |
263
|
|
|
$definition->addInterface($interface->toString()); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
foreach ($statement->stmts as $stmt) { |
268
|
|
|
if ($stmt instanceof Node\Stmt\ClassMethod) { |
269
|
|
|
$method = new ClassMethod($stmt->name, $stmt, $stmt->type); |
270
|
|
|
|
271
|
|
|
$definition->addMethod($method); |
272
|
|
|
} elseif ($stmt instanceof Node\Stmt\Property) { |
273
|
|
|
$definition->addProperty($stmt); |
274
|
|
|
} elseif ($stmt instanceof Node\Stmt\ClassConst) { |
275
|
|
|
$definition->addConst($stmt); |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
$this->getCompiler()->addClass($definition); |
280
|
|
|
} elseif ($statement instanceof Node\Stmt\Function_) { |
281
|
|
|
$definition = new FunctionDefinition($statement->name, $statement); |
282
|
|
|
$definition->setFilepath($filepath); |
283
|
|
|
$definition->setNamespace($aliasManager->getNamespace()); |
284
|
|
|
|
285
|
|
|
$this->getCompiler()->addFunction($definition); |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
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.