Completed
Push — master ( 383c64...d37ce4 )
by brian
03:46
created

TildeRangeParser   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
cbo 3
dl 0
loc 68
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A canParse() 0 8 3
A parse() 0 13 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 tilde ranges.
21
 *
22
 * Behaviour of caret ranges is described @ https://getcomposer.org/doc/articles/versions.md#tilde
23
 */
24
final class TildeRangeParser 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 2
    public function __construct(
46
        VersionParser $versionParser,
47
        ComparatorInterface $greaterOrEqualTo,
48
        ComparatorInterface $lessThan
49
    ) {
50 2
        $this->versionParser = $versionParser;
51 2
        $this->greaterOrEqualTo = $greaterOrEqualTo;
52 2
        $this->lessThan = $lessThan;
53 2
    }
54
55
    /**
56
     * Returns true if the provided tokens represent a tilde range.
57
     *
58
     * @param Token[] $tokenList
59
     *
60
     * @return boolean
61
     */
62 2
    public function canParse(array $tokenList)
63
    {
64
        return (
65 2
            count($tokenList) > 0
66 2
            && Token::TILDE_RANGE === $tokenList[0]->getType()
67 2
            && $this->versionParser->canParse(array_slice($tokenList, 1))
68
        );
69
    }
70
71
    /**
72
     * Build a comparator version representing the tilde range.
73
     *
74
     * @param Token[] $tokenList
75
     *
76
     * @return VersionRangeInterface
77
     */
78 2
    public function parse(array $tokenList)
79
    {
80 2
        if (!$this->canParse($tokenList)) {
81 1
            throw new \RuntimeException('Invalid version');
82
        }
83
84 1
        return $this->parseSimpleVersionRange(
85 1
            $this->versionParser,
86 1
            $this->greaterOrEqualTo,
87 1
            $this->lessThan,
88 1
            array_slice($tokenList, 1) // Remove prefix tilde
89
        );
90
    }
91
}
92