PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, March 3, 2022

[FIXED] Parsing package versions with wildcards for comparison in PHP

 March 03, 2022     composer-php, php, semantic-versioning, shopware     No comments   

Issue

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);


Answered By - Huzaifa Mustafa
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing