Completed
Pull Request — master (#2)
by Julien
02:17
created

PreReleaseComparator::isInt()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace SemVer\SemVer;
5
6
final class PreReleaseComparator
7
{
8
    
9
    public function __invoke()
10
    {
11
        dump('ok');
12
    }
13
    /**
14
     * @param Version $version1
15
     * @param Version $version2
16
     *
17
     * @return int
18
     */
19
    public static function compare(Version $version1, Version $version2) : int
20
    {
21
        $preRelease1 = $version1->getPreRelease();
22
        $preRelease2 = $version2->getPreRelease();
23
24
        $leftPreReleaseIsEmpty = '' === $preRelease1;
25
        $rightPreReleaseIsEmpty = '' === $preRelease2;
26
        if ($rightPreReleaseIsEmpty !== $leftPreReleaseIsEmpty) {
27
            return $leftPreReleaseIsEmpty ? 1 : -1;
28
        }
29
30
        if ($leftPreReleaseIsEmpty) {
31
            return 0;
32
        }
33
34
        return self::compareIdentifiers(explode('.', $preRelease1), explode('.', $preRelease2));
35
    }
36
37
    /**
38
     * @param array $preReleaseIdentifiers1
39
     * @param array $preReleaseIdentifiers2
40
     *
41
     * @return int
42
     */
43
    private static function compareIdentifiers(array $preReleaseIdentifiers1, array $preReleaseIdentifiers2) : int
44
    {
45
        do {
46
            $preReleasePart1 = array_shift($preReleaseIdentifiers1);
47
            $preReleasePart2 = array_shift($preReleaseIdentifiers2);
48
            if (null === $preReleasePart1) {
49
                return -1;
50
            }
51
52
            if (null === $preReleasePart2) {
53
                return 1;
54
            }
55
56
            $compare = self::compareIdentifier($preReleasePart1, $preReleasePart2);
57
        } while (0 === $compare && (count($preReleaseIdentifiers1) || count($preReleaseIdentifiers2)));
58
59
        return $compare;
60
    }
61
62
    /**
63
     * @param string $preReleasePart1
64
     * @param string $preReleasePart2
65
     *
66
     * @return int
67
     */
68
    private static function compareIdentifier(string $preReleasePart1, string $preReleasePart2) : int
69
    {
70
        $mineIsInt = self::isInt($preReleasePart1);
71
        $theirIsInt = self::isInt($preReleasePart2);
72
73
        if ($mineIsInt !== $theirIsInt) {
74
            return $mineIsInt ? -1 : 1;
75
        }
76
77
        if ($mineIsInt) {
78
            return ((int)$preReleasePart1) <=> ((int)$preReleasePart2);
79
        }
80
81
        return $preReleasePart1 <=> $preReleasePart2;
82
    }
83
84
    /**
85
     * @param string $identifier
86
     *
87
     * @return bool
88
     */
89
    private static function isInt(string $identifier)
90
    {
91
        return ctype_digit($identifier) && strpos($identifier, '00') !== 0;
92
    }
93
}
94