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
|
387 |
|
protected function configure() |
38
|
|
|
{ |
39
|
387 |
|
$this |
40
|
387 |
|
->setName('check') |
41
|
387 |
|
->setDescription('SPA') |
42
|
387 |
|
->addOption('blame', null, InputOption::VALUE_OPTIONAL, 'Git blame author for bad code ;)', -1) |
43
|
387 |
|
->addArgument('path', InputArgument::OPTIONAL, 'Path to check file or directory', '.') |
44
|
387 |
|
->addOption( |
45
|
387 |
|
'report-json', |
46
|
387 |
|
null, |
47
|
387 |
|
InputOption::VALUE_REQUIRED, |
48
|
|
|
'Path to save detailed report in JSON format. Example: /tmp/report.json' |
49
|
387 |
|
); |
50
|
387 |
|
} |
51
|
|
|
|
52
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$output->writeln(''); |
55
|
|
|
|
56
|
|
|
if (extension_loaded('xdebug')) { |
57
|
|
|
/** |
58
|
|
|
* This will disable only showing stack traces on error conditions. |
59
|
|
|
*/ |
60
|
|
|
if (function_exists('xdebug_disable')) { |
61
|
|
|
xdebug_disable(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$output->writeln('<error>It is highly recommended to disable the XDebug extension before invoking this command.</error>'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7, new \PhpParser\Lexer\Emulative([ |
68
|
|
|
'usedAttributes' => [ |
69
|
|
|
'comments', |
70
|
|
|
'startLine', |
71
|
|
|
'endLine', |
72
|
|
|
'startTokenPos', |
73
|
|
|
'endTokenPos' |
74
|
|
|
] |
75
|
|
|
])); |
76
|
|
|
|
77
|
|
|
/** @var Application $application */ |
78
|
|
|
$application = $this->getApplication(); |
79
|
|
|
$application->compiler = new Compiler(); |
80
|
|
|
|
81
|
|
|
$loader = new ConfigurationLoader(new FileLocator([ |
82
|
|
|
realpath($input->getArgument('path')) . DIRECTORY_SEPARATOR |
83
|
|
|
])); |
84
|
|
|
|
85
|
|
|
$application->configuration = new Configuration( |
86
|
|
|
$loader->load('.phpsa.yml') |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$em = EventManager::getInstance(); |
|
|
|
|
90
|
|
|
Analyzer\Factory::factory($em); |
91
|
|
|
|
92
|
|
|
$context = new Context($output, $application, $em); |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Store option's in application's configuration |
96
|
|
|
*/ |
97
|
|
|
$blame = $input->getOption('blame'); |
98
|
|
|
if ($blame === -1) { |
99
|
|
|
$application->configuration->setValue('blame', $blame); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$fileParser = new FileParser( |
103
|
|
|
$parser, |
104
|
|
|
$this->getCompiler() |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
$path = $input->getArgument('path'); |
108
|
|
|
if (is_dir($path)) { |
109
|
|
|
$directoryIterator = new RecursiveIteratorIterator( |
110
|
|
|
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS) |
111
|
|
|
); |
112
|
|
|
$output->writeln('Scanning directory <info>' . $path . '</info>'); |
113
|
|
|
|
114
|
|
|
$count = 0; |
115
|
|
|
|
116
|
|
|
/** @var SplFileInfo $file */ |
117
|
|
|
foreach ($directoryIterator as $file) { |
118
|
|
|
if ($file->getExtension() !== 'php') { |
119
|
|
|
continue; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$context->debug($file->getPathname()); |
123
|
|
|
$count++; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$output->writeln("Found <info>{$count} files</info>"); |
127
|
|
|
|
128
|
|
|
if ($count > 100) { |
129
|
|
|
$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>'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$output->writeln(''); |
133
|
|
|
|
134
|
|
|
/** @var SplFileInfo $file */ |
135
|
|
|
foreach ($directoryIterator as $file) { |
136
|
|
|
if ($file->getExtension() !== 'php') { |
137
|
|
|
continue; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$fileParser->parserFile($file->getPathname(), $context); |
141
|
|
|
} |
142
|
|
|
} elseif (is_file($path)) { |
143
|
|
|
$fileParser->parserFile($path, $context); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Step 2 Recursive check ... |
149
|
|
|
*/ |
150
|
|
|
$application->compiler->compile($context); |
151
|
|
|
|
152
|
|
|
$jsonReport = $input->getOption('report-json'); |
153
|
|
|
if ($jsonReport) { |
154
|
|
|
file_put_contents( |
155
|
|
|
$jsonReport, |
156
|
|
|
json_encode( |
157
|
|
|
$this->getApplication()->getIssuesCollector()->getIssues() |
158
|
|
|
) |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$output->writeln(''); |
163
|
|
|
$output->writeln('Memory usage: ' . $this->getMemoryUsage(false) . ' (peak: ' . $this->getMemoryUsage(true) . ') MB'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param boolean $type |
168
|
|
|
* @return float |
169
|
|
|
*/ |
170
|
|
|
protected function getMemoryUsage($type) |
171
|
|
|
{ |
172
|
|
|
return round(memory_get_usage($type) / 1024 / 1024, 2); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return Compiler |
177
|
|
|
*/ |
178
|
|
|
protected function getCompiler() |
179
|
|
|
{ |
180
|
|
|
return $this->getApplication()->compiler; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
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.