-
Notifications
You must be signed in to change notification settings - Fork 574
Expand file tree
/
Copy pathPHPStanDiagnoseExtension.php
More file actions
227 lines (202 loc) · 7.66 KB
/
PHPStanDiagnoseExtension.php
File metadata and controls
227 lines (202 loc) · 7.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php declare(strict_types = 1);
namespace PHPStan\Diagnose;
use Phar;
use PHPStan\Analyser\ProcessedFilesCollector;
use PHPStan\Command\Output;
use PHPStan\ExtensionInstaller\GeneratedConfig;
use PHPStan\File\FileHelper;
use PHPStan\File\RelativePathHelper;
use PHPStan\Internal\ComposerHelper;
use PHPStan\Php\ComposerPhpVersionFactory;
use PHPStan\Php\PhpVersion;
use ReflectionClass;
use function array_key_exists;
use function array_slice;
use function class_exists;
use function count;
use function dirname;
use function explode;
use function implode;
use function in_array;
use function is_array;
use function is_file;
use function is_readable;
use function sprintf;
use function str_starts_with;
use function strlen;
use function substr;
use const PHP_VERSION_ID;
final class PHPStanDiagnoseExtension implements DiagnoseExtension
{
/**
* @param int|array{min: int, max: int}|null $configPhpVersion
* @param string[] $composerAutoloaderProjectPaths
* @param string [] $allConfigFiles
*/
public function __construct(
private PhpVersion $phpVersion,
private int|array|null $configPhpVersion,
private FileHelper $fileHelper,
private array $composerAutoloaderProjectPaths,
private array $allConfigFiles,
private ComposerPhpVersionFactory $composerPhpVersionFactory,
private ProcessedFilesCollector $processedFilesCollector,
private RelativePathHelper $simpleRelativePathHelper,
)
{
}
public function print(Output $output): void
{
$phpRuntimeVersion = new PhpVersion(PHP_VERSION_ID);
$output->writeLineFormatted(sprintf(
'<info>PHP runtime version:</info> %s',
$phpRuntimeVersion->getVersionString(),
));
if (
$this->phpVersion->getSource() === PhpVersion::SOURCE_CONFIG
&& is_array($this->configPhpVersion)
) {
$minVersion = new PhpVersion($this->configPhpVersion['min']);
$maxVersion = new PhpVersion($this->configPhpVersion['max']);
$output->writeLineFormatted(sprintf(
'<info>PHP version for analysis:</info> %s-%s (from %s)',
$minVersion->getVersionString(),
$maxVersion->getVersionString(),
$this->phpVersion->getSourceLabel(),
));
} else {
$minComposerPhpVersion = $this->composerPhpVersionFactory->getMinVersion();
$maxComposerPhpVersion = $this->composerPhpVersionFactory->getMaxVersion();
if ($minComposerPhpVersion !== null && $maxComposerPhpVersion !== null) {
if ($minComposerPhpVersion->getVersionId() !== $maxComposerPhpVersion->getVersionId()) {
$output->writeLineFormatted(sprintf(
'<info>PHP composer.json required version:</info> %s-%s',
$minComposerPhpVersion->getVersionString(),
$maxComposerPhpVersion->getVersionString(),
));
} else {
$output->writeLineFormatted(sprintf(
'<info>PHP composer.json required version:</info> %s',
$minComposerPhpVersion->getVersionString(),
));
}
}
$output->writeLineFormatted(sprintf(
'<info>PHP version for analysis:</info> %s (from %s)',
$this->phpVersion->getVersionString(),
$this->phpVersion->getSourceLabel(),
));
}
$output->writeLineFormatted('');
$output->writeLineFormatted(sprintf(
'<info>PHPStan version:</info> %s',
ComposerHelper::getPhpStanVersion(),
));
$output->writeLineFormatted('<info>PHPStan running from:</info>');
$pharRunning = Phar::running(false);
if ($pharRunning !== '') {
$output->writeLineFormatted(dirname($pharRunning));
} else {
if (isset($_SERVER['argv'][0]) && is_file($_SERVER['argv'][0])) {
$output->writeLineFormatted($_SERVER['argv'][0]);
} else {
$output->writeLineFormatted('Unknown');
}
}
$output->writeLineFormatted('');
$configFilesFromExtensionInstaller = [];
if (class_exists('PHPStan\ExtensionInstaller\GeneratedConfig')) {
$output->writeLineFormatted('<info>Extension installer:</info>');
if (count(GeneratedConfig::EXTENSIONS) === 0) {
$output->writeLineFormatted('No extensions installed');
}
$generatedConfigReflection = new ReflectionClass('PHPStan\ExtensionInstaller\GeneratedConfig');
$generatedConfigDirectory = dirname($generatedConfigReflection->getFileName());
foreach (GeneratedConfig::EXTENSIONS as $name => $extensionConfig) {
$output->writeLineFormatted(sprintf('%s: %s', $name, $extensionConfig['version'] ?? 'Unknown version'));
foreach ($extensionConfig['extra']['includes'] ?? [] as $includedFile) {
$includedFilePath = null;
if (isset($extensionConfig['relative_install_path'])) {
$includedFilePath = sprintf('%s/%s/%s', $generatedConfigDirectory, $extensionConfig['relative_install_path'], $includedFile);
if (!is_file($includedFilePath) || !is_readable($includedFilePath)) {
$includedFilePath = null;
}
}
if ($includedFilePath === null) {
$includedFilePath = sprintf('%s/%s', $extensionConfig['install_path'], $includedFile);
}
$configFilesFromExtensionInstaller[] = $this->fileHelper->normalizePath($includedFilePath, '/');
}
}
} else {
$output->writeLineFormatted('<info>Extension installer:</info> Not installed');
}
$output->writeLineFormatted('');
$thirdPartyIncludedConfigs = [];
foreach ($this->allConfigFiles as $configFile) {
$configFile = $this->fileHelper->normalizePath($configFile, '/');
if (in_array($configFile, $configFilesFromExtensionInstaller, true)) {
continue;
}
foreach ($this->composerAutoloaderProjectPaths as $composerAutoloaderProjectPath) {
$composerConfig = ComposerHelper::getComposerConfig($composerAutoloaderProjectPath);
if ($composerConfig === null) {
continue;
}
$vendorDir = $this->fileHelper->normalizePath(ComposerHelper::getVendorDirFromComposerConfig($composerAutoloaderProjectPath, $composerConfig), '/');
if (!str_starts_with($configFile, $vendorDir)) {
continue;
}
$installedPath = $vendorDir . '/composer/installed.php';
if (!is_file($installedPath)) {
continue;
}
$installed = require $installedPath;
$trimmed = substr($configFile, strlen($vendorDir) + 1);
$parts = explode('/', $trimmed);
$package = implode('/', array_slice($parts, 0, 2));
$configPath = implode('/', array_slice($parts, 2));
if (!array_key_exists($package, $installed['versions'])) {
continue;
}
$packageVersion = $installed['versions'][$package]['pretty_version'] ?? null;
if ($packageVersion === null) {
continue;
}
$thirdPartyIncludedConfigs[] = [$package, $packageVersion, $configPath];
}
}
if (count($thirdPartyIncludedConfigs) > 0) {
$output->writeLineFormatted('<info>Included configs from Composer packages:</info>');
foreach ($thirdPartyIncludedConfigs as [$package, $packageVersion, $configPath]) {
$output->writeLineFormatted(sprintf('%s (%s): %s', $package, $configPath, $packageVersion));
}
$output->writeLineFormatted('');
}
$composerAutoloaderProjectPathsCount = count($this->composerAutoloaderProjectPaths);
$output->writeLineFormatted(sprintf(
'<info>Discovered Composer project %s:</info>',
$composerAutoloaderProjectPathsCount === 1 ? 'root' : 'roots',
));
if ($composerAutoloaderProjectPathsCount === 0) {
$output->writeLineFormatted('None');
}
foreach ($this->composerAutoloaderProjectPaths as $composerAutoloaderProjectPath) {
$output->writeLineFormatted($composerAutoloaderProjectPath);
}
$output->writeLineFormatted('');
$topFiles = $this->processedFilesCollector->getTopMostAnalysedFiles(5);
if (count($topFiles) <= 0) {
return;
}
$output->writeLineFormatted('<info>Most often analysed files:</info>');
foreach ($topFiles as $file => $count) {
$output->writeLineFormatted(sprintf(
' %s: %d times',
$this->simpleRelativePathHelper->getRelativePath($file),
$count,
));
}
$output->writeLineFormatted('');
}
}