VersionEngine   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 12

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
cbo 12
dl 0
loc 82
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 1
A parseVersion() 0 12 2
A parseRange() 0 12 2
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;
13
14
use ptlis\SemanticVersion\Comparator\ComparatorFactory;
15
use ptlis\SemanticVersion\Parse\LogicalOperatorProcessor;
16
use ptlis\SemanticVersion\Parse\RangeParser\BranchParser;
17
use ptlis\SemanticVersion\Parse\RangeParser\CaretRangeParser;
18
use ptlis\SemanticVersion\Parse\RangeParser\ComparatorVersionParser;
19
use ptlis\SemanticVersion\Parse\RangeParser\HyphenatedRangeParser;
20
use ptlis\SemanticVersion\Parse\RangeParser\TildeRangeParser;
21
use ptlis\SemanticVersion\Parse\RangeParser\WildcardRangeParser;
22
use ptlis\SemanticVersion\Parse\VersionParser;
23
use ptlis\SemanticVersion\Parse\VersionRangeParser;
24
use ptlis\SemanticVersion\Parse\VersionTokenizer;
25
use ptlis\SemanticVersion\Version\Label\LabelBuilder;
26
use ptlis\SemanticVersion\Version\VersionInterface;
27
use ptlis\SemanticVersion\VersionRange\VersionRangeInterface;
28
29
/**
30
 * Simple class to provide version parsing with good defaults.
31
 */
32
final class VersionEngine
0 ignored issues
show
Complexity introduced by
The class VersionEngine has a coupling between objects value of 19. Consider to reduce the number of dependencies under 13.
Loading history...
33
{
34
    /** @var VersionTokenizer */
35
    private $tokenizer;
36
37
    /** @var VersionRangeParser */
38
    private $versionRangeParser;
39
40
    /** @var VersionParser */
41
    private $versionParser;
42
43
44
    /**
45
     * Constructor.
46
     */
47 7
    public function __construct()
48
    {
49 7
        $this->versionParser = new VersionParser(new LabelBuilder());
50 7
        $comparatorFactory = new ComparatorFactory();
51
52
        $matcherList = [
53 7
            new CaretRangeParser($this->versionParser, $comparatorFactory->get('>='), $comparatorFactory->get('<')),
54 7
            new TildeRangeParser($this->versionParser, $comparatorFactory->get('>='), $comparatorFactory->get('<')),
55 7
            new WildcardRangeParser($this->versionParser, $comparatorFactory->get('>='), $comparatorFactory->get('<')),
56 7
            new BranchParser($this->versionParser, $comparatorFactory->get('>='), $comparatorFactory->get('<')),
57 7
            new ComparatorVersionParser($comparatorFactory, $this->versionParser),
58 7
            new HyphenatedRangeParser(
59 7
                $this->versionParser,
60 7
                $comparatorFactory->get('>='),
61 7
                $comparatorFactory->get('<'),
62 7
                $comparatorFactory->get('<=')
63 7
            )
64 7
        ];
65
66 7
        $this->tokenizer = new VersionTokenizer();
67 7
        $this->versionRangeParser = new VersionRangeParser(new LogicalOperatorProcessor(), $matcherList);
68 7
    }
69
70
    /**
71
     * Parse a semantic version string into an object implementing VersionInterface.
72
     *
73
     * @param string $versionString
74
     *
75
     * @throws \InvalidArgumentException When version string is invalid.
76
     *
77
     * @return VersionInterface
78
     */
79 3
    public function parseVersion($versionString)
80
    {
81 3
        $tokenList = $this->tokenizer->tokenize($versionString);
82
83
        try {
84 3
            $version = $this->versionParser->parse($tokenList);
85 3
        } catch (\RuntimeException $e) {
86 2
            throw new \InvalidArgumentException('"' . $versionString . '" is not a valid semantic version number', $e->getCode(), $e);
87
        }
88
89 1
        return $version;
90
    }
91
92
    /**
93
     * Parse a version range & return an object implementing VersionRangeInterface that encodes those rules.
94
     *
95
     * @param string $rangeString
96
     *
97
     * @throws \InvalidArgumentException When version range string is invalid.
98
     *
99
     * @return VersionRangeInterface
100
     */
101 4
    public function parseRange($rangeString)
102
    {
103 4
        $tokenList = $this->tokenizer->tokenize($rangeString);
104
105
        try {
106 4
            $range = $this->versionRangeParser->parseRange($tokenList);
107 4
        } catch (\RuntimeException $e) {
108 1
            throw new \InvalidArgumentException('"' . $rangeString . '" is not a valid version range', $e->getCode(), $e);
109
        }
110
111 3
        return $range;
112
    }
113
}
114