Version::compare()   F
last analyzed

Complexity

Conditions 16
Paths 12289

Size

Total Lines 53
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 16
eloc 36
nc 12289
nop 2
dl 0
loc 53
rs 1.4
c 1
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* Copyright (C) 2024       Rafael San José         <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace Dolibarr\Lib;
20
21
abstract class Version
22
{
23
    /**
24
     *  Renvoi une version en chaine depuis une version en tableau
25
     *
26
     * @param array $versionarray Tableau de version (vermajeur,vermineur,autre)
27
     * @return     string                          Chaine version
28
     * @see Version::compare()
29
     */
30
    public static function toString($versionarray)
31
    {
32
        $string = '?';
33
        if (isset($versionarray[0])) {
34
            $string = $versionarray[0];
35
        }
36
        if (isset($versionarray[1])) {
37
            $string .= '.' . $versionarray[1];
38
        }
39
        if (isset($versionarray[2])) {
40
            $string .= '.' . $versionarray[2];
41
        }
42
        return $string;
43
    }
44
45
    /**
46
     *  Compare 2 versions (stored into 2 arrays).
47
     *  To check if Dolibarr version is lower than (x,y,z), do "if Version::compare(Version::toArray(), array(x.y.z)) <= 0"
48
     *  For example: if (Version::compare(Version::toArray(),array(4,0,-5)) >= 0) is true if version is 4.0 alpha or higher.
49
     *  For example: if (Version::compare(Version::toArray(),array(4,0,0)) >= 0) is true if version is 4.0 final or higher.
50
     *  For example: if (Version::compare(Version::toArray(),array(4,0,1)) >= 0) is true if version is 4.0.1 or higher.
51
     *  Alternative way to compare: if ((float) DOL_VERSION >= 4.0) is true if version is 4.0 alpha or higher (works only to compare first and second level)
52
     *
53
     * @param array $versionarray1 Array of version (vermajor,verminor,patch)
54
     * @param array $versionarray2 Array of version (vermajor,verminor,patch)
55
     * @return     int                             -4,-3,-2,-1 if versionarray1<versionarray2 (value depends on level of difference)
56
     *                                              0 if same
57
     *                                              1,2,3,4 if versionarray1>versionarray2 (value depends on level of difference)
58
     * @see Version::toString()
59
     */
60
    public static function compare($versionarray1, $versionarray2)
61
    {
62
        $ret = 0;
63
        $level = 0;
64
        $count1 = count($versionarray1);
65
        $count2 = count($versionarray2);
66
        $maxcount = max($count1, $count2);
67
        while ($level < $maxcount) {
68
            $operande1 = isset($versionarray1[$level]) ? $versionarray1[$level] : 0;
69
            $operande2 = isset($versionarray2[$level]) ? $versionarray2[$level] : 0;
70
            if (preg_match('/alpha|dev/i', $operande1)) {
71
                $operande1 = -5;
72
            }
73
            if (preg_match('/alpha|dev/i', $operande2)) {
74
                $operande2 = -5;
75
            }
76
            if (preg_match('/beta$/i', $operande1)) {
77
                $operande1 = -4;
78
            }
79
            if (preg_match('/beta$/i', $operande2)) {
80
                $operande2 = -4;
81
            }
82
            if (preg_match('/beta([0-9])+/i', $operande1)) {
83
                $operande1 = -3;
84
            }
85
            if (preg_match('/beta([0-9])+/i', $operande2)) {
86
                $operande2 = -3;
87
            }
88
            if (preg_match('/rc$/i', $operande1)) {
89
                $operande1 = -2;
90
            }
91
            if (preg_match('/rc$/i', $operande2)) {
92
                $operande2 = -2;
93
            }
94
            if (preg_match('/rc([0-9])+/i', $operande1)) {
95
                $operande1 = -1;
96
            }
97
            if (preg_match('/rc([0-9])+/i', $operande2)) {
98
                $operande2 = -1;
99
            }
100
            $level++;
101
            //print 'level '.$level.' '.$operande1.'-'.$operande2.'<br>';
102
            if ($operande1 < $operande2) {
103
                $ret = -$level;
104
                break;
105
            }
106
            if ($operande1 > $operande2) {
107
                $ret = $level;
108
                break;
109
            }
110
        }
111
        //print join('.',$versionarray1).'('.count($versionarray1).') / '.join('.',$versionarray2).'('.count($versionarray2).') => '.$ret.'<br>'."\n";
112
        return $ret;
113
    }
114
115
    /**
116
     * Returns an array of 3 elements with the PHP_MAJOR_VERSION,
117
     * PHP_MINOR_VERSION and PHP_RELEASE_VERSION.
118
     *
119
     * @return string[]
120
     * @see Version::compare()
121
     */
122
    public static function arrayPhp()
123
    {
124
        return explode('.', PHP_VERSION);
125
    }
126
127
    /**
128
     *  Return version Dolibarr
129
     *
130
     * @return     array               Tableau de version (vermajeur,vermineur,autre)
131
     * @see Version::compare()
132
     */
133
    public static function toArray()
134
    {
135
        return explode('.', DOL_VERSION);
136
    }
137
}
138