Completed
Push — master ( d12968...2a6550 )
by Harry
02:36
created

Parser::parse()   C

Complexity

Conditions 17
Paths 19

Size

Total Lines 68
Code Lines 49

Duplication

Lines 10
Ratio 14.71 %

Code Coverage

Tests 53
CRAP Score 17

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 10
loc 68
ccs 53
cts 53
cp 1
rs 5.7068
cc 17
eloc 49
nc 19
nop 1
crap 17

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Graze\CsvToken;
4
5
use ArrayIterator;
6
use Graze\CsvToken\Tokeniser\Token;
7
use Graze\CsvToken\ValueParser\Value;
8
use Graze\CsvToken\ValueParser\ValueParserInterface;
9
use Iterator;
10
use RuntimeException;
11
12
class Parser implements ParserInterface
13
{
14
    /** @var ValueParserInterface[] */
15
    private $valueParsers = [];
16
17
    /**
18
     * Parser constructor.
19
     *
20
     * @param ValueParserInterface[] $valueParsers
21
     */
22 12
    public function __construct(array $valueParsers = [])
23
    {
24 12
        array_map([$this, 'addValueParser'], $valueParsers);
25 12
    }
26
27
    /**
28
     * @param Iterator $tokens
29
     *
30
     * @return Iterator Iterator of csv line Iterators
31
     */
32 8
    public function parse(Iterator $tokens)
33
    {
34 8
        $value = new Value($this->valueParsers);
35 8
        $row = new ArrayIterator();
36
37 8
        $tokens->rewind();
38
        /** @var Token $token */
39 8
        $token = $tokens->current();
40 8
        while (!is_null($token)) {
41 7
            switch (true) {
42 7
                case $token->getType() == Token::T_ESCAPE:
43 3
                    $tokens->next();
44 3
                    $token = $tokens->current();
45 3
                    if (is_null($token)) {
46 1
                        throw new RuntimeException(
47
                            "Invalid CSV: The csv strings final character is an escape character"
48 1
                        );
49
                    }
50 2
                    $value->addContent($token->getContent());
51 2
                    break;
52
53 7
                case ($value->isInQuotes() && $token->getType() == Token::T_DOUBLE_QUOTE):
54 1
                    $value->addContent(substr($token->getContent(), 0, $token->getLength() / 2));
55 1
                    break;
56
57 7
                case $token->getType() == Token::T_QUOTE:
58 6
                    $value->setInQuotes(!$value->isInQuotes());
59 6
                    break;
60
61 7
                case $value->isInQuotes():
62 6
                    $value->addContent($token->getContent());
63 6
                    break;
64
65 7
                case ($value->isEmpty()
66 7
                    && !$value->isInQuotes()
67 7
                    && !$value->wasQuoted()
68 7
                    && $token->getType() == Token::T_NULL):
69 4
                    $value->addContent($token->getContent());
70 4
                    $value->setIsNull();
71 4
                    break;
72
73 7 View Code Duplication
                case (!$value->isInQuotes() && $token->getType() == Token::T_DELIMITER):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74 6
                    $row->append($value->getValue());
75 6
                    $value->reset();
76 6
                    break;
77
78 6 View Code Duplication
                case (!$value->isInQuotes() && $token->getType() == Token::T_NEW_LINE):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79 2
                    $row->append($value->getValue());
80 2
                    $value->reset();
81 2
                    yield $row;
82 2
                    $row = new ArrayIterator();
83 2
                    break;
84
85 6
                default:
86 6
                    $value->addContent($token->getContent());
87 5
                    break;
88 6
            }
89
90 7
            $tokens->next();
91 7
            $token = $tokens->current();
92 7
        }
93
94 6
        if (!$value->isEmpty()) {
95 5
            $row->append($value->getValue());
96 5
        }
97
98 6
        yield new ArrayIterator($row);
99 6
    }
100
101
    /**
102
     * @param ValueParserInterface $valueParser
103
     */
104 4
    public function addValueParser(ValueParserInterface $valueParser)
105
    {
106 4
        $this->valueParsers[] = $valueParser;
107 4
    }
108
109
    /**
110
     * @param ValueParserInterface $valueParser
111
     */
112 1
    public function removeValueParser(ValueParserInterface $valueParser)
113
    {
114 1
        $index = array_search($valueParser, $this->valueParsers, true);
115 1
        if ($index !== false) {
116 1
            unset($this->valueParsers[$index]);
117 1
        }
118 1
    }
119
120
    /**
121
     * @param ValueParserInterface $valueParser
122
     *
123
     * @return bool
124
     */
125 3
    public function hasValueParser(ValueParserInterface $valueParser)
126
    {
127 3
        return in_array($valueParser, $this->valueParsers);
128
    }
129
}
130