Completed
Push — master ( 797df5...db0fc9 )
by Nikola
02:41
created

PreRelease::comparePreReleaseIdentifierValues()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 8.2222
cc 7
eloc 10
nc 4
nop 2
crap 7
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\Metadata;
13
14
use Version\Identifier\PreReleaseIdentifier;
15
16
/**
17
 * @author Nikola Posa <[email protected]>
18
 */
19
final class PreRelease extends BaseIdentifyingMetadata
20
{
21 41
    protected static function createAssociatedIdentifier($value)
22
    {
23 41
        return PreReleaseIdentifier::create($value);
24
    }
25
26
    /**
27
     * @param self $preRelease
28
     * @return int (> 0 if $this > $preRelease, < 0 if $this < $preRelease, 0 if equal)
29
     */
30 17
    public function compareTo(PreRelease $preRelease)
31
    {
32 17
        $pr1Ids = array_values($this->getIdentifiers());
33 17
        $pr2Ids = array_values($preRelease->getIdentifiers());
34
35 17
        $pr1Count = count($pr1Ids);
36 17
        $pr2Count = count($pr2Ids);
37
38 17
        $limit = min($pr1Count, $pr2Count);
39
40 17
        for ($i = 0; $i < $limit; $i++) {
41 17
            $pr1IdVal = $pr1Ids[$i]->getValue();
42 17
            $pr2IdVal = $pr2Ids[$i]->getValue();
43
44 17
            if ($pr1IdVal == $pr2IdVal) {
45 11
                continue;
46
            }
47
48 11
            return $this->comparePreReleaseIdentifierValues($pr1IdVal, $pr2IdVal);
49
        }
50
51 7
        return $pr1Count - $pr2Count;
52
    }
53
54 11
    private function comparePreReleaseIdentifierValues($pr1IdVal, $pr2IdVal)
55
    {
56 11
        $pr1IsAlpha = ctype_alpha($pr1IdVal);
57 11
        $pr2IsAlpha = ctype_alpha($pr2IdVal);
58
59 11
        if ($pr1IsAlpha && !$pr2IsAlpha) {
60 2
            return 1;
61
        }
62
63 9
        if ($pr2IsAlpha && !$pr1IsAlpha) {
64 1
            return -1;
65
        }
66
67 9
        if (ctype_digit($pr1IdVal) && ctype_digit($pr2IdVal)) {
68 3
            return (int) $pr1IdVal - (int) $pr2IdVal;
69
        }
70
71 7
        return strcmp($pr1IdVal, $pr2IdVal);
72
    }
73
}
74