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

BranchParser::canParse()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 5
nop 1
crap 5
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 composer branch ranges.
21
 *
22
 * Behaviour of branch ranges is described @ https://getcomposer.org/doc/02-libraries.md#branches
23
 */
24
final class BranchParser 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 tokens can be parsed as a Packagist-style branch
57
     *
58
     * @param Token[] $tokenList
59
     *
60
     * @return boolean
61
     */
62 2
    public function canParse(array $tokenList)
63
    {
64 2
        $tokenListCount = count($tokenList);
65
66
        return (
67 2
            $tokenListCount >= 3
68 2
            && Token::WILDCARD_DIGITS === $tokenList[$tokenListCount - 3]->getType()
69 2
            && Token::DASH_SEPARATOR === $tokenList[$tokenListCount - 2]->getType()
70 2
            && Token::LABEL_STRING === $tokenList[$tokenListCount - 1]->getType()
71 2
            && $this->versionParser->canParse(array_slice($tokenList, 0, count($tokenList) - 3))
72
        );
73
    }
74
75
    /**
76
     * Build a ComparatorVersion representing the branch.
77
     *
78
     * @param Token[] $tokenList
79
     *
80
     * @return VersionRangeInterface
81
     */
82 2
    public function parse(array $tokenList)
83
    {
84 2
        if (!$this->canParse($tokenList)) {
85 1
            throw new \RuntimeException('Invalid version');
86
        }
87
88 1
        return $this->parseSimpleVersionRange(
89 1
            $this->versionParser,
90 1
            $this->greaterOrEqualTo,
91 1
            $this->lessThan,
92 1
            array_slice($tokenList, 0, count($tokenList) - 3) // Remove x-branch suffix
93
        );
94
    }
95
}
96