Completed
Push — master ( b6cc57...033934 )
by Harry
8s
created

StateBuilder::buildStates()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 28
ccs 17
cts 17
cp 1
rs 8.8571
cc 1
eloc 17
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Graze\CsvToken\Tokeniser;
4
5
trait StateBuilder
6
{
7
    /**
8
     * @param array $types
9
     *
10
     * @return State The default starting state
11
     */
12
    public function buildStates(array $types)
13
    {
14
        $getTypes = function ($tokenMask) use ($types) {
15 19
            return array_filter($types, function ($type) use ($tokenMask) {
16 19
                return $type & $tokenMask;
17 19
            });
18 19
        };
19
20 19
        $any = new State($getTypes(State::S_ANY_TOKENS));
21 19
        $inQuote = new State($getTypes(State::S_IN_QUOTE_TOKENS));
22 19
        $inEscape = new State($getTypes(State::S_IN_ESCAPE_TOKENS));
23 19
        $inQuoteEscape = new State($getTypes(State::S_IN_QUOTE_ESCAPE_TOKENS));
24
25
        // generate state mapping
26 19
        $any->addStateTarget(Token::T_ANY & ~Token::T_QUOTE & ~Token::T_ESCAPE, $any);
27 19
        $any->addStateTarget(Token::T_QUOTE, $inQuote);
28 19
        $any->addStateTarget(Token::T_ESCAPE, $inEscape);
29
30 19
        $inQuote->addStateTarget(Token::T_CONTENT | Token::T_DOUBLE_QUOTE, $inQuote);
31 19
        $inQuote->addStateTarget(Token::T_QUOTE, $any);
32 19
        $inQuote->addStateTarget(Token::T_ESCAPE, $inQuoteEscape);
33
34 19
        $inEscape->addStateTarget(Token::T_CONTENT, $any);
35
36 19
        $inQuoteEscape->addStateTarget(Token::T_CONTENT, $inQuote);
37
38 19
        return $any;
39
    }
40
}
41