Completed
Push — master ( 67e2e1...c8a456 )
by brian
02:04
created

src/VersionEngine.php (11 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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