Skip to content

Commit 219172e

Browse files
committed
Dump PHPStan version constraint to GeneratedConfig
1 parent 9416e06 commit 219172e

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/GeneratedConfig.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ final class GeneratedConfig
1919

2020
public const NOT_INSTALLED = [];
2121

22+
/** @var string|null */
23+
public const PHPSTAN_VERSION_CONSTRAINT = null;
24+
2225
private function __construct()
2326
{
2427
}

src/Plugin.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
use Composer\Plugin\PluginInterface;
99
use Composer\Script\Event;
1010
use Composer\Script\ScriptEvents;
11+
use Composer\Semver\Constraint\MultiConstraint;
12+
use Composer\Semver\Intervals;
1113
use Composer\Util\Filesystem;
14+
use function array_key_exists;
1215
use function array_keys;
16+
use function class_exists;
1317
use function dirname;
1418
use function file_exists;
1519
use function file_put_contents;
@@ -45,6 +49,9 @@ final class GeneratedConfig
4549
4650
public const NOT_INSTALLED = %s;
4751
52+
/** @var string|null */
53+
public const PHPSTAN_VERSION_CONSTRAINT = %s;
54+
4855
private function __construct()
4956
{
5057
}
@@ -110,6 +117,8 @@ public function process(Event $event): void
110117
$ignore = $packageExtra['phpstan/extension-installer']['ignore'];
111118
}
112119

120+
$phpstanVersionConstraints = [];
121+
113122
foreach ($composer->getRepositoryManager()->getLocalRepository()->getPackages() as $package) {
114123
if (
115124
$package->getType() !== 'phpstan-extension'
@@ -151,14 +160,36 @@ public function process(Event $event): void
151160
];
152161

153162
$installedPackages[$package->getName()] = true;
163+
164+
$packageRequires = $package->getRequires();
165+
if (array_key_exists('phpstan/phpstan', $packageRequires)) {
166+
$phpstanVersionConstraints[] = $packageRequires['phpstan/phpstan']->getConstraint();
167+
}
168+
}
169+
170+
$phpstanVersionConstraint = null;
171+
if (count($phpstanVersionConstraints) > 0 && class_exists(Intervals::class)) {
172+
if (count($phpstanVersionConstraints) === 1) {
173+
$multiConstraint = $phpstanVersionConstraints[0];
174+
} else {
175+
$multiConstraint = new MultiConstraint($phpstanVersionConstraints);
176+
}
177+
$compactedConstraint = Intervals::compactConstraint($multiConstraint);
178+
$phpstanVersionConstraint = sprintf(
179+
'%s%s && %s%s',
180+
$compactedConstraint->getLowerBound()->isInclusive() ? '>=' : '>',
181+
$compactedConstraint->getLowerBound()->getVersion(),
182+
$compactedConstraint->getUpperBound()->isInclusive() ? '<=' : '<',
183+
$compactedConstraint->getUpperBound()->getVersion()
184+
);
154185
}
155186

156187
ksort($data);
157188
ksort($installedPackages);
158189
ksort($notInstalledPackages);
159190
sort($ignoredPackages);
160191

161-
$generatedConfigFileContents = sprintf(self::$generatedFileTemplate, var_export($data, true), var_export($notInstalledPackages, true));
192+
$generatedConfigFileContents = sprintf(self::$generatedFileTemplate, var_export($data, true), var_export($notInstalledPackages, true), var_export($phpstanVersionConstraint, true));
162193
file_put_contents($generatedConfigFilePath, $generatedConfigFileContents);
163194
$io->write('<info>phpstan/extension-installer:</info> Extensions installed');
164195

0 commit comments

Comments
 (0)