VersionRangeParser   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
cbo 4
dl 0
loc 82
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A parseRange() 0 20 4
A attemptParse() 0 12 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;
13
14
use ptlis\SemanticVersion\Parse\RangeParser\RangeParserInterface;
15
use ptlis\SemanticVersion\VersionRange\VersionRangeInterface;
16
17
/**
18
 * Parser accepting array of tokens and returning an array of comparators & versions.
19
 */
20
final class VersionRangeParser
21
{
22
    use ChunkBySeparator;
23
24
    /** @var LogicalOperatorProcessor */
25
    private $logicalOperatorProcessor;
26
27
    /** @var RangeParserInterface[] */
28
    private $rangeParserList;
29
30
    /** @var string[] Array of tokens representing logical operators */
31
    private $operatorTokenList = [
32
        Token::LOGICAL_AND,
33
        Token::LOGICAL_OR
34
    ];
35
36
37
    /**
38
     * Constructor
39
     *
40
     * @param LogicalOperatorProcessor $logicalOperatorProcessor
41
     * @param RangeParserInterface[] $rangeParserList
42
     */
43 32
    public function __construct(
44
        LogicalOperatorProcessor $logicalOperatorProcessor,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $logicalOperatorProcessor exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
45
        array $rangeParserList
46
    ) {
47 32
        $this->logicalOperatorProcessor = $logicalOperatorProcessor;
48 32
        $this->rangeParserList = $rangeParserList;
49 32
    }
50
51
    /**
52
     * Parse a version range.
53
     *
54
     * @param Token[] $tokenList
55
     *
56
     * @return VersionRangeInterface
57
     */
58 32
    public function parseRange(array $tokenList)
59
    {
60 32
        $realResultList = [];
61 32
        $tokenClusterList = $this->chunk($tokenList, $this->operatorTokenList);
62 32
        foreach ($tokenClusterList as $tokenCluster) {
63 32
            $parsed = $this->attemptParse($tokenCluster);
64
65 32
            if (is_null($parsed)) {
66 7
                if (in_array($tokenCluster[0]->getType(), $this->operatorTokenList)) {
67 6
                    $realResultList[] = $tokenCluster[0];
68 6
                } else {
69 1
                    throw new \RuntimeException('Unable to parse version string');
70
                }
71 6
            } else {
72 31
                $realResultList[] = $parsed;
73
            }
74 31
        }
75
76 31
        return $this->logicalOperatorProcessor->run($realResultList);
77
    }
78
79
    /**
80
     * Attempt to parse the token list as a version range into an object implementing VersionRangeInterface
81
     *
82
     * Iterates through the provided range parsers checking to see if they can parse the token list. If they can then we
83
     * call the parse method and return a version range object, otherwise return null.
84
     *
85
     * @param Token[] $tokenList
86
     *
87
     * @return VersionRangeInterface|null
88
     */
89 32
    private function attemptParse(array $tokenList)
90
    {
91 32
        $parsed = null;
92 32
        foreach ($this->rangeParserList as $rangeParser) {
93 32
            if ($rangeParser->canParse($tokenList)) {
94 31
                $parsed = $rangeParser->parse($tokenList);
95 31
                break;
96
            }
97 32
        }
98
99 32
        return $parsed;
100
    }
101
}
102