Skip to content

Commit

Permalink
Dump PHPStan version constraint to GeneratedConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jun 6, 2024
1 parent 9416e06 commit 219172e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/GeneratedConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ final class GeneratedConfig

public const NOT_INSTALLED = [];

/** @var string|null */
public const PHPSTAN_VERSION_CONSTRAINT = null;

private function __construct()
{
}
Expand Down
33 changes: 32 additions & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,6 +49,9 @@ final class GeneratedConfig
public const NOT_INSTALLED = %s;
/** @var string|null */
public const PHPSTAN_VERSION_CONSTRAINT = %s;
private function __construct()
{
}
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -151,14 +160,36 @@ 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);
ksort($installedPackages);
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('<info>phpstan/extension-installer:</info> Extensions installed');

Expand Down

0 comments on commit 219172e

Please sign in to comment.