Completed
Push — master ( d366a2...854d4a )
by brian
01:55
created

ChunkBySeparator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 39
c 0
b 0
f 0
wmc 5
cbo 1
ccs 13
cts 13
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B chunk() 0 27 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;
13
14
/**
15
 * Trait implementing method to chunk token lists into multiple smaller lists by a specified token.
16
 */
17
trait ChunkBySeparator
18
{
19
    /**
20
     * Chuck the tokens, splitting on the specified token types.
21
     *
22
     * @param Token[] $tokenList
23
     * @param string[] $separatorList One of Token class constants
24
     * @param bool $includeSeparator Whether to include an array containing just the separator that split the tokens.
25
     *
26
     * @return Token[][]
27
     */
28 43
    private function chunk(
29
        array $tokenList,
30
        $separatorList = [Token::DASH_SEPARATOR],
31
        $includeSeparator = false
32
    ) {
33 43
        $chunkList = [];
34 43
        $accumulator = [];
35
36
        // Split token stream by dash separators
37 43
        for ($i = 0; $i < count($tokenList); $i++) {
38 43
            if (in_array($tokenList[$i]->getType(), $separatorList)) {
39 23
                $chunkList[] = $accumulator;
40 23
                if ($includeSeparator) {
41 6
                    $chunkList[] = [$tokenList[$i]];
42
                }
43 23
                $accumulator = [];
44
            } else {
45 43
                $accumulator[] = $tokenList[$i];
46
            }
47
        }
48
49 43
        if (count($accumulator)) {
50 43
            $chunkList[] = $accumulator;
51
        }
52
53 43
        return $chunkList;
54
    }
55
}