Completed
Push — master ( 1110fe...581a9c )
by Nikola
06:17
created

PreRelease::comparePreReleaseIdentifierValues()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 7.049

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
ccs 9
cts 10
cp 0.9
rs 8.2222
cc 7
eloc 10
nc 4
nop 2
crap 7.049
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 34
    protected static function createAssociatedIdentifier($value)
22
    {
23 34
        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 16
    public function compareTo(PreRelease $preRelease)
31
    {
32 16
        $pr1Ids = array_values($this->getIdentifiers());
33 16
        $pr2Ids = array_values($preRelease->getIdentifiers());
34
35 16
        $pr1Count = count($pr1Ids);
36 16
        $pr2Count = count($pr2Ids);
37
38 16
        $limit = min($pr1Count, $pr2Count);
39
40 16
        for ($i = 0; $i < $limit; $i++) {
41 16
            $pr1IdVal = $pr1Ids[$i]->getValue();
42 16
            $pr2IdVal = $pr2Ids[$i]->getValue();
43
44 16
            if ($pr1IdVal == $pr2IdVal) {
45 10
                continue;
46
            }
47
48 10
            return $this->comparePreReleaseIdentifierValues($pr1IdVal, $pr2IdVal);
49
        }
50
51 6
        return $pr1Count - $pr2Count;
52
    }
53
54 10
    private function comparePreReleaseIdentifierValues($pr1IdVal, $pr2IdVal)
55
    {
56 10
        $pr1IsAlpha = ctype_alpha($pr1IdVal);
57 10
        $pr2IsAlpha = ctype_alpha($pr2IdVal);
58
59 10
        if ($pr1IsAlpha && !$pr2IsAlpha) {
60 2
            return 1;
61
        }
62
63 8
        if ($pr2IsAlpha && !$pr1IsAlpha) {
64
            return -1;
65
        }
66
67 8
        if (ctype_digit($pr1IdVal) && ctype_digit($pr2IdVal)) {
68 2
            return (int) $pr1IdVal - (int) $pr2IdVal;
69
        }
70
71 6
        return strcmp($pr1IdVal, $pr2IdVal);
72
    }
73
}
74