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

StateBuilder   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
c 1
b 0
f 0
lcom 0
cbo 1
dl 0
loc 36
ccs 17
cts 17
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B buildStates() 0 28 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