Completed
Push — master ( a48ec2...4c6d80 )
by
unknown
13:40
created

VersionNumberUtility::raiseVersionNumber()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 21
nc 5
nop 2
dl 0
loc 26
rs 9.2728
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Core\Utility;
17
18
use TYPO3\CMS\Core\Information\Typo3Version;
19
20
/**
21
 * Class with helper functions for version number handling
22
 */
23
class VersionNumberUtility
24
{
25
    /**
26
     * Returns an integer from a three part version number, eg '4.12.3' -> 4012003
27
     *
28
     * @param string $versionNumber Version number on format x.x.x
29
     * @return int Integer version of version number (where each part can count to 999)
30
     */
31
    public static function convertVersionNumberToInteger($versionNumber)
32
    {
33
        $versionParts = explode('.', $versionNumber);
34
        $version = $versionParts[0];
35
        for ($i = 1; $i < 3; $i++) {
36
            if (!empty($versionParts[$i])) {
37
                $version .= str_pad((int)$versionParts[$i], 3, '0', STR_PAD_LEFT);
38
            } else {
39
                $version .= '000';
40
            }
41
        }
42
        return (int)$version;
43
    }
44
45
    /**
46
     * Removes -dev -alpha -beta -RC states (also without '-' prefix) from a version number
47
     * and replaces them by .0 and normalizes to a three part version number
48
     *
49
     * @return string
50
     */
51
    public static function getNumericTypo3Version()
52
    {
53
        $t3version = static::getCurrentTypo3Version();
54
        $t3version = preg_replace('/-?(dev|alpha|beta|RC).*$/', '', $t3version);
55
        $parts = GeneralUtility::intExplode('.', $t3version . '..');
56
        $t3version = MathUtility::forceIntegerInRange($parts[0], 0, 999) . '.' .
57
            MathUtility::forceIntegerInRange($parts[1], 0, 999) . '.' .
58
            MathUtility::forceIntegerInRange($parts[2], 0, 999);
59
        return $t3version;
60
    }
61
62
    /**
63
     * Wrapper function for TYPO3_version constant to make functions using
64
     * the constant unit testable
65
     *
66
     * @return string
67
     */
68
    public static function getCurrentTypo3Version()
69
    {
70
        return (string)GeneralUtility::makeInstance(Typo3Version::class);
71
    }
72
73
    /**
74
     * This function converts version range strings (like '4.2.0-4.4.99') to an array
75
     * (like array('4.2.0', '4.4.99'). It also forces each version part to be between
76
     * 0 and 999
77
     *
78
     * @param string $versionsString
79
     * @return array
80
     */
81
    public static function convertVersionsStringToVersionNumbers($versionsString)
82
    {
83
        $versions = GeneralUtility::trimExplode('-', $versionsString);
84
        $versionsCount = count($versions);
85
        for ($i = 0; $i < $versionsCount; $i++) {
86
            $cleanedVersion = GeneralUtility::trimExplode('.', $versions[$i]);
87
            $cleanedVersionCount = count($cleanedVersion);
88
            for ($j = 0; $j < $cleanedVersionCount; $j++) {
89
                $cleanedVersion[$j] = MathUtility::forceIntegerInRange($cleanedVersion[$j], 0, 999);
0 ignored issues
show
Bug introduced by
$cleanedVersion[$j] of type string is incompatible with the type integer expected by parameter $theInt of TYPO3\CMS\Core\Utility\M...::forceIntegerInRange(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

89
                $cleanedVersion[$j] = MathUtility::forceIntegerInRange(/** @scrutinizer ignore-type */ $cleanedVersion[$j], 0, 999);
Loading history...
90
            }
91
            $cleanedVersionString = implode('.', $cleanedVersion);
92
            if (static::convertVersionNumberToInteger($cleanedVersionString) === 0) {
93
                $cleanedVersionString = '';
94
            }
95
            $versions[$i] = $cleanedVersionString;
96
        }
97
        return $versions;
98
    }
99
100
    /**
101
     * Parses the version number x.x.x and returns an array with the various parts.
102
     * It also forces each … 0 to 999
103
     *
104
     * @param string $version Version code, x.x.x
105
     * @return array
106
     */
107
    public static function convertVersionStringToArray($version)
108
    {
109
        $parts = GeneralUtility::intExplode('.', $version . '..');
110
        $parts[0] = MathUtility::forceIntegerInRange($parts[0], 0, 999);
111
        $parts[1] = MathUtility::forceIntegerInRange($parts[1], 0, 999);
112
        $parts[2] = MathUtility::forceIntegerInRange($parts[2], 0, 999);
113
        $result = [];
114
        $result['version'] = $parts[0] . '.' . $parts[1] . '.' . $parts[2];
115
        $result['version_int'] = (int)($parts[0] * 1000000 + $parts[1] * 1000 + $parts[2]);
116
        $result['version_main'] = $parts[0];
117
        $result['version_sub'] = $parts[1];
118
        $result['version_dev'] = $parts[2];
119
        return $result;
120
    }
121
}
122