ChunkBySeparator::chunk()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 15
cts 15
cp 1
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 15
nc 6
nop 2
crap 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;
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 Array of Token class constants
24
     *
25
     * @return Token[][]
26
     */
27 43
    private function chunk(
28
        array $tokenList,
29
        array $separatorList
30
    ) {
31 43
        $chunkList = [];
32 43
        $accumulator = [];
33
34
        // Split token stream by dash separators
35 43
        for ($i = 0; $i < count($tokenList); $i++) {
36 43
            if (in_array($tokenList[$i]->getType(), $separatorList)) {
37 23
                $chunkList[] = $accumulator;
38 23
                $chunkList[] = [$tokenList[$i]];
39 23
                $accumulator = [];
40 23
            } else {
41 43
                $accumulator[] = $tokenList[$i];
42
            }
43 43
        }
44
45 43
        if (count($accumulator)) {
46 43
            $chunkList[] = $accumulator;
47 43
        }
48
49 43
        return $chunkList;
50
    }
51
}
52