Skip to content

Commit

Permalink
PHPStanDiagnoseExtension - show Composer packages with included confi…
Browse files Browse the repository at this point in the history
…g files
  • Loading branch information
ondrejmirtes committed Aug 19, 2024
1 parent f618124 commit 58d202f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2137,6 +2137,7 @@ services:
class: PHPStan\Diagnose\PHPStanDiagnoseExtension
arguments:
composerAutoloaderProjectPaths: %composerAutoloaderProjectPaths%
allConfigFiles: %allConfigFiles%
autowired: false

# Error formatters
Expand Down
56 changes: 56 additions & 0 deletions src/Diagnose/PHPStanDiagnoseExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,35 @@
use Phar;
use PHPStan\Command\Output;
use PHPStan\ExtensionInstaller\GeneratedConfig;
use PHPStan\File\FileHelper;
use PHPStan\Internal\ComposerHelper;
use PHPStan\Php\PhpVersion;
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 is_file;
use function sprintf;
use function str_starts_with;
use function strlen;
use function substr;
use const PHP_VERSION_ID;

class PHPStanDiagnoseExtension implements DiagnoseExtension
{

/**
* @param string[] $composerAutoloaderProjectPaths
* @param string [] $allConfigFiles
*/
public function __construct(
private PhpVersion $phpVersion,
private FileHelper $fileHelper,
private array $composerAutoloaderProjectPaths,
private array $allConfigFiles,
)
{
}
Expand Down Expand Up @@ -71,6 +82,51 @@ public function print(Output $output): void
}
$output->writeLineFormatted('');

$thirdPartyIncludedConfigs = [];
foreach ($this->allConfigFiles as $configFile) {
$configFile = $this->fileHelper->normalizePath($configFile, '/');
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>',
Expand Down

0 comments on commit 58d202f

Please sign in to comment.