GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Comparator::compareIdentifiers()   D
last analyzed

Complexity

Conditions 20
Paths 64

Size

Total Lines 55
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 420

Importance

Changes 0
Metric Value
cc 20
eloc 34
c 0
b 0
f 0
nc 64
nop 2
dl 0
loc 55
rs 4.1666
ccs 0
cts 44
cp 0
crap 420

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
declare(strict_types=1);
4
5
namespace Deployer\Component\PharUpdate\Version;
6
7
/**
8
 * Compares two Version instances.
9
 *
10
 * @author Kevin Herrera <[email protected]>
11
 */
12
class Comparator
13
{
14
    /**
15
     * The version is equal to another.
16
     */
17
    public const EQUAL_TO = 0;
18
19
    /**
20
     * The version is greater than another.
21
     */
22
    public const GREATER_THAN = 1;
23
24
    /**
25
     * The version is less than another.
26
     */
27
    public const LESS_THAN = -1;
28
29
    /**
30
     * Compares one version with another.
31
     *
32
     * @param Version $left  The left version to compare.
33
     * @param Version $right The right version to compare.
34
     *
35
     * @return integer Returns Comparator::EQUAL_TO if the two versions are
36
     *                 equal. If the left version is less than the right
37
     *                 version, Comparator::LESS_THAN is returned. If the left
38
     *                 version is greater than the right version,
39
     *                 Comparator::GREATER_THAN is returned.
40
     */
41
    public static function compareTo(Version $left, Version $right)
42
    {
43
        switch (true) {
44
            case ($left->getMajor() < $right->getMajor()):
45
                return self::LESS_THAN;
46
            case ($left->getMajor() > $right->getMajor()):
47
                return self::GREATER_THAN;
48
            case ($left->getMinor() > $right->getMinor()):
49
                return self::GREATER_THAN;
50
            case ($left->getMinor() < $right->getMinor()):
51
                return self::LESS_THAN;
52
            case ($left->getPatch() > $right->getPatch()):
53
                return self::GREATER_THAN;
54
            case ($left->getPatch() < $right->getPatch()):
55
                return self::LESS_THAN;
56
                // @codeCoverageIgnoreStart
57
        }
58
        // @codeCoverageIgnoreEnd
59
60
        return self::compareIdentifiers(
61
            $left->getPreRelease(),
62
            $right->getPreRelease(),
63
        );
64
    }
65
66
    /**
67
     * Checks if the left version is equal to the right.
68
     *
69
     * @param Version $left  The left version to compare.
70
     * @param Version $right The right version to compare.
71
     *
72
     * @return boolean TRUE if the left version is equal to the right, FALSE
73
     *                 if not.
74
     */
75
    public static function isEqualTo(Version $left, Version $right)
76
    {
77
        return (self::EQUAL_TO === self::compareTo($left, $right));
78
    }
79
80
    /**
81
     * Checks if the left version is greater than the right.
82
     *
83
     * @param Version $left  The left version to compare.
84
     * @param Version $right The right version to compare.
85
     *
86
     * @return boolean TRUE if the left version is greater than the right,
87
     *                 FALSE if not.
88
     */
89
    public static function isGreaterThan(Version $left, Version $right)
90
    {
91
        return (self::GREATER_THAN === self::compareTo($left, $right));
92
    }
93
94
    /**
95
     * Checks if the left version is less than the right.
96
     *
97
     * @param Version $left  The left version to compare.
98
     * @param Version $right The right version to compare.
99
     *
100
     * @return boolean TRUE if the left version is less than the right,
101
     *                 FALSE if not.
102
     */
103
    public static function isLessThan(Version $left, Version $right)
104
    {
105
        return (self::LESS_THAN === self::compareTo($left, $right));
106
    }
107
108
    /**
109
     * Compares the identifier components of the left and right versions.
110
     *
111
     * @param array $left  The left identifiers.
112
     * @param array $right The right identifiers.
113
     *
114
     * @return integer Returns Comparator::EQUAL_TO if the two identifiers are
115
     *                 equal. If the left identifiers is less than the right
116
     *                 identifiers, Comparator::LESS_THAN is returned. If the
117
     *                 left identifiers is greater than the right identifiers,
118
     *                 Comparator::GREATER_THAN is returned.
119
     */
120
    public static function compareIdentifiers(array $left, array $right)
121
    {
122
        if ($left && empty($right)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $left of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
123
            return self::LESS_THAN;
124
        } elseif (empty($left) && $right) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $right of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
125
            return self::GREATER_THAN;
126
        }
127
128
        $l = $left;
129
        $r = $right;
130
        $x = self::GREATER_THAN;
131
        $y = self::LESS_THAN;
132
133
        if (count($l) < count($r)) {
134
            $l = $right;
135
            $r = $left;
136
            $x = self::LESS_THAN;
137
            $y = self::GREATER_THAN;
138
        }
139
140
        foreach (array_keys($l) as $i) {
141
            if (!isset($r[$i])) {
142
                return $x;
143
            }
144
145
            if ($l[$i] === $r[$i]) {
146
                continue;
147
            }
148
149
            if (true === ($li = (false != preg_match('/^\d+$/', $l[$i])))) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing preg_match('/^\d+$/', $l[$i]) of type integer to the boolean false. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
150
                $l[$i] = intval($l[$i]);
151
            }
152
153
            if (true === ($ri = (false != preg_match('/^\d+$/', $r[$i])))) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing preg_match('/^\d+$/', $r[$i]) of type integer to the boolean false. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
154
                $r[$i] = intval($r[$i]);
155
            }
156
157
            if ($li && $ri) {
158
                return ($l[$i] > $r[$i]) ? $x : $y;
159
            } elseif (!$li && $ri) {
160
                return $x;
161
            } elseif ($li && !$ri) {
162
                return $y;
163
            }
164
165
            $result = strcmp($l[$i], $r[$i]);
166
167
            if ($result > 0) {
168
                return $x;
169
            } elseif ($result < 0) {
170
                return $y;
171
            }
172
        }
173
174
        return self::EQUAL_TO;
175
    }
176
}
177