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