Comparator::compare()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 3
rs 10
1
<?php
2
3
namespace Fractal\SemVer;
4
5
6
use Fractal\SemVer\Contracts\VersionInterface;
7
use Fractal\SemVer\Exceptions\InvalidOperatorException;
8
use Fractal\SemVer\Exceptions\VersionClassNotEqualException;
9
10
/**
11
 * Class Comparator
12
 *
13
 * @author Mikhail Shtanko <[email protected]>
14
 */
15
final class Comparator
16
{
17
    /**
18
     * Compares version one greater than version two
19
     *
20
     * @param \Fractal\SemVer\Contracts\VersionInterface $one
21
     * @param \Fractal\SemVer\Contracts\VersionInterface $two
22
     *
23
     * @return bool
24
     *
25
     * @throws \Fractal\SemVer\Exceptions\InvalidOperatorException
26
     * @throws \Fractal\SemVer\Exceptions\VersionClassNotEqualException
27
     */
28 2
    public static function gt(VersionInterface $one, VersionInterface $two): bool
29
    {
30 2
        return static::compare($one, $two, Operator::GREATER);
31
    }
32
33
    /**
34
     * Compares version one greater than or equal to version two
35
     *
36
     * @param \Fractal\SemVer\Contracts\VersionInterface $one
37
     * @param \Fractal\SemVer\Contracts\VersionInterface $two
38
     *
39
     * @return bool
40
     *
41
     * @throws \Fractal\SemVer\Exceptions\InvalidOperatorException
42
     * @throws \Fractal\SemVer\Exceptions\VersionClassNotEqualException
43
     */
44 2
    public static function gte(VersionInterface $one, VersionInterface $two): bool
45
    {
46 2
        return static::compare($one, $two, Operator::GREATER_OR_EQUAL);
47
    }
48
49
    /**
50
     * Compares version one less than version two
51
     *
52
     * @param \Fractal\SemVer\Contracts\VersionInterface $one
53
     * @param \Fractal\SemVer\Contracts\VersionInterface $two
54
     *
55
     * @return bool
56
     *
57
     * @throws \Fractal\SemVer\Exceptions\InvalidOperatorException
58
     * @throws \Fractal\SemVer\Exceptions\VersionClassNotEqualException
59
     */
60 2
    public static function lt(VersionInterface $one, VersionInterface $two): bool
61
    {
62 2
        return static::compare($one, $two, Operator::LESS);
63
    }
64
65
    /**
66
     * Compares version one less than or equal to version two
67
     *
68
     * @param \Fractal\SemVer\Contracts\VersionInterface $one
69
     * @param \Fractal\SemVer\Contracts\VersionInterface $two
70
     *
71
     * @return bool
72
     *
73
     * @throws \Fractal\SemVer\Exceptions\InvalidOperatorException
74
     * @throws \Fractal\SemVer\Exceptions\VersionClassNotEqualException
75
     */
76 2
    public static function lte(VersionInterface $one, VersionInterface $two): bool
77
    {
78 2
        return static::compare($one, $two, Operator::LESS_OR_EQUAL);
79
    }
80
81
    /**
82
     * Compares version one equal to version two
83
     *
84
     * @param \Fractal\SemVer\Contracts\VersionInterface $one
85
     * @param \Fractal\SemVer\Contracts\VersionInterface $two
86
     *
87
     * @return bool
88
     *
89
     * @throws \Fractal\SemVer\Exceptions\InvalidOperatorException
90
     * @throws \Fractal\SemVer\Exceptions\VersionClassNotEqualException
91
     */
92 2
    public static function eq(VersionInterface $one, VersionInterface $two): bool
93
    {
94 2
        return static::compare($one, $two, Operator::EQUAL);
95
    }
96
97
    /**
98
     * Compares version one not equal to version two
99
     *
100
     * @param \Fractal\SemVer\Contracts\VersionInterface $one
101
     * @param \Fractal\SemVer\Contracts\VersionInterface $two
102
     *
103
     * @return bool
104
     *
105
     * @throws \Fractal\SemVer\Exceptions\InvalidOperatorException
106
     * @throws \Fractal\SemVer\Exceptions\VersionClassNotEqualException
107
     */
108 2
    public static function ne(VersionInterface $one, VersionInterface $two): bool
109
    {
110 2
        return static::compare($one, $two, Operator::NOT_EQUAL);
111
    }
112
113
    /**
114
     * Compares version one and two by operator
115
     *
116
     * @param \Fractal\SemVer\Contracts\VersionInterface $one
117
     * @param \Fractal\SemVer\Contracts\VersionInterface $two
118
     * @param string $operator
119
     *
120
     * @return bool
121
     *
122
     * @throws \Fractal\SemVer\Exceptions\InvalidOperatorException
123
     * @throws \Fractal\SemVer\Exceptions\VersionClassNotEqualException
124
     */
125 15
    protected static function compare(VersionInterface $one, VersionInterface $two, string $operator): bool
126
    {
127 15
        if (! Operator::has($operator)) {
128 1
            throw new InvalidOperatorException($operator);
129
        }
130
131 14
        if (\get_class($one) !== \get_class($two)) {
132 1
            throw new VersionClassNotEqualException($one, $two);
133
        }
134
135 13
        return version_compare($one, $two, $operator);
136
    }
137
}
138