Commit eee89b81 authored by crell's avatar crell Committed by Stefan Bürk
Browse files

[!!!][TASK] Add native types to VersionNumberUtility

Also, a little cleanup in DefaultFunctionsProvider.
The method getCompatVersionFunction() was
calling convertVersionNumberToInteger() with an inconsistent
type as a result of TypoScript parsing. We therefore need to
force-cast it to a string, which is what
convertVersionNumberToInteger() wants.

Resolves: #97383
Related: #97210
Related: #97372
Releases: main
Change-Id: Id47e42fd70d8ec89f6255616a26e6666c461efe7
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/74317

Tested-by: Nikita Hovratov's avatarNikita Hovratov <nikita.h@live.de>
Tested-by: core-ci's avatarcore-ci <typo3@b13.com>
Tested-by: Stefan Bürk's avatarStefan Bürk <stefan@buerk.tech>
Reviewed-by: Nikita Hovratov's avatarNikita Hovratov <nikita.h@live.de>
Reviewed-by: Oliver Klee's avatarOliver Klee <typo3-coding@oliverklee.de>
Reviewed-by: Stefan Bürk's avatarStefan Bürk <stefan@buerk.tech>
parent 7ec2cb5f
......@@ -70,13 +70,11 @@ class DefaultFunctionsProvider implements ExpressionFunctionProviderInterface
{
return new ExpressionFunction(
'compatVersion',
static function () {
// Not implemented, we only use the evaluator
},
static function ($arguments, $str) {
static fn () => null, // Not implemented, we only use the evaluator
static function ($arguments, mixed $str) {
$typo3Version = GeneralUtility::makeInstance(Typo3Version::class);
return VersionNumberUtility::convertVersionNumberToInteger($typo3Version->getBranch()) >=
VersionNumberUtility::convertVersionNumberToInteger($str);
VersionNumberUtility::convertVersionNumberToInteger((string)$str);
}
);
}
......
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
......@@ -28,7 +30,7 @@ class VersionNumberUtility
* @param string $versionNumber Version number on format x.x.x
* @return int Integer version of version number (where each part can count to 999)
*/
public static function convertVersionNumberToInteger($versionNumber)
public static function convertVersionNumberToInteger(string $versionNumber): int
{
$versionParts = explode('.', $versionNumber);
$version = $versionParts[0];
......@@ -45,10 +47,8 @@ class VersionNumberUtility
/**
* Removes -dev -alpha -beta -RC states (also without '-' prefix) from a version number
* and replaces them by .0 and normalizes to a three part version number
*
* @return string
*/
public static function getNumericTypo3Version()
public static function getNumericTypo3Version(): string
{
$t3version = static::getCurrentTypo3Version();
$t3version = preg_replace('/-?(dev|alpha|beta|RC).*$/', '', $t3version);
......@@ -62,10 +62,8 @@ class VersionNumberUtility
/**
* Wrapper function for the static TYPO3 version to
* make functions using the constant unit testable.
*
* @return string
*/
public static function getCurrentTypo3Version()
public static function getCurrentTypo3Version(): string
{
return (string)GeneralUtility::makeInstance(Typo3Version::class);
}
......@@ -75,18 +73,16 @@ class VersionNumberUtility
* (like array('4.2.0', '4.4.99'). It also forces each version part to be between
* 0 and 999
*
* @param string $versionsString
* @return array
* @param string $versionsString A string in the form 'x.x.x-y.y.y'
* @return string[]
*/
public static function convertVersionsStringToVersionNumbers($versionsString)
public static function convertVersionsStringToVersionNumbers(string $versionsString): array
{
$versions = GeneralUtility::trimExplode('-', $versionsString);
$versionsCount = count($versions);
for ($i = 0; $i < $versionsCount; $i++) {
$cleanedVersion = GeneralUtility::trimExplode('.', $versions[$i]);
$cleanedVersionCount = count($cleanedVersion);
for ($j = 0; $j < $cleanedVersionCount; $j++) {
$cleanedVersion[$j] = MathUtility::forceIntegerInRange((int)$cleanedVersion[$j], 0, 999);
foreach ($versions as $i => $version) {
$cleanedVersion = GeneralUtility::trimExplode('.', $version);
foreach ($cleanedVersion as $j => $cleaned) {
$cleanedVersion[$j] = MathUtility::forceIntegerInRange((int)$cleaned, 0, 999);
}
$cleanedVersionString = implode('.', $cleanedVersion);
if (static::convertVersionNumberToInteger($cleanedVersionString) === 0) {
......@@ -101,10 +97,10 @@ class VersionNumberUtility
* Parses the version number x.x.x and returns an array with the various parts.
* It also forces each … 0 to 999
*
* @param string $version Version code, x.x.x
* @return array
* @param string $version Version string, in the format x.x.x
* @return array<string, int|string>
*/
public static function convertVersionStringToArray($version)
public static function convertVersionStringToArray(string $version): array
{
$parts = GeneralUtility::intExplode('.', $version . '..');
$parts[0] = MathUtility::forceIntegerInRange($parts[0], 0, 999);
......
......@@ -21,6 +21,7 @@ expectations and existing behavior.
- :php:`\TYPO3\CMS\Core\Utility\MathUtility`
- :php:`\TYPO3\CMS\Core\Utility\PathUtility`
- :php:`\TYPO3\CMS\Core\Utility\RootlineUtility`
- :php:`\TYPO3\CMS\Core\Utility\VersionNumberUtility`
The following PHP classes have added public class property types:
......
......@@ -111,7 +111,7 @@ class VersionNumberUtilityTest extends UnitTestCase
* @test
* @dataProvider convertVersionsStringToVersionNumbersForcesVersionNumberInRangeDataProvider
*/
public function convertVersionsStringToVersionNumbersForcesVersionNumberInRange($versionString, $expectedResult): void
public function convertVersionsStringToVersionNumbersForcesVersionNumberInRange(string $versionString, array $expectedResult): void
{
$versions = VersionNumberUtility::convertVersionsStringToVersionNumbers($versionString);
self::assertEquals($expectedResult, $versions);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment