|
5 | 5 | use Phar;
|
6 | 6 | use PHPStan\Command\Output;
|
7 | 7 | use PHPStan\ExtensionInstaller\GeneratedConfig;
|
| 8 | +use PHPStan\File\FileHelper; |
8 | 9 | use PHPStan\Internal\ComposerHelper;
|
9 | 10 | use PHPStan\Php\PhpVersion;
|
| 11 | +use function array_key_exists; |
| 12 | +use function array_slice; |
10 | 13 | use function class_exists;
|
11 | 14 | use function count;
|
12 | 15 | use function dirname;
|
| 16 | +use function explode; |
| 17 | +use function implode; |
13 | 18 | use function is_file;
|
14 | 19 | use function sprintf;
|
| 20 | +use function str_starts_with; |
| 21 | +use function strlen; |
| 22 | +use function substr; |
15 | 23 | use const PHP_VERSION_ID;
|
16 | 24 |
|
17 | 25 | class PHPStanDiagnoseExtension implements DiagnoseExtension
|
18 | 26 | {
|
19 | 27 |
|
20 | 28 | /**
|
21 | 29 | * @param string[] $composerAutoloaderProjectPaths
|
| 30 | + * @param string [] $allConfigFiles |
22 | 31 | */
|
23 | 32 | public function __construct(
|
24 | 33 | private PhpVersion $phpVersion,
|
| 34 | + private FileHelper $fileHelper, |
25 | 35 | private array $composerAutoloaderProjectPaths,
|
| 36 | + private array $allConfigFiles, |
26 | 37 | )
|
27 | 38 | {
|
28 | 39 | }
|
@@ -71,6 +82,51 @@ public function print(Output $output): void
|
71 | 82 | }
|
72 | 83 | $output->writeLineFormatted('');
|
73 | 84 |
|
| 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 | + |
74 | 130 | $composerAutoloaderProjectPathsCount = count($this->composerAutoloaderProjectPaths);
|
75 | 131 | $output->writeLineFormatted(sprintf(
|
76 | 132 | '<info>Discovered Composer project %s:</info>',
|
|
0 commit comments