From 258606908dc22f7fe5840edf8fb08559e5b0c219 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Thu, 6 Jun 2024 13:21:08 +0200 Subject: [PATCH] Convert constraints into strings consistently --- src/Plugin.php | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index e1fd498..cf0ef90 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -8,6 +8,7 @@ use Composer\Plugin\PluginInterface; use Composer\Script\Event; use Composer\Script\ScriptEvents; +use Composer\Semver\Constraint\ConstraintInterface; use Composer\Semver\Constraint\MultiConstraint; use Composer\Semver\Intervals; use Composer\Util\Filesystem; @@ -165,7 +166,7 @@ public function process(Event $event): void 'relative_install_path' => $fs->findShortestPath(dirname($generatedConfigFilePath), $absoluteInstallPath, true), 'extra' => $package->getExtra()['phpstan'] ?? null, 'version' => $package->getFullPrettyVersion(), - 'phpstanVersionConstraint' => $phpstanConstraint !== null ? (string) $phpstanConstraint : null, + 'phpstanVersionConstraint' => $phpstanConstraint !== null ? $this->constraintIntoString($phpstanConstraint) : null, ]; $installedPackages[$package->getName()] = true; @@ -178,14 +179,7 @@ public function process(Event $event): void } 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() - ); + $phpstanVersionConstraint = $this->constraintIntoString(Intervals::compactConstraint($multiConstraint)); } ksort($data); @@ -214,4 +208,15 @@ public function process(Event $event): void } } + private function constraintIntoString(ConstraintInterface $constraint): string + { + return sprintf( + '%s%s && %s%s', + $constraint->getLowerBound()->isInclusive() ? '>=' : '>', + $constraint->getLowerBound()->getVersion(), + $constraint->getUpperBound()->isInclusive() ? '<=' : '<', + $constraint->getUpperBound()->getVersion() + ); + } + }