Passed
Push — master ( 852e13...6d4e0e )
by Edward
03:37
created

Utf8TokenMatcher   F

Complexity

Total Complexity 60

Size/Duplication

Total Lines 266
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 195
dl 0
loc 266
rs 3.6
c 3
b 0
f 0
wmc 60

1 Method

Rating   Name   Duplication   Size   Complexity  
F match() 0 263 60

How to fix   Complexity   

Complex Class

Complex classes like Utf8TokenMatcher often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Utf8TokenMatcher, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * Unicode UTF-8 token matcher.
5
 *
6
 * Auto-generated file, please don't edit manually.
7
 * Generated by UniLex.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Remorhaz\UniLex\Unicode\Grammar;
13
14
use Remorhaz\UniLex\IO\CharBufferInterface;
15
use Remorhaz\UniLex\Lexer\TokenFactoryInterface;
16
use Remorhaz\UniLex\Lexer\TokenMatcherTemplate;
17
18
class Utf8TokenMatcher extends TokenMatcherTemplate
19
{
20
21
    public function match(CharBufferInterface $buffer, TokenFactoryInterface $tokenFactory): bool
22
    {
23
        $context = $this->createContext($buffer, $tokenFactory);
24
        goto state1;
25
26
        state1:
27
        if ($context->getBuffer()->isEnd()) {
28
            goto error;
29
        }
30
        $char = $context->getBuffer()->getSymbol();
31
        if (0x00 <= $char && $char <= 0x7F) {
32
            $context->getBuffer()->nextSymbol();
33
            // [\x00-\x7F]
34
            $context
35
                ->setNewToken(TokenType::SYMBOL)
36
                ->setTokenAttribute(TokenAttribute::UNICODE_CHAR, $char);
37
38
            return true;
39
        }
40
        if (0xC0 <= $char && $char <= 0xDF) {
41
            $context->getBuffer()->nextSymbol();
42
            goto state3;
43
        }
44
        if (0xE0 <= $char && $char <= 0xEF) {
45
            $context->getBuffer()->nextSymbol();
46
            goto state4;
47
        }
48
        if (0xF0 <= $char && $char <= 0xF7) {
49
            $context->getBuffer()->nextSymbol();
50
            goto state5;
51
        }
52
        if (0xF8 <= $char && $char <= 0xFB) {
53
            $context->getBuffer()->nextSymbol();
54
            goto state6;
55
        }
56
        if (0xFC == $char || 0xFD == $char) {
57
            $context->getBuffer()->nextSymbol();
58
            goto state7;
59
        }
60
        goto error;
61
62
        state3:
63
        if ($context->getBuffer()->isEnd()) {
64
            goto error;
65
        }
66
        $char = $context->getBuffer()->getSymbol();
67
        if (0x80 <= $char && $char <= 0xBF) {
68
            $context->getBuffer()->nextSymbol();
69
            // [\xC0-\xDF][\x80-\xBF]
70
            $charList = array_slice($context->getSymbolList(), -2);
71
            $symbol = ($charList[0] & 0x1F) << 6;
72
            $symbol |= ($charList[1] & 0x3F);
73
            $context
74
                ->setNewToken(TokenType::SYMBOL)
75
                ->setTokenAttribute(TokenAttribute::UNICODE_CHAR, $symbol);
76
77
            return true;
78
        }
79
        goto error;
80
81
        state4:
82
        if ($context->getBuffer()->isEnd()) {
83
            goto error;
84
        }
85
        $char = $context->getBuffer()->getSymbol();
86
        if (0x80 <= $char && $char <= 0xBF) {
87
            $context->getBuffer()->nextSymbol();
88
            goto state20;
89
        }
90
        goto error;
91
92
        state5:
93
        if ($context->getBuffer()->isEnd()) {
94
            goto error;
95
        }
96
        $char = $context->getBuffer()->getSymbol();
97
        if (0x80 <= $char && $char <= 0xBF) {
98
            $context->getBuffer()->nextSymbol();
99
            goto state17;
100
        }
101
        goto error;
102
103
        state6:
104
        if ($context->getBuffer()->isEnd()) {
105
            goto error;
106
        }
107
        $char = $context->getBuffer()->getSymbol();
108
        if (0x80 <= $char && $char <= 0xBF) {
109
            $context->getBuffer()->nextSymbol();
110
            goto state13;
111
        }
112
        goto error;
113
114
        state7:
115
        if ($context->getBuffer()->isEnd()) {
116
            goto error;
117
        }
118
        $char = $context->getBuffer()->getSymbol();
119
        if (0x80 <= $char && $char <= 0xBF) {
120
            $context->getBuffer()->nextSymbol();
121
            goto state8;
122
        }
123
        goto error;
124
125
        state8:
126
        if ($context->getBuffer()->isEnd()) {
127
            goto error;
128
        }
129
        $char = $context->getBuffer()->getSymbol();
130
        if (0x80 <= $char && $char <= 0xBF) {
131
            $context->getBuffer()->nextSymbol();
132
            goto state9;
133
        }
134
        goto error;
135
136
        state9:
137
        if ($context->getBuffer()->isEnd()) {
138
            goto error;
139
        }
140
        $char = $context->getBuffer()->getSymbol();
141
        if (0x80 <= $char && $char <= 0xBF) {
142
            $context->getBuffer()->nextSymbol();
143
            goto state10;
144
        }
145
        goto error;
146
147
        state10:
148
        if ($context->getBuffer()->isEnd()) {
149
            goto error;
150
        }
151
        $char = $context->getBuffer()->getSymbol();
152
        if (0x80 <= $char && $char <= 0xBF) {
153
            $context->getBuffer()->nextSymbol();
154
            goto state11;
155
        }
156
        goto error;
157
158
        state11:
159
        if ($context->getBuffer()->isEnd()) {
160
            goto error;
161
        }
162
        $char = $context->getBuffer()->getSymbol();
163
        if (0x80 <= $char && $char <= 0xBF) {
164
            $context->getBuffer()->nextSymbol();
165
            // [\xFC-\xFD][\x80-\xBF]{5}
166
            $charList = array_slice($context->getSymbolList(), -6);
167
            $symbol = ($charList[0] & 0x01) << 30;
168
            $symbol |= ($charList[1] & 0x03) << 24;
169
            $symbol |= ($charList[2] & 0x3F) << 18;
170
            $symbol |= ($charList[3] & 0x3F) << 12;
171
            $symbol |= ($charList[4] & 0x3F) << 6;
172
            $symbol |= ($charList[5] & 0x3F);
173
            $context
174
                ->setNewToken(TokenType::SYMBOL)
175
                ->setTokenAttribute(TokenAttribute::UNICODE_CHAR, $symbol);
176
177
            return true;
178
        }
179
        goto error;
180
181
        state13:
182
        if ($context->getBuffer()->isEnd()) {
183
            goto error;
184
        }
185
        $char = $context->getBuffer()->getSymbol();
186
        if (0x80 <= $char && $char <= 0xBF) {
187
            $context->getBuffer()->nextSymbol();
188
            goto state14;
189
        }
190
        goto error;
191
192
        state14:
193
        if ($context->getBuffer()->isEnd()) {
194
            goto error;
195
        }
196
        $char = $context->getBuffer()->getSymbol();
197
        if (0x80 <= $char && $char <= 0xBF) {
198
            $context->getBuffer()->nextSymbol();
199
            goto state15;
200
        }
201
        goto error;
202
203
        state15:
204
        if ($context->getBuffer()->isEnd()) {
205
            goto error;
206
        }
207
        $char = $context->getBuffer()->getSymbol();
208
        if (0x80 <= $char && $char <= 0xBF) {
209
            $context->getBuffer()->nextSymbol();
210
            // [\xF8-\xFB][\x80-\xBF]{4}
211
            $charList = array_slice($context->getSymbolList(), -5);
212
            $symbol = ($charList[0] & 0x03) << 24;
213
            $symbol |= ($charList[1] & 0x3F) << 18;
214
            $symbol |= ($charList[2] & 0x3F) << 12;
215
            $symbol |= ($charList[3] & 0x3F) << 6;
216
            $symbol |= ($charList[4] & 0x3F);
217
            $context
218
                ->setNewToken(TokenType::SYMBOL)
219
                ->setTokenAttribute(TokenAttribute::UNICODE_CHAR, $symbol);
220
221
            return true;
222
        }
223
        goto error;
224
225
        state17:
226
        if ($context->getBuffer()->isEnd()) {
227
            goto error;
228
        }
229
        $char = $context->getBuffer()->getSymbol();
230
        if (0x80 <= $char && $char <= 0xBF) {
231
            $context->getBuffer()->nextSymbol();
232
            goto state18;
233
        }
234
        goto error;
235
236
        state18:
237
        if ($context->getBuffer()->isEnd()) {
238
            goto error;
239
        }
240
        $char = $context->getBuffer()->getSymbol();
241
        if (0x80 <= $char && $char <= 0xBF) {
242
            $context->getBuffer()->nextSymbol();
243
            // [\xF0-\xF7][\x80-\xBF]{3}
244
            $charList = array_slice($context->getSymbolList(), -4);
245
            $symbol = ($charList[0] & 0x07) << 18;
246
            $symbol |= ($charList[1] & 0x3F) << 12;
247
            $symbol |= ($charList[2] & 0x3F) << 6;
248
            $symbol |= ($charList[3] & 0x3F);
249
            $context
250
                ->setNewToken(TokenType::SYMBOL)
251
                ->setTokenAttribute(TokenAttribute::UNICODE_CHAR, $symbol);
252
253
            return true;
254
        }
255
        goto error;
256
257
        state20:
258
        if ($context->getBuffer()->isEnd()) {
259
            goto error;
260
        }
261
        $char = $context->getBuffer()->getSymbol();
262
        if (0x80 <= $char && $char <= 0xBF) {
263
            $context->getBuffer()->nextSymbol();
264
            // [\xE0-\xEF][\x80-\xBF]{2}
265
            $charList = array_slice($context->getSymbolList(), -3);
266
            $symbol = ($charList[0] & 0x0F) << 12;
267
            $symbol |= ($charList[1] & 0x3F) << 6;
268
            $symbol |= ($charList[2] & 0x3F);
269
            $context
270
                ->setNewToken(TokenType::SYMBOL)
271
                ->setTokenAttribute(TokenAttribute::UNICODE_CHAR, $symbol);
272
273
            return true;
274
        }
275
        goto error;
276
277
        error:
278
        if ($context->getBuffer()->isEnd()) {
279
            return false;
280
        }
281
        $context->getBuffer()->nextSymbol();
282
        $context->setNewToken(TokenType::INVALID_BYTES);
283
        return true;
284
    }
285
}
286