From 5229813c4761830f1c7ff067f55d860c68655bbb Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Tue, 11 Jun 2024 10:57:05 -0700 Subject: [PATCH] [ticket/17336] Optimize version sorting PHPBB-17336 Signed-off-by: Matt Friedman --- phpBB/phpbb/composer/installer.php | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/phpBB/phpbb/composer/installer.php b/phpBB/phpbb/composer/installer.php index 68b7f2e520..3c59fc552d 100644 --- a/phpBB/phpbb/composer/installer.php +++ b/phpBB/phpbb/composer/installer.php @@ -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