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