ComparatorVersionParser::parse()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 1
crap 3
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\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
    /** @var ComparatorFactory */
26
    private $comparatorFactory;
27
28
    /** @var VersionParser */
29
    private $versionParser;
30
31
    /**
32
     * Constructor.
33
     *
34
     * @param ComparatorFactory $comparatorFactory
35
     * @param VersionParser $versionParser
36
     */
37 7
    public function __construct(
38
        ComparatorFactory $comparatorFactory,
39
        VersionParser $versionParser
40
    ) {
41 7
        $this->comparatorFactory = $comparatorFactory;
42 7
        $this->versionParser = $versionParser;
43 7
    }
44
45
    /**
46
     * Returns true if the tokens can be parsed as a ComparatorVersion.
47
     *
48
     * @param Token[] $tokenList
49
     *
50
     * @return boolean
51
     */
52 7
    public function canParse(array $tokenList)
53
    {
54
        return (
55 7
            $this->versionParser->canParse($tokenList)
56
            || (
57 6
                $this->comparatorFactory->isComparator($tokenList[0]->getValue())
58 6
                && $this->versionParser->canParse(array_slice($tokenList, 1))
59 4
            )
60 7
        );
61
    }
62
63
    /**
64
     * Build a ComparatorVersion representing the comparator & version.
65
     *
66
     * @param Token[] $tokenList
67
     *
68
     * @return VersionRangeInterface
69
     */
70 6
    public function parse(array $tokenList)
71
    {
72 6
        if (!$this->canParse($tokenList)) {
73 1
            throw new \RuntimeException('Invalid comparator (>1.3.0) version range');
74
        }
75
76
        // Default to equality comparator
77 5
        $comparator = $this->comparatorFactory->get('=');
78
79
        // Prefixed comparator, create comparator instance and remove from token list
80 5
        if ($this->comparatorFactory->isComparator($tokenList[0]->getValue())) {
81 4
            $comparator = $this->comparatorFactory->get($tokenList[0]->getValue());
82 4
            $tokenList = array_slice($tokenList, 1);
83 4
        }
84
85 5
        return new ComparatorVersion(
86 5
            $comparator,
87 5
            $this->versionParser->parse($tokenList)
88 5
        );
89
    }
90
}
91