Completed
Push — master ( 475c0f...a498f5 )
by brian
03:00
created

VersionEngine   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 12

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
cbo 12
dl 0
loc 98
ccs 43
cts 43
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 33 1
A parseVersion() 0 18 3
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\Matcher\BranchParser;
16
use ptlis\SemanticVersion\Parse\Matcher\CaretRangeParser;
17
use ptlis\SemanticVersion\Parse\Matcher\ComparatorVersionParser;
18
use ptlis\SemanticVersion\Parse\Matcher\HyphenatedRangeParser;
19
use ptlis\SemanticVersion\Parse\Matcher\TildeRangeParser;
20
use ptlis\SemanticVersion\Parse\Matcher\WildcardRangeParser;
21
use ptlis\SemanticVersion\Parse\VersionParser;
22
use ptlis\SemanticVersion\Parse\VersionTokenizer;
23
use ptlis\SemanticVersion\Version\Label\LabelBuilder;
24
use ptlis\SemanticVersion\Version\VersionBuilder;
25
use ptlis\SemanticVersion\Version\VersionInterface;
26
use ptlis\SemanticVersion\VersionRange\ComparatorVersion;
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 18. Consider to reduce the number of dependencies under 13.
Loading history...
33
{
34
    /** @var VersionTokenizer */
35
    private $tokenizer;
36
37
    /** @var VersionParser */
38
    private $parser;
39
40
41
    /**
42
     * Constructor.
43
     */
44 7
    public function __construct()
45
    {
46 7
        $comparatorFactory = new ComparatorFactory();
47 7
        $versionBuilder = new VersionBuilder(new LabelBuilder());
48
49 7
        $wildcardParser = new WildcardRangeParser(
50 7
            $comparatorFactory->get('>='),
0 ignored issues
show
Bug introduced by
It seems like $comparatorFactory->get('>=') can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
51 7
            $comparatorFactory->get('<')
0 ignored issues
show
Bug introduced by
It seems like $comparatorFactory->get('<') can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
52 7
        );
53
54
        $matcherList = [
55 7
            new CaretRangeParser(
56 7
                $comparatorFactory->get('>='),
0 ignored issues
show
Bug introduced by
It seems like $comparatorFactory->get('>=') can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
57 7
                $comparatorFactory->get('<')
0 ignored issues
show
Bug introduced by
It seems like $comparatorFactory->get('<') can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
58 7
            ),
59 7
            new TildeRangeParser($wildcardParser),
60 7
            $wildcardParser,
61 7
            new BranchParser($wildcardParser),
62 7
            new ComparatorVersionParser(
63 7
                $comparatorFactory,
64
                $versionBuilder
65 7
            ),
66 7
            new HyphenatedRangeParser(
67 7
                $versionBuilder,
68 7
                $comparatorFactory->get('>='),
0 ignored issues
show
Bug introduced by
It seems like $comparatorFactory->get('>=') can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
69 7
                $comparatorFactory->get('<'),
0 ignored issues
show
Bug introduced by
It seems like $comparatorFactory->get('<') can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
70 7
                $comparatorFactory->get('<=')
0 ignored issues
show
Bug introduced by
It seems like $comparatorFactory->get('<=') can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
71 7
            )
72 7
        ];
73
74 7
        $this->tokenizer = new VersionTokenizer();
75 7
        $this->parser = new VersionParser($matcherList);
76 7
    }
77
78
    /**
79
     * Parse a semantic version string into an object implementing VersionInterface.
80
     *
81
     * @todo Hacky - create a better method for handling this.
82
     *
83
     * @param string $versionString
84
     *
85
     * @throws \InvalidArgumentException When version string is invalid.
86
     *
87
     * @return VersionInterface
88
     */
89 3
    public function parseVersion($versionString)
90
    {
91 3
        $tokenList = $this->tokenizer->tokenize($versionString);
92
93
        try {
94 3
            $range = $this->parser->parseRange($tokenList);
95 3
        } catch (\RuntimeException $e) {
96 1
            throw new \InvalidArgumentException('"' . $versionString . '" is not a valid semantic version number', $e->getCode(), $e);
97
        }
98
99 2
        if (!($range instanceof ComparatorVersion)) {
100 1
            throw new \InvalidArgumentException(
101 1
                '"' . $versionString . '" is not a valid semantic version number'
102 1
            );
103
        }
104
105 1
        return $range->getVersion();
106
    }
107
108
    /**
109
     * Parse a version range & return an object implementing VersionRangeInterface that encodes those rules.
110
     *
111
     * @param string $rangeString
112
     *
113
     * @throws \InvalidArgumentException When version range string is invalid.
114
     *
115
     * @return VersionRangeInterface
116
     */
117 4
    public function parseRange($rangeString)
118
    {
119 4
        $tokenList = $this->tokenizer->tokenize($rangeString);
120
121
        try {
122 4
            $range = $this->parser->parseRange($tokenList);
123 4
        } catch (\RuntimeException $e) {
124 1
            throw new \InvalidArgumentException('"' . $rangeString . '" is not a valid version range', $e->getCode(), $e);
125
        }
126
127 3
        return $range;
128
    }
129
}
130