Completed
Push — master ( a498f5...578d3f )
by brian
02:12
created

ChunkByDash   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
cbo 1
dl 0
loc 36
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B chunk() 0 25 4
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\Matcher;
13
14
use ptlis\SemanticVersion\Parse\Token;
15
16
/**
17
 * Trait implementing method to chunk tokens by dash seperator.
18
 */
19
trait ChunkByDash
20
{
21
    /**
22
     * Chuck the tokens, splitting on hyphen.
23
     *
24
     * @param Token[] $tokenList
25
     * @param string $separator One of Token class constants
26
     *
27
     * @return Token[][]
28
     */
29 17
    private function chunk(array $tokenList, $separator = Token::DASH_SEPARATOR)
30
    {
31 17
        $tokenListCount = count($tokenList);
32 17
        $chunkedList = [];
33 17
        $accumulator = [];
34
35 17
        for ($i = 0; $i < $tokenListCount; $i++) {
36 17
            $token = $tokenList[$i];
37
38
            // Accumulate until we hit a dash
39 17
            if ($separator !== $token->getType()) {
40 17
                $accumulator[] = $token;
41
1 ignored issue
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
42 17
            } else {
43 11
                $chunkedList[] = $accumulator;
44 11
                $accumulator = [];
45
            }
46 17
        }
47
48 17
        if (count($accumulator)) {
49 17
            $chunkedList[] = $accumulator;
50 17
        }
51
52 17
        return $chunkedList;
53
    }
54
}