LessThan   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 23
cbo 2
dl 0
loc 144
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 4 1
A compareMajor() 0 4 1
A compare() 0 7 2
A compareVersionNumber() 0 8 3
A compareFullLabel() 0 7 2
A compareMinor() 0 7 2
A comparePatch() 0 8 3
A compareLabel() 0 9 4
B compareLabelVersion() 0 10 5
1
<?php
2
3
/**
4
 * @copyright   (c) 2014-2017 brian ridley
5
 * @author      brian ridley <[email protected]>
6
 * @license     http://opensource.org/licenses/MIT MIT
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ptlis\SemanticVersion\Comparator;
13
14
use ptlis\SemanticVersion\Version\VersionInterface;
15
16
/**
17
 * Version less than comparator.
18
 */
19
final class LessThan implements ComparatorInterface
20
{
21
    /**
22
     * Return true if the left version is less than right version.
23
     *
24
     * @param VersionInterface $lVersion
25
     * @param VersionInterface $rVersion
26
     *
27
     * @return boolean
28
     */
29 21
    public function compare(VersionInterface $lVersion, VersionInterface $rVersion)
30
    {
31
        return (
32 21
            $this->compareVersionNumber($lVersion, $rVersion)
33 18
            || $this->compareFullLabel($lVersion, $rVersion)
34 21
        );
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 1
    public function __toString()
41
    {
42 1
        return '<';
43
    }
44
45
    /**
46
     * Returns true if left version number is less than right version number.
47
     *
48
     * @param VersionInterface $lVersion
49
     * @param VersionInterface $rVersion
50
     *
51
     * @return bool
52
     */
53 21
    private function compareVersionNumber(VersionInterface $lVersion, VersionInterface $rVersion)
54
    {
55
        return (
56 21
            $this->compareMajor($lVersion, $rVersion)
57 20
            || $this->compareMinor($lVersion, $rVersion)
58 20
            || $this->comparePatch($lVersion, $rVersion)
59 21
        );
60
    }
61
62
    /**
63
     * Returns true if left label is less than right version label.
64
     *
65
     * @param VersionInterface $lVersion
66
     * @param VersionInterface $rVersion
67
     *
68
     * @return bool
69
     */
70 18
    private function compareFullLabel(VersionInterface $lVersion, VersionInterface $rVersion)
71
    {
72
        return (
73 18
            $this->compareLabel($lVersion, $rVersion)
74 14
            || $this->compareLabelVersion($lVersion, $rVersion)
75 18
        );
76
    }
77
78
    /**
79
     * Returns true if left major is less than right major version.
80
     *
81
     * @param VersionInterface $lVersion
82
     * @param VersionInterface $rVersion
83
     *
84
     * @return bool
85
     */
86 21
    private function compareMajor(VersionInterface $lVersion, VersionInterface $rVersion)
87
    {
88 21
        return $lVersion->getMajor() < $rVersion->getMajor();
89
    }
90
91
    /**
92
     * Returns true if left & right major values match & left minor is less than right major version.
93
     *
94
     * @param VersionInterface $lVersion
95
     * @param VersionInterface $rVersion
96
     *
97
     * @return bool
98
     */
99 20
    private function compareMinor(VersionInterface $lVersion, VersionInterface $rVersion)
100
    {
101
        return (
102 20
            $lVersion->getMajor() == $rVersion->getMajor()
103 20
            && $lVersion->getMinor() < $rVersion->getMinor()
104 20
        );
105
    }
106
107
    /**
108
     * Returns true if left & right major & minor values match & left patch is less than right patch version.
109
     *
110
     * @param VersionInterface $lVersion
111
     * @param VersionInterface $rVersion
112
     *
113
     * @return bool
114
     */
115 19
    private function comparePatch(VersionInterface $lVersion, VersionInterface $rVersion)
116
    {
117
        return (
118 19
            $lVersion->getMajor() == $rVersion->getMajor()
119 19
            && $lVersion->getMinor() == $rVersion->getMinor()
120 19
            && $lVersion->getPatch() < $rVersion->getPatch()
121 19
        );
122
    }
123
124
    /**
125
     * Returns true if left & right major, minor & patch values match & left label precedence is less than right label
126
     * precedence.
127
     *
128
     * @param VersionInterface $lVersion
129
     * @param VersionInterface $rVersion
130
     *
131
     * @return bool
132
     */
133 18
    private function compareLabel(VersionInterface $lVersion, VersionInterface $rVersion)
134
    {
135
        return (
136 18
            $lVersion->getMajor() == $rVersion->getMajor()
137 18
            && $lVersion->getMinor() == $rVersion->getMinor()
138 18
            && $lVersion->getPatch() == $rVersion->getPatch()
139 18
            && $lVersion->getLabel()->getPrecedence() < $rVersion->getLabel()->getPrecedence()
140 18
        );
141
    }
142
143
    /**
144
     * Returns true if left & right major, minor, patch & label precedence values match & left label version is less
145
     * than right patch version.
146
     *
147
     * @param VersionInterface $lVersion
148
     * @param VersionInterface $rVersion
149
     *
150
     * @return bool
151
     */
152 14
    private function compareLabelVersion(VersionInterface $lVersion, VersionInterface $rVersion)
153
    {
154
        return (
155 14
            $lVersion->getMajor() == $rVersion->getMajor()
156 14
            && $lVersion->getMinor() == $rVersion->getMinor()
157 14
            && $lVersion->getPatch() == $rVersion->getPatch()
158 14
            && $lVersion->getLabel()->getPrecedence() == $rVersion->getLabel()->getPrecedence()
159 14
            && $lVersion->getLabel()->getVersion() < $rVersion->getLabel()->getVersion()
160 14
        );
161
    }
162
}
163