Completed
Push — master ( 8d7014...4fa85a )
by brian
05:27
created

LabelBuilder   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 2

Test Coverage

Coverage 52.94%

Importance

Changes 0
Metric Value
wmc 11
cbo 2
dl 0
loc 126
ccs 18
cts 34
cp 0.5294
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setName() 0 7 1
A setVersion() 0 7 1
B build() 0 25 4
B buildFromTokens() 0 32 4
1
<?php
2
3
/**
4
 * @copyright   (c) 2014-2017 brian ridley
5
 * @author      brian ridley <[email protected]>
6
 * @license     http://opensource.org/licenses/MIT MIT
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ptlis\SemanticVersion\Version\Label;
13
14
use ptlis\SemanticVersion\Parse\Token;
15
16
/**
17
 * Immutable builder for label instances.
18
 */
19
final class LabelBuilder
20
{
21
    /**
22
     * @var string
23
     */
24
    private $name;
25
26
    /**
27
     * @var int|null
28
     */
29
    private $version;
30
31
32
    /**
33
     * Constructor.
34
     *
35
     * @param string $name
36
     * @param int|null $version
37
     */
38 5
    public function __construct($name = '', $version = null)
39
    {
40 5
        $this->name = $name;
41 5
        $this->version = $version;
42 5
    }
43
44
    /**
45
     * Set label name.
46
     *
47
     * @param string $name
48
     *
49
     * @return $this
50
     */
51 4
    public function setName($name)
52
    {
53 4
        return new LabelBuilder(
54
            $name,
55 4
            $this->version
56
        );
57
    }
58
59
    /**
60
     * Set label version.
61
     *
62
     * @param int|null $version
63
     *
64
     * @return $this
65
     */
66 3
    public function setVersion($version)
67
    {
68 3
        return new LabelBuilder(
69 3
            $this->name,
70
            $version
71
        );
72
    }
73
74
    /**
75
     * Build a label from the provided specification
76
     *
77
     * @return LabelInterface
78
     */
79 5
    public function build()
80
    {
81
        $labelMap = [
82 5
            'alpha' => Label::PRECEDENCE_ALPHA,
83
            'beta' => Label::PRECEDENCE_BETA,
84
            'rc' => Label::PRECEDENCE_RC
85
        ];
86
87
        // No Label present or a dev label (these are a special-case for packagist - a version like 1.0.x-dev is
88
        // equivalent to 1.0.* in  conventional notation - there is additional semantics attached to this but it's not
89
        // important for our purposes
90 5
        if (!strlen($this->name) || 'dev' === $this->name) {
91 1
            $label = new Label(Label::PRECEDENCE_ABSENT);
92
93
        // Alpha, Beta & RC standard labels
94 4
        } elseif (array_key_exists($this->name, $labelMap)) {
95 3
            $label = new Label($labelMap[$this->name], $this->version);
96
97
        // Anything else is a miscellaneous 'dev' label
98
        } else {
99 1
            $label = new Label(Label::PRECEDENCE_DEV, $this->version, $this->name);
100
        }
101
102 5
        return $label;
103
    }
104
105
    /**
106
     * Build a label from a token list.
107
     *
108
     * @param Token[] $labelTokenList
109
     *
110
     * @return LabelInterface|null
111
     */
112
    public function buildFromTokens(array $labelTokenList)
113
    {
114
        $label = null;
115
116
        switch (count($labelTokenList)) {
1 ignored issue
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
117
118
            // No label
119
            case 0:
120
                // Do Nothing
121
                break;
122
123
            // Version string part only
124
            case 1:
125
                $label = $this
126
                    ->setName($labelTokenList[0]->getValue())
127
                    ->build();
128
                break;
129
130
            // Label version
131
            case 3:
132
                $label = $this
133
                    ->setName($labelTokenList[0]->getValue())
134
                    ->setVersion($labelTokenList[2]->getValue())
135
                    ->build();
136
                break;
137
138
            default:
139
                throw new \RuntimeException('Invalid version');
140
        }
141
142
        return $label;
143
    }
144
}
145