Completed
Pull Request — master (#9)
by Nikola
02:21
created

SemverComparator::compareMeta()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 23
ccs 12
cts 12
cp 1
rs 8.5906
cc 5
eloc 13
nc 5
nop 2
crap 5
1
<?php
2
3
/**
4
 * This file is part of the Version package.
5
 *
6
 * Copyright (c) Nikola Posa <[email protected]>
7
 *
8
 * For full copyright and license information, please refer to the LICENSE file,
9
 * located at the package root folder.
10
 */
11
12
namespace Version\Comparator;
13
14
use Version\Version;
15
16
/**
17
 * @author Nikola Posa <[email protected]>
18
 */
19
final class SemverComparator implements ComparatorInterface
20
{
21
    /**
22
     * {@inheritDoc}
23
     */
24 37
    public function compare(Version $version1, Version $version2)
25
    {
26 37
        if ($version1->getMajor() > $version2->getMajor()) {
27 6
            return 1;
28
        }
29
30 36
        if ($version1->getMajor() < $version2->getMajor()) {
31 9
            return -1;
32
        }
33
34 33
        if ($version1->getMinor() > $version2->getMinor()) {
35 8
            return 1;
36
        }
37
38 27
        if ($version1->getMinor() < $version2->getMinor()) {
39 1
            return -1;
40
        }
41
42 27
        if ($version1->getPatch() > $version2->getPatch()) {
43 4
            return 1;
44
        }
45
46 24
        if ($version1->getPatch() < $version2->getPatch()) {
47 2
            return -1;
48
        }
49
50 22
        return $this->compareMeta($version1, $version2);
51
    }
52
53 22
    private function compareMeta(Version $version1, Version $version2)
54
    {
55 22
        $v1IsPreRelease = $version1->isPreRelease();
56 22
        $v2IsPreRelease = $version2->isPreRelease();
57
58 22
        if ($v1IsPreRelease xor $v2IsPreRelease) {
59
            return !$v1IsPreRelease
60 3
                ? 1 // normal version has greater precedence than a pre-release version version
61 3
                : -1; // pre-release version has lower precedence than a normal version
62
        }
63
64 19
        $result = $version1->getPreRelease()->compareTo($version2->getPreRelease());
65
66 19
        if ($result > 0) {
67 7
            return 1;
68
        }
69
70 12
        if ($result < 0) {
71 1
            return -1;
72
        }
73
74 11
        return 0;
75
    }
76
}
77