+# check if we've got a distinct version
+if (isset($versions[$version])) {
+ printf("%s\n", $version);
+ exit;
+}
+
+$by_minor = [];
+# build the tree of latest versions per minor
+foreach (array_keys((array) $versions) as $release) {
+ list($major, $minor, $patch) = explode(".", $release);
+ if (isset($by_minor["$major.$minor"])) {
+ if (version_compare($release, $by_minor["$major.$minor"], "<")) {
+ continue;
+ }
+ }
+ $by_minor["$major.$minor"] = $release;
+}