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

Tokenizer   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 142
Duplicated Lines 5.63 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.08%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 4
dl 8
loc 142
ccs 58
cts 61
cp 0.9508
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A tokenize() 0 4 1
A tokenizeInternal() 0 17 4
A extractGlobalVariable() 0 19 4
B extractGlobalVariableValue() 0 23 9
A ignoreComment() 8 8 3
A extractRecord() 0 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}