From 6c4477c9fcc2d62579cb1e353ca902b4b9ebb888 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Mon, 19 Aug 2024 22:10:53 +0200 Subject: [PATCH] PHPStanDiagnoseExtension - skip showing config files in "Included configs from Composer packages" if already present in the "Extension installer" section --- phpstan-baseline.neon | 15 ++++++++++++++ src/Diagnose/PHPStanDiagnoseExtension.php | 25 +++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index f7eb188d4..787b39901 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -159,6 +159,21 @@ parameters: count: 1 path: src/DependencyInjection/NeonAdapter.php + - + message: "#^Creating new ReflectionClass is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine\\. Use objects retrieved from ReflectionProvider instead\\.$#" + count: 1 + path: src/Diagnose/PHPStanDiagnoseExtension.php + + - + message: "#^Parameter \\#1 \\$objectOrClass of class ReflectionClass constructor expects class\\-string\\\\|PHPStan\\\\ExtensionInstaller\\\\GeneratedConfig, string given\\.$#" + count: 1 + path: src/Diagnose/PHPStanDiagnoseExtension.php + + - + message: "#^Parameter \\#1 \\$path of function dirname expects string, string\\|false given\\.$#" + count: 1 + path: src/Diagnose/PHPStanDiagnoseExtension.php + - message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#" count: 1 diff --git a/src/Diagnose/PHPStanDiagnoseExtension.php b/src/Diagnose/PHPStanDiagnoseExtension.php index 8a0834d27..b5ace2572 100644 --- a/src/Diagnose/PHPStanDiagnoseExtension.php +++ b/src/Diagnose/PHPStanDiagnoseExtension.php @@ -8,6 +8,7 @@ use PHPStan\File\FileHelper; use PHPStan\Internal\ComposerHelper; use PHPStan\Php\PhpVersion; +use ReflectionClass; use function array_key_exists; use function array_slice; use function class_exists; @@ -15,7 +16,9 @@ use function dirname; use function explode; use function implode; +use function in_array; use function is_file; +use function is_readable; use function sprintf; use function str_starts_with; use function strlen; @@ -69,13 +72,32 @@ public function print(Output $output): void } $output->writeLineFormatted(''); + $configFilesFromExtensionInstaller = []; if (class_exists('PHPStan\ExtensionInstaller\GeneratedConfig')) { $output->writeLineFormatted('Extension installer:'); 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('Extension installer: Not installed'); @@ -85,6 +107,9 @@ public function print(Output $output): void $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) {