Test Setup Failed
Pull Request — master (#21)
by Alexandr
02:04
created

Tokenizer::extractGlobalVariableValue()   B

Complexity

Conditions 9
Paths 0

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 9.0164

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 16
cts 17
cp 0.9412
rs 8.0555
c 0
b 0
f 0
cc 9
nc 0
nop 1
crap 9.0164
1
<?php
2
/**
3
 * @author: Viskov Sergey
4
 * @date  : 8/4/15
5
 * @time  : 9:31 PM
6
 */
7
8
namespace LTDBeget\dns;
9
10
use LTDBeget\ascii\AsciiChar;
11
use LTDBeget\dns\record\Record;
12
use LTDBeget\stringstream\StringStream;
13
14
/**
15
 * Class Tokenizer
16
 *
17
 * @package LTDBeget\dns
18
 */
19
final class Tokenizer
20
{
21
    /**
22
     * @var StringStream
23
     */
24
    private $stream;
25
    /**
26
     * Result of tokenize input string
27
     *
28
     * @var array
29
     */
30
    private $tokens = [];
31
    /**
32
     * @var string
33
     */
34
    private $ttl;
35
    /**
36
     * @var null
37
     */
38
    private $origin;
39
    /**
40
     * @var array
41
     */
42
    private $allowedGlobalVariables = [
43
        'origin' => true,
44
        'ttl'    => true
45
    ];
46
47
    /**
48
     * @var int
49
     */
50
    private $recordsAmmount = 0;
51
52
    /**
53
     * Tokenizer constructor.
54
     *
55
     * @param string $string
56
     */
57 7
    private function __construct(string $string)
58
    {
59 7
        $this->stream = new StringStream($string);
60 7
    }
61
62
    /**
63
     * @param string $plainData
64
     * @return array
65
     */
66 7
    public static function tokenize(string $plainData) : array
67
    {
68 7
        return (new self($plainData))->tokenizeInternal()->tokens;
69
    }
70
71
    /**
72
     * @return Tokenizer
73
     */
74 7
    private function tokenizeInternal() : Tokenizer
75
    {
76
        do {
77 7
            $this->stream->ignoreWhitespace();
78 7
            if ($this->stream->currentAscii()->is(AsciiChar::DOLLAR)) {
79 3
                $this->stream->next();
80 3
                $this->extractGlobalVariable();
81 7
            } elseif ($this->stream->currentAscii()->is(AsciiChar::SEMICOLON)) {
82 5
                $this->ignoreComment();
83
            } else {
84 7
                $this->extractRecord();
85 6
                $this->stream->ignoreWhitespace();
86
            }
87 6
        } while (!$this->stream->isEnd());
88
89 6
        return $this;
90
    }
91
92 3
    private function extractGlobalVariable()
93
    {
94 3
        $variableName = '';
95
        start:
96 3
        if ($this->stream->currentAscii()->isLetter()) {
97 3
            $variableName .= $this->stream->current();
98 3
            $this->stream->next();
99 3
            goto start;
100 3
        } elseif ($this->stream->currentAscii()->isHorizontalSpace()) {
101 3
            $variableName = mb_strtolower($variableName);
102 3
            if (!array_key_exists($variableName, $this->allowedGlobalVariables)) {
103
                throw new SyntaxErrorException($this->stream);
104
            }
105 3
            $this->stream->ignoreHorizontalSpace();
106 3
            $this->extractGlobalVariableValue($variableName);
107
        } else {
108
            throw new SyntaxErrorException($this->stream);
109
        }
110 3
    }
111
112
    /**
113
     * @param string $variableName
114
     */
115 3
    private function extractGlobalVariableValue(string $variableName)
116
    {
117 3
        $this->{$variableName} = '';
118
        start:
119 3
        $char = $this->stream->currentAscii();
120 3
        $this->{$variableName} .= '';
121 3
        if ($char->isLetter() ||
122 3
            $char->isDigit() ||
123 3
            $char->is(AsciiChar::UNDERSCORE) ||
124 3
            $char->is(AsciiChar::DOT) ||
125 3
            $char->is(AsciiChar::HYPHEN) ||
126 3
            $char->is(AsciiChar::AT_SYMBOL) ||
127 3
            $char->is(AsciiChar::ASTERISK)
128
        ) {
129 3
            $this->{$variableName} .= $this->stream->current();
130 3
            $this->stream->next();
131 3
            goto start;
132 3
        } elseif ($char->isWhiteSpace()) {
133 3
            return;
134
        } else {
135
            throw new SyntaxErrorException($this->stream);
136
        }
137
    }
138
139 5 View Code Duplication
    private function ignoreComment()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
140
    {
141
        start:
142 5
        if (!$this->stream->currentAscii()->isVerticalSpace() && !$this->stream->isEnd()) {
143 5
            $this->stream->next();
144 5
            goto start;
145
        }
146 5
    }
147
148 7
    private function extractRecord()
149
    {
150 7
        $isFirst = $this->recordsAmmount === 0;
151 7
        if(! $isFirst ) {
152 5
            $lastParsedRecord = end($this->tokens);
153 5
            $previousName = $lastParsedRecord['NAME'];
154
        } else {
155 7
            $previousName = NULL;
156
        }
157 7
        $this->tokens[] = (new Record($this->stream, $this->origin, $this->ttl, $isFirst, $previousName))->tokenize();
158 6
        $this->recordsAmmount++;
159
    }
160
}