mirror of
https://github.com/nextcloud/server.git
synced 2026-10-15 02:57:15 +02:00
PHP 8.3 allows class constants to declare a type. Adopt it where doing so cannot break third-party apps. Typing an inheritable constant is a hard BC break: a subclass that redeclares it untyped fails to load with "Type of C::FOO must be compatible with P::FOO of type string". Changes are therefore limited to constants that cannot be redeclared by a subclass: - private const (not inherited) - final public/protected const - constants declared in a final class or an enum Interface constants, trait constants, and public/protected constants in non-final (including abstract) classes are left untyped, as is all of lib/public (OCP) and lib/unstable (NCU). One review-requested exception: the public OBJECT_PREFIX/OBJECT_SUFFIX constants of the app-internal CalDAV import helpers (TextImporter, XmlImporter) are typed as well; these classes are not public API and have no subclasses. Only string, int and array are used. float is avoided because it would silently coerce an int literal and change === comparisons. No constant name or value is modified: every changed line adds only the type token, so runtime behaviour is unchanged. Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com>
82 lines
2.9 KiB
PHP
82 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
namespace OC\App;
|
|
|
|
use InvalidArgumentException;
|
|
use function explode;
|
|
|
|
class CompareVersion {
|
|
private const string REGEX_MAJOR = '/^\d+$/';
|
|
private const string REGEX_MAJOR_MINOR = '/^\d+\.\d+$/';
|
|
private const string REGEX_MAJOR_MINOR_PATCH = '/^\d+\.\d+\.\d+(?!\.\d+)/';
|
|
private const string REGEX_ACTUAL = '/^\d+(\.\d+){1,2}/';
|
|
|
|
/**
|
|
* Checks if the given server version fulfills the given (app) version requirements.
|
|
*
|
|
* Version requirements can be 'major.minor.patch', 'major.minor' or just 'major',
|
|
* so '13.0.1', '13.0' and '13' are valid.
|
|
*
|
|
* @param string $actual version as major.minor.patch notation
|
|
* @param string $required version where major is required and minor and patch are optional
|
|
* @param string $comparator passed to `version_compare`
|
|
* @return bool whether the requirement is fulfilled
|
|
* @throws InvalidArgumentException if versions specified in an invalid format
|
|
*/
|
|
public function isCompatible(string $actual, string $required,
|
|
string $comparator = '>='): bool {
|
|
if (!preg_match(self::REGEX_ACTUAL, $actual, $matches)) {
|
|
throw new InvalidArgumentException("version specification $actual is invalid");
|
|
}
|
|
$cleanActual = $matches[0];
|
|
|
|
if (preg_match(self::REGEX_MAJOR, $required) === 1) {
|
|
return $this->compareMajor($cleanActual, $required, $comparator);
|
|
} elseif (preg_match(self::REGEX_MAJOR_MINOR, $required) === 1) {
|
|
return $this->compareMajorMinor($cleanActual, $required, $comparator);
|
|
} elseif (preg_match(self::REGEX_MAJOR_MINOR_PATCH, $required) === 1) {
|
|
return $this->compareMajorMinorPatch($cleanActual, $required, $comparator);
|
|
} else {
|
|
throw new InvalidArgumentException("required version $required is invalid");
|
|
}
|
|
}
|
|
|
|
private function compareMajor(string $actual, string $required,
|
|
string $comparator) {
|
|
$actualMajor = explode('.', $actual)[0];
|
|
$requiredMajor = explode('.', $required)[0];
|
|
|
|
return version_compare($actualMajor, $requiredMajor, $comparator);
|
|
}
|
|
|
|
private function compareMajorMinor(string $actual, string $required,
|
|
string $comparator) {
|
|
$actualMajor = explode('.', $actual)[0];
|
|
$actualMinor = explode('.', $actual)[1];
|
|
$requiredMajor = explode('.', $required)[0];
|
|
$requiredMinor = explode('.', $required)[1];
|
|
|
|
return version_compare("$actualMajor.$actualMinor",
|
|
"$requiredMajor.$requiredMinor", $comparator);
|
|
}
|
|
|
|
private function compareMajorMinorPatch($actual, $required, $comparator) {
|
|
$actualMajor = explode('.', $actual)[0];
|
|
$actualMinor = explode('.', $actual)[1];
|
|
$actualPatch = explode('.', $actual)[2];
|
|
$requiredMajor = explode('.', $required)[0];
|
|
$requiredMinor = explode('.', $required)[1];
|
|
$requiredPatch = explode('.', $required)[2];
|
|
|
|
return version_compare("$actualMajor.$actualMinor.$actualPatch",
|
|
"$requiredMajor.$requiredMinor.$requiredPatch", $comparator);
|
|
}
|
|
}
|