Completed
Push — master ( 578d3f...fb2c6d )
by brian
02:16
created

WildcardRangeParser::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
crap 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\Parse\RangeMatcher;
13
14
use ptlis\SemanticVersion\Comparator\ComparatorInterface;
15
use ptlis\SemanticVersion\Parse\Token;
16
use ptlis\SemanticVersion\Parse\VersionParser;
17
use ptlis\SemanticVersion\Version\Version;
18
use ptlis\SemanticVersion\VersionRange\ComparatorVersion;
19
use ptlis\SemanticVersion\VersionRange\LogicalAnd;
20
use ptlis\SemanticVersion\VersionRange\VersionRangeInterface;
21
22
/**
23
 * Parser for wildcard ranges.
24
 *
25
 * Behaviour of wildcard ranges is described @ https://getcomposer.org/doc/articles/versions.md#wildcard
26
 */
27
final class WildcardRangeParser implements RangeParserInterface
28
{
29
    use ParseSimpleRange;
30
31
    /** @var VersionParser */
32
    private $versionParser;
33
34
    /** @var ComparatorInterface */
35
    private $greaterOrEqualTo;
36
37
    /** @var ComparatorInterface */
38
    private $lessThan;
39
40
41
    /**
42
     * Constructor.
43
     *
44
     * @param VersionParser $versionParser
45
     * @param ComparatorInterface $greaterOrEqualTo
46
     * @param ComparatorInterface $lessThan
47
     */
48 3
    public function __construct(
49
        VersionParser $versionParser,
50
        ComparatorInterface $greaterOrEqualTo,
51
        ComparatorInterface $lessThan
52
    ) {
53 3
        $this->versionParser = $versionParser;
54 3
        $this->greaterOrEqualTo = $greaterOrEqualTo;
55 3
        $this->lessThan = $lessThan;
56 3
    }
57
58
    /**
59
     * Returns true if the tokens represent a wildcard range.
60
     *
61
     * @param Token[] $tokenList
62
     *
63
     * @return boolean
64
     */
65 3
    public function canParse(array $tokenList)
66
    {
67
        return (
68 3
            count($tokenList) > 0
69 3
            && Token::WILDCARD_DIGITS === $tokenList[count($tokenList) - 1]->getType()
70 3
            && $this->versionParser->canParse(array_slice($tokenList, 0, count($tokenList) - 1))
71
        );
72
    }
73
74
    /**
75
     * Build a comparator representing the wildcard range.
76
     *
77
     * @param Token[] $tokenList
78
     *
79
     * @return VersionRangeInterface
80
     */
81 3
    public function parse(array $tokenList)
82
    {
83 3
        if (!$this->canParse($tokenList)) {
84 1
            throw new \RuntimeException('Invalid version');
85
        }
86
87 2
        return $this->parseSimpleVersionRange(
88 2
            $this->versionParser,
89 2
            $this->greaterOrEqualTo,
90 2
            $this->lessThan,
91 2
            array_slice($tokenList, 0, count($tokenList) - 1) // Remove trailing '*' when parsing as version
92
        );
93
    }
94
}
95