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

src/VersionEngine.php (2 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\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
33
{
34
    /**
35
     * @var VersionTokenizer
36
     */
37
    private $tokenizer;
38
39
    /**
40
     * @var VersionParser
41
     */
42
    private $parser;
43
44
45
    /**
46
     * Constructor.
47
     */
48
    public function __construct()
49
    {
50
        $comparatorFactory = new ComparatorFactory();
51
        $versionBuilder = new VersionBuilder(new LabelBuilder());
52
53
        $wildcardParser = new WildcardRangeParser(
54
            $comparatorFactory->get('>='),
55
            $comparatorFactory->get('<')
56
        );
57
58
        $matcherList = [
59
            new CaretRangeParser(
60
                $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...
61
                $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
            ),
63
            new TildeRangeParser($wildcardParser),
64
            $wildcardParser,
65
            new BranchParser($wildcardParser),
66
            new ComparatorVersionParser(
67
                $comparatorFactory,
68
                $versionBuilder
69
            ),
70
            new HyphenatedRangeParser(
71
                $versionBuilder,
72
                $comparatorFactory->get('>='),
73
                $comparatorFactory->get('<'),
74
                $comparatorFactory->get('<=')
75
            )
76
        ];
77
78
        $this->tokenizer = new VersionTokenizer();
79
        $this->parser = new VersionParser($matcherList);
80
    }
81
82
    /**
83
     * Parse a semantic version string into an object implementing VersionInterface.
84
     *
85
     * @todo Hacky - create a better method for handling this.
86
     *
87
     * @param string $versionString
88
     *
89
     * @return VersionInterface
90
     */
91
    public function parseVersion($versionString)
92
    {
93
        $tokenList = $this->tokenizer->tokenize($versionString);
94
95
        $range = $this->parser->parseRange($tokenList);
96
97
        if (!($range instanceof ComparatorVersion)) {
98
            throw new \InvalidArgumentException(
99
                '"' . $versionString . '" is not a valid semantic version number'
100
            );
101
        }
102
103
        return $range->getVersion();
104
    }
105
106
    /**
107
     * Parse a version range & return an object implementing VersionRangeInterface that encodes those rules.
108
     *
109
     * @param string $rangeString
110
     *
111
     * @return VersionRangeInterface
112
     */
113
    public function parseRange($rangeString)
114
    {
115
        $tokenList = $this->tokenizer->tokenize($rangeString);
116
117
        return $this->parser->parseRange($tokenList);
118
    }
119
}
120