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

src/Parse/RangeMatcher/ComparatorVersionParser.php (1 issue)

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\Parse\RangeMatcher;
13
14
use ptlis\SemanticVersion\Comparator\ComparatorFactory;
15
use ptlis\SemanticVersion\Parse\Token;
16
use ptlis\SemanticVersion\Parse\VersionParser;
17
use ptlis\SemanticVersion\VersionRange\ComparatorVersion;
18
use ptlis\SemanticVersion\VersionRange\VersionRangeInterface;
19
20
/**
21
 * Comparator versions store a comparator & version specifying part of a version range.
22
 */
23
final class ComparatorVersionParser implements RangeParserInterface
24
{
25
    use ChunkByDash;
26
27
    /** @var ComparatorFactory */
28
    private $comparatorFactory;
29
30
    /** @var VersionParser */
31
    private $versionParser;
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param ComparatorFactory $comparatorFactory
37
     * @param VersionParser $versionParser
38
     */
39 7
    public function __construct(
40
        ComparatorFactory $comparatorFactory,
41
        VersionParser $versionParser
42
    ) {
43 7
        $this->comparatorFactory = $comparatorFactory;
44 7
        $this->versionParser = $versionParser;
45 7
    }
46
47
    /**
48
     * Returns true if the tokens can be parsed as a ComparatorVersion.
49
     *
50
     * @param Token[] $tokenList
51
     *
52
     * @return boolean
53
     */
54 7
    public function canParse(array $tokenList)
55
    {
56
        return (
57 7
            $this->versionParser->canParse($tokenList)
58
            || (
59 6
                $this->comparatorFactory->isComparator($tokenList[0]->getValue())
60 7
                && $this->versionParser->canParse(array_slice($tokenList, 1))
61
            )
62
        );
63
    }
64
65
    /**
66
     * Build a ComparatorVersion representing the comparator & version.
67
     *
68
     * @param Token[] $tokenList
69
     *
70
     * @return VersionRangeInterface
71
     */
72 6
    public function parse(array $tokenList)
73
    {
74 6
        if (!$this->canParse($tokenList)) {
75 1
            throw new \RuntimeException('Invalid comparator (>1.3.0) version range');
76
        }
77
78
        // Default to equality comparator
79 5
        $comparator = $this->comparatorFactory->get('=');
80
81
        // Prefixed comparator, create comparator instance and remove from token list
82 5
        if ($this->comparatorFactory->isComparator($tokenList[0]->getValue())) {
83 4
            $comparator = $this->comparatorFactory->get($tokenList[0]->getValue());
84 4
            $tokenList = array_slice($tokenList, 1);
85
        }
86
87 5
        return new ComparatorVersion(
88
            $comparator,
0 ignored issues
show
It seems like $comparator 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...
89 5
            $this->versionParser->parse($tokenList)
90
        );
91
    }
92
}
93