[ticket/17336] Optimize version sorting

PHPBB-17336

Signed-off-by: Matt Friedman <maf675@gmail.com>
This commit is contained in:
Matt Friedman 2024-06-11 10:57:05 -07:00
parent d8aa3f05e5
commit 5229813c47
No known key found for this signature in database

View file

@ -24,7 +24,6 @@ use Composer\Json\JsonValidationException;
use Composer\Package\BasePackage; use Composer\Package\BasePackage;
use Composer\Package\CompleteAliasPackage; use Composer\Package\CompleteAliasPackage;
use Composer\Package\CompletePackage; use Composer\Package\CompletePackage;
use Composer\Package\Version\VersionParser;
use Composer\PartialComposer; use Composer\PartialComposer;
use Composer\Repository\ComposerRepository; use Composer\Repository\ComposerRepository;
use Composer\Semver\Constraint\ConstraintInterface; use Composer\Semver\Constraint\ConstraintInterface;
@ -395,20 +394,23 @@ class installer
/** @var CompletePackage|CompleteAliasPackage $highest_version */ /** @var CompletePackage|CompleteAliasPackage $highest_version */
$highest_version = null; $highest_version = null;
/** @var CompletePackage|CompleteAliasPackage $version */ // Sort the versions array in descending order
foreach ($versions as $version) usort($versions, function ($a, $b)
{ {
if (!$highest_version || version_compare($version->getVersion(), $highest_version->getVersion(), '>')) return version_compare($b->getVersion(), $a->getVersion());
{ });
$highest_version = $version;
}
}
// If highest version is DEFAULT_BRANCH_ALIAS (9999999-dev), then it's a non-numeric dev branch handled by // The first element in the sorted array is the highest version
// an Alias, so we need to get the actual package being aliased in order to show the true non-numeric version. if (!empty($versions))
if ($highest_version->getVersion() === VersionParser::DEFAULT_BRANCH_ALIAS)
{ {
$highest_version = $highest_version->getAliasOf(); $highest_version = $versions[0];
// If highest version is a non-numeric dev branch, it's an instance of CompleteAliasPackage,
// so we need to get the package being aliased in order to show the true non-numeric version.
if ($highest_version instanceof CompleteAliasPackage)
{
$highest_version = $highest_version->getAliasOf();
}
} }
// Generates the entry // Generates the entry