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