Skip to content

Commit 58d202f

Browse files
committed
PHPStanDiagnoseExtension - show Composer packages with included config files
1 parent f618124 commit 58d202f

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

conf/config.neon

+1
Original file line numberDiff line numberDiff line change
@@ -2137,6 +2137,7 @@ services:
21372137
class: PHPStan\Diagnose\PHPStanDiagnoseExtension
21382138
arguments:
21392139
composerAutoloaderProjectPaths: %composerAutoloaderProjectPaths%
2140+
allConfigFiles: %allConfigFiles%
21402141
autowired: false
21412142

21422143
# Error formatters

src/Diagnose/PHPStanDiagnoseExtension.php

+56
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,35 @@
55
use Phar;
66
use PHPStan\Command\Output;
77
use PHPStan\ExtensionInstaller\GeneratedConfig;
8+
use PHPStan\File\FileHelper;
89
use PHPStan\Internal\ComposerHelper;
910
use PHPStan\Php\PhpVersion;
11+
use function array_key_exists;
12+
use function array_slice;
1013
use function class_exists;
1114
use function count;
1215
use function dirname;
16+
use function explode;
17+
use function implode;
1318
use function is_file;
1419
use function sprintf;
20+
use function str_starts_with;
21+
use function strlen;
22+
use function substr;
1523
use const PHP_VERSION_ID;
1624

1725
class PHPStanDiagnoseExtension implements DiagnoseExtension
1826
{
1927

2028
/**
2129
* @param string[] $composerAutoloaderProjectPaths
30+
* @param string [] $allConfigFiles
2231
*/
2332
public function __construct(
2433
private PhpVersion $phpVersion,
34+
private FileHelper $fileHelper,
2535
private array $composerAutoloaderProjectPaths,
36+
private array $allConfigFiles,
2637
)
2738
{
2839
}
@@ -71,6 +82,51 @@ public function print(Output $output): void
7182
}
7283
$output->writeLineFormatted('');
7384

85+
$thirdPartyIncludedConfigs = [];
86+
foreach ($this->allConfigFiles as $configFile) {
87+
$configFile = $this->fileHelper->normalizePath($configFile, '/');
88+
foreach ($this->composerAutoloaderProjectPaths as $composerAutoloaderProjectPath) {
89+
$composerConfig = ComposerHelper::getComposerConfig($composerAutoloaderProjectPath);
90+
if ($composerConfig === null) {
91+
continue;
92+
}
93+
$vendorDir = $this->fileHelper->normalizePath(ComposerHelper::getVendorDirFromComposerConfig($composerAutoloaderProjectPath, $composerConfig), '/');
94+
if (!str_starts_with($configFile, $vendorDir)) {
95+
continue;
96+
}
97+
98+
$installedPath = $vendorDir . '/composer/installed.php';
99+
if (!is_file($installedPath)) {
100+
continue;
101+
}
102+
103+
$installed = require $installedPath;
104+
105+
$trimmed = substr($configFile, strlen($vendorDir) + 1);
106+
$parts = explode('/', $trimmed);
107+
$package = implode('/', array_slice($parts, 0, 2));
108+
$configPath = implode('/', array_slice($parts, 2));
109+
if (!array_key_exists($package, $installed['versions'])) {
110+
continue;
111+
}
112+
113+
$packageVersion = $installed['versions'][$package]['pretty_version'] ?? null;
114+
if ($packageVersion === null) {
115+
continue;
116+
}
117+
118+
$thirdPartyIncludedConfigs[] = [$package, $packageVersion, $configPath];
119+
}
120+
}
121+
122+
if (count($thirdPartyIncludedConfigs) > 0) {
123+
$output->writeLineFormatted('<info>Included configs from Composer packages:</info>');
124+
foreach ($thirdPartyIncludedConfigs as [$package, $packageVersion, $configPath]) {
125+
$output->writeLineFormatted(sprintf('%s (%s): %s', $package, $configPath, $packageVersion));
126+
}
127+
$output->writeLineFormatted('');
128+
}
129+
74130
$composerAutoloaderProjectPathsCount = count($this->composerAutoloaderProjectPaths);
75131
$output->writeLineFormatted(sprintf(
76132
'<info>Discovered Composer project %s:</info>',

0 commit comments

Comments
 (0)