Completed
Push — master ( d291df...b0e701 )
by brian
01:56
created

src/Parse/RangeMatcher/ChunkByDash.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\RangeMatcher;
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 11
    private function chunk(array $tokenList, $separator = Token::DASH_SEPARATOR)
30
    {
31 11
        $tokenListCount = count($tokenList);
32 11
        $chunkedList = [];
33 11
        $accumulator = [];
34
35 11
        for ($i = 0; $i < $tokenListCount; $i++) {
36 11
            $token = $tokenList[$i];
37
38
            // Accumulate until we hit a dash
39 11
            if ($separator !== $token->getType()) {
40 11
                $accumulator[] = $token;
41
1 ignored issue
show
Blank line found at end of control structure
Loading history...
42
            } else {
43 10
                $chunkedList[] = $accumulator;
44 10
                $accumulator = [];
45
            }
46
        }
47
48 11
        if (count($accumulator)) {
49 11
            $chunkedList[] = $accumulator;
50
        }
51
52 11
        return $chunkedList;
53
    }
54
}