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.
Passed
Push — master ( fba8ee...5e9ebc )
by Anton
02:06
created

Comparator::compareTo()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
nc 7
nop 2
dl 0
loc 24
ccs 0
cts 20
cp 0
crap 56
rs 8.6026
c 0
b 0
f 0
1
<?php
2
3
namespace Deployer\Component\PharUpdate\Version;
4
5
/**
6
 * Compares two Version instances.
7
 *
8
 * @author Kevin Herrera <[email protected]>
9
 */
10
class Comparator
11
{
12
    /**
13
     * The version is equal to another.
14
     */
15
    const EQUAL_TO = 0;
16
17
    /**
18
     * The version is greater than another.
19
     */
20
    const GREATER_THAN = 1;
21
22
    /**
23
     * The version is less than another.
24
     */
25
    const LESS_THAN = -1;
26
27
    /**
28
     * Compares one version with another.
29
     *
30
     * @param Version $left  The left version to compare.
31
     * @param Version $right The right version to compare.
32
     *
33
     * @return integer Returns Comparator::EQUAL_TO if the two versions are
34
     *                 equal. If the left version is less than the right
35
     *                 version, Comparator::LESS_THAN is returned. If the left
36
     *                 version is greater than the right version,
37
     *                 Comparator::GREATER_THAN is returned.
38
     */
39
    public static function compareTo(Version $left, Version $right)
40
    {
41
        switch (true) {
42
            case ($left->getMajor() < $right->getMajor()):
43
                return self::LESS_THAN;
44
            case ($left->getMajor() > $right->getMajor()):
45
                return self::GREATER_THAN;
46
            case ($left->getMinor() > $right->getMinor()):
47
                return self::GREATER_THAN;
48
            case ($left->getMinor() < $right->getMinor()):
49
                return self::LESS_THAN;
50
            case ($left->getPatch() > $right->getPatch()):
51
                return self::GREATER_THAN;
52
            case ($left->getPatch() < $right->getPatch()):
53
                return self::LESS_THAN;
54
            // @codeCoverageIgnoreStart
55
        }
56
        // @codeCoverageIgnoreEnd
57
58
        return self::compareIdentifiers(
59
            $left->getPreRelease(),
60
            $right->getPreRelease()
61
        );
62
    }
63
64
    /**
65
     * Checks if the left version is equal to the right.
66
     *
67
     * @param Version $left  The left version to compare.
68
     * @param Version $right The right version to compare.
69
     *
70
     * @return boolean TRUE if the left version is equal to the right, FALSE
71
     *                 if not.
72
     */
73
    public static function isEqualTo(Version $left, Version $right)
74
    {
75
        return (self::EQUAL_TO === self::compareTo($left, $right));
76
    }
77
78
    /**
79
     * Checks if the left version is greater than the right.
80
     *
81
     * @param Version $left  The left version to compare.
82
     * @param Version $right The right version to compare.
83
     *
84
     * @return boolean TRUE if the left version is greater than the right,
85
     *                 FALSE if not.
86
     */
87
    public static function isGreaterThan(Version $left, Version $right)
88
    {
89
        return (self::GREATER_THAN === self::compareTo($left, $right));
90
    }
91
92
    /**
93
     * Checks if the left version is less than the right.
94
     *
95
     * @param Version $left  The left version to compare.
96
     * @param Version $right The right version to compare.
97
     *
98
     * @return boolean TRUE if the left version is less than the right,
99
     *                 FALSE if not.
100
     */
101
    public static function isLessThan(Version $left, Version $right)
102
    {
103
        return (self::LESS_THAN === self::compareTo($left, $right));
104
    }
105
106
    /**
107
     * Compares the identifier components of the left and right versions.
108
     *
109
     * @param array $left  The left identifiers.
110
     * @param array $right The right identifiers.
111
     *
112
     * @return integer Returns Comparator::EQUAL_TO if the two identifiers are
113
     *                 equal. If the left identifiers is less than the right
114
     *                 identifiers, Comparator::LESS_THAN is returned. If the
115
     *                 left identifiers is greater than the right identifiers,
116
     *                 Comparator::GREATER_THAN is returned.
117
     */
118
    public static function compareIdentifiers(array $left, array $right)
119
    {
120
        if ($left && empty($right)) {
0 ignored issues
show
Bug Best Practice introduced by Anton Medvedev
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...
121
            return self::LESS_THAN;
122
        } elseif (empty($left) && $right) {
0 ignored issues
show
Bug Best Practice introduced by Anton Medvedev
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...
123
            return self::GREATER_THAN;
124
        }
125
126
        $l = $left;
127
        $r = $right;
128
        $x = self::GREATER_THAN;
129
        $y = self::LESS_THAN;
130
131
        if (count($l) < count($r)) {
132
            $l = $right;
133
            $r = $left;
134
            $x = self::LESS_THAN;
135
            $y = self::GREATER_THAN;
136
        }
137
138
        foreach (array_keys($l) as $i) {
139
            if (!isset($r[$i])) {
140
                return $x;
141
            }
142
143
            if ($l[$i] === $r[$i]) {
144
                continue;
145
            }
146
147 View Code Duplication
            if (true === ($li = (false != preg_match('/^\d+$/', $l[$i])))) {
0 ignored issues
show
Bug Best Practice introduced by Anton Medvedev
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...
148
                $l[$i] = intval($l[$i]);
149
            }
150
151 View Code Duplication
            if (true === ($ri = (false != preg_match('/^\d+$/', $r[$i])))) {
0 ignored issues
show
Bug Best Practice introduced by Anton Medvedev
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...
152
                $r[$i] = intval($r[$i]);
153
            }
154
155
            if ($li && $ri) {
156
                return ($l[$i] > $r[$i]) ? $x : $y;
157
            } elseif (!$li && $ri) {
158
                return $x;
159
            } elseif ($li && !$ri) {
160
                return $y;
161
            }
162
163
            $result = strcmp($l[$i], $r[$i]);
164
165
            if ($result > 0) {
166
                return $x;
167
            } elseif ($result < 0) {
168
                return $y;
169
            }
170
        }
171
172
        return self::EQUAL_TO;
173
    }
174
}
175