Completed
Push — master ( bd7f7f...85213f )
by brian
01:56
created

ChunkByDash::chunk()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 14
cts 14
cp 1
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 12
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\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
        $chunkList = [];
32 11
        $accumulator = [];
33
34
        // Split token stream by dash separators
35 11
        for ($i = 0; $i < count($tokenList); $i++) {
1 ignored issue
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
36 11
            if ($separator !== $tokenList[$i]->getType()) {
37 11
                $accumulator[] = $tokenList[$i];
38 11
            } else {
39 10
                $chunkList[] = $accumulator;
40 10
                $accumulator = [];
41
            }
42 11
        }
43
44 11
        if (count($accumulator)) {
45 11
            $chunkList[] = $accumulator;
46 11
        }
47
48 11
        return $chunkList;
49
    }
50
}