1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Patsura Dmitry https://github.com/ovr <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace PHPSA\Command; |
7
|
|
|
|
8
|
|
|
use PHPSA\Analyzer; |
9
|
|
|
use PHPSA\Application; |
10
|
|
|
use PHPSA\Compiler; |
11
|
|
|
use PHPSA\Context; |
12
|
|
|
use PHPSA\Definition\FileParser; |
13
|
|
|
use RecursiveDirectoryIterator; |
14
|
|
|
use RecursiveIteratorIterator; |
15
|
|
|
use SplFileInfo; |
16
|
|
|
use FilesystemIterator; |
17
|
|
|
use Symfony\Component\Config\FileLocator; |
18
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
20
|
|
|
use Symfony\Component\Console\Input\InputOption; |
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
22
|
|
|
use Webiny\Component\EventManager\EventManager; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Command to run compiler and analyzers on files |
26
|
|
|
* |
27
|
|
|
* @package PHPSA\Command |
28
|
|
|
* @method Application getApplication(); |
29
|
|
|
*/ |
30
|
|
|
class CheckCommand extends AbstractCommand |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
1 |
|
protected function configure() |
37
|
|
|
{ |
38
|
|
|
$this |
39
|
1 |
|
->setName('check') |
40
|
1 |
|
->setDescription('Runs compiler and analyzers on all files in path') |
41
|
1 |
|
->addOption('config-file', null, InputOption::VALUE_REQUIRED, 'Path to the configuration file.') |
42
|
1 |
|
->addArgument('path', InputArgument::OPTIONAL, 'Path to check file or directory', '.') |
43
|
1 |
|
->addOption( |
44
|
1 |
|
'report-json', |
45
|
1 |
|
null, |
46
|
1 |
|
InputOption::VALUE_REQUIRED, |
47
|
1 |
|
'Path to save detailed report in JSON format. Example: /tmp/report.json' |
48
|
|
|
); |
49
|
1 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$output->writeln(''); |
57
|
|
|
|
58
|
|
|
if (extension_loaded('xdebug')) { |
59
|
|
|
/** |
60
|
|
|
* This will disable only showing stack traces on error conditions. |
61
|
|
|
*/ |
62
|
|
|
if (function_exists('xdebug_disable')) { |
63
|
|
|
xdebug_disable(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$output->writeln('<error>It is highly recommended to disable the XDebug extension before invoking this command.</error>'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** @var Application $application */ |
70
|
|
|
$application = $this->getApplication(); |
71
|
|
|
$application->compiler = new Compiler(); |
72
|
|
|
|
73
|
|
|
$configFile = $input->getOption('config-file') ?: '.phpsa.yml'; |
74
|
|
|
$configDir = realpath($input->getArgument('path')); |
75
|
|
|
$application->configuration = $this->loadConfiguration($configFile, $configDir); |
76
|
|
|
|
77
|
|
|
$parser = $this->createParser($application); |
|
|
|
|
78
|
|
|
|
79
|
|
|
$output->writeln('Used config file: ' . $application->configuration->getPath()); |
80
|
|
|
|
81
|
|
|
$em = EventManager::getInstance(); |
|
|
|
|
82
|
|
|
Analyzer\Factory::factory($em, $application->configuration); |
83
|
|
|
$context = new Context($output, $application, $em); |
84
|
|
|
|
85
|
|
|
$fileParser = new FileParser( |
86
|
|
|
$parser, |
87
|
|
|
$application->compiler |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$path = $input->getArgument('path'); |
91
|
|
|
if (is_dir($path)) { |
92
|
|
|
$directoryIterator = new RecursiveIteratorIterator( |
93
|
|
|
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS) |
94
|
|
|
); |
95
|
|
|
$output->writeln('Scanning directory <info>' . $path . '</info>'); |
96
|
|
|
|
97
|
|
|
$count = 0; |
98
|
|
|
|
99
|
|
|
$ignore = $application->configuration->getValue('ignore'); |
100
|
|
|
/** @var SplFileInfo $file */ |
101
|
|
|
foreach ($directoryIterator as $file) { |
102
|
|
|
$skip = 0; |
103
|
|
|
foreach ($ignore as $item) { |
104
|
|
|
$item = preg_replace('#/+#', '/', ($path . $item)); |
105
|
|
|
|
106
|
|
|
if (preg_match("#$item#", $file->getPathname())) { |
107
|
|
|
$skip = 1; |
108
|
|
|
break; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if ($file->getExtension() !== 'php' || $skip) { |
113
|
|
|
continue; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$context->debug($file->getPathname()); |
117
|
|
|
$count++; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$output->writeln("Found <info>{$count} files</info>"); |
121
|
|
|
|
122
|
|
|
if ($count > 100) { |
123
|
|
|
$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>'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$output->writeln(''); |
127
|
|
|
|
128
|
|
|
/** @var SplFileInfo $file */ |
129
|
|
|
foreach ($directoryIterator as $file) { |
130
|
|
|
$skip = 0; |
131
|
|
|
foreach ($ignore as $item) { |
132
|
|
|
$item = preg_replace('#/+#', '/', ($path . $item)); |
133
|
|
|
|
134
|
|
|
if (preg_match("#$item#", $file->getPathname())) { |
135
|
|
|
$skip = 1; |
136
|
|
|
break; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if ($file->getExtension() !== 'php' || $skip) { |
141
|
|
|
continue; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$fileParser->parserFile($file->getPathname(), $context); |
145
|
|
|
} |
146
|
|
|
} elseif (is_file($path)) { |
147
|
|
|
$fileParser->parserFile($path, $context); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Step 2 Recursive check ... |
153
|
|
|
*/ |
154
|
|
|
$application->compiler->compile($context); |
155
|
|
|
|
156
|
|
|
$jsonReport = $input->getOption('report-json'); |
157
|
|
|
if ($jsonReport) { |
158
|
|
|
file_put_contents( |
159
|
|
|
$jsonReport, |
160
|
|
|
json_encode( |
161
|
|
|
$this->getApplication()->getIssuesCollector()->getIssues() |
162
|
|
|
) |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$output->writeln(''); |
167
|
|
|
$output->writeln('Memory usage: ' . $this->getMemoryUsage(false) . ' (peak: ' . $this->getMemoryUsage(true) . ') MB'); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
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.