diff --git a/src/GeneratedConfig.php b/src/GeneratedConfig.php index 3c9004a..b7c7a6a 100644 --- a/src/GeneratedConfig.php +++ b/src/GeneratedConfig.php @@ -19,6 +19,9 @@ final class GeneratedConfig public const NOT_INSTALLED = []; + /** @var string|null */ + public const PHPSTAN_VERSION_CONSTRAINT = null; + private function __construct() { } diff --git a/src/Plugin.php b/src/Plugin.php index b8e605b..ddebb1a 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -8,8 +8,12 @@ use Composer\Plugin\PluginInterface; use Composer\Script\Event; use Composer\Script\ScriptEvents; +use Composer\Semver\Constraint\MultiConstraint; +use Composer\Semver\Intervals; use Composer\Util\Filesystem; +use function array_key_exists; use function array_keys; +use function class_exists; use function dirname; use function file_exists; use function file_put_contents; @@ -45,6 +49,9 @@ final class GeneratedConfig public const NOT_INSTALLED = %s; + /** @var string|null */ + public const PHPSTAN_VERSION_CONSTRAINT = %s; + private function __construct() { } @@ -110,6 +117,8 @@ public function process(Event $event): void $ignore = $packageExtra['phpstan/extension-installer']['ignore']; } + $phpstanVersionConstraints = []; + foreach ($composer->getRepositoryManager()->getLocalRepository()->getPackages() as $package) { if ( $package->getType() !== 'phpstan-extension' @@ -151,6 +160,28 @@ public function process(Event $event): void ]; $installedPackages[$package->getName()] = true; + + $packageRequires = $package->getRequires(); + if (array_key_exists('phpstan/phpstan', $packageRequires)) { + $phpstanVersionConstraints[] = $packageRequires['phpstan/phpstan']->getConstraint(); + } + } + + $phpstanVersionConstraint = null; + if (count($phpstanVersionConstraints) > 0 && class_exists(Intervals::class)) { + if (count($phpstanVersionConstraints) === 1) { + $multiConstraint = $phpstanVersionConstraints[0]; + } else { + $multiConstraint = new MultiConstraint($phpstanVersionConstraints); + } + $compactedConstraint = Intervals::compactConstraint($multiConstraint); + $phpstanVersionConstraint = sprintf( + '%s%s && %s%s', + $compactedConstraint->getLowerBound()->isInclusive() ? '>=' : '>', + $compactedConstraint->getLowerBound()->getVersion(), + $compactedConstraint->getUpperBound()->isInclusive() ? '<=' : '<', + $compactedConstraint->getUpperBound()->getVersion() + ); } ksort($data); @@ -158,7 +189,7 @@ public function process(Event $event): void ksort($notInstalledPackages); sort($ignoredPackages); - $generatedConfigFileContents = sprintf(self::$generatedFileTemplate, var_export($data, true), var_export($notInstalledPackages, true)); + $generatedConfigFileContents = sprintf(self::$generatedFileTemplate, var_export($data, true), var_export($notInstalledPackages, true), var_export($phpstanVersionConstraint, true)); file_put_contents($generatedConfigFilePath, $generatedConfigFileContents); $io->write('phpstan/extension-installer: Extensions installed');