phpcomposer-phpshopwaresemantic-versioning

Parsing package versions with wildcards for comparison in PHP


I am trying to compare the package version such as ^6.1||^6.2||^6.3 using \Composer\Semver\VersionParser but am not getting the expected result.

I am breaking down the version like so

            $currentVersion = '6.3';
            $requiredPackage = '^6.1||^6.2||^6.3';
            $compatibleVersions = explode('||', $requiredPackage);
            $lowestSupported = ltrim(current($compatibleVersions), '^');
            $highestSupported = ltrim(end($compatibleVersions), '^');

            $parser = new VersionParser();
            $lowestSupported = $parser->normalize($lowestSupported);
            $highestSupported = $parser->normalize($highestSupported);

            $isLower = version_compare($currentVersion, $lowestSupported, '<');
            $isHigher = version_compare($currentVersion, $highestSupported, '>');

            if ($isLower || $isHigher) {
                return false;
            }

This works fine for cases where $currentVersion is 6.3, but I think it would fail for 6.3.4.0 because although ^6.3 matches the package, but since VersionParser normalizes the version 6.3 as 6.3.0.0 instead, and it cannot parse the ^6.3 correctly to allow 6.3.4.0.

Any ideas?


Solution

  • I was able to accomplish it using Nico Haase's suggestion as following:

    $isLower = Comparator::lessThan($currentVersion, $lowestSupported);
    $isHigher = Comparator::greaterThan($highestSupported, $currentVersion);