SimpleParser::parse()   F
last analyzed

Complexity

Conditions 36
Paths 3

Size

Total Lines 124
Code Lines 104

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 36
eloc 104
nc 3
nop 0
dl 0
loc 124
rs 3.3333
c 0
b 0
f 0

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
declare(strict_types=1);
4
5
/**
6
 * soluble-japha / PHPJavaBridge driver client.
7
 *
8
 * Refactored version of phpjababridge's Java.inc file compatible
9
 * with php java bridge 6.2
10
 *
11
 *
12
 * @credits   http://php-java-bridge.sourceforge.net/pjb/
13
 *
14
 * @see      http://github.com/belgattitude/soluble-japha
15
 *
16
 * @author Jost Boekemeier
17
 * @author Vanvelthem Sébastien (refactoring and fixes from original implementation)
18
 * @license   MIT
19
 *
20
 * The MIT License (MIT)
21
 * Copyright (c) 2014-2017 Jost Boekemeier
22
 * Permission is hereby granted, free of charge, to any person obtaining a copy
23
 * of this software and associated documentation files (the "Software"), to deal
24
 * in the Software without restriction, including without limitation the rights
25
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26
 * copies of the Software, and to permit persons to whom the Software is
27
 * furnished to do so, subject to the following conditions:
28
 *
29
 * The above copyright notice and this permission notice shall be included in
30
 * all copies or substantial portions of the Software.
31
 *
32
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
38
 * THE SOFTWARE.
39
 */
40
41
namespace Soluble\Japha\Bridge\Driver\Pjb62;
42
43
class SimpleParser implements ParserInterface
44
{
45
    /**
46
     * @var int
47
     */
48
    public $SLEN = 256;
49
    public $handler;
50
    public $tag;
51
    public $buf;
52
    public $len;
53
    public $s;
54
    public $type;
55
56
    public $BEGIN = 0;
57
    public $KEY = 1;
58
    public $VAL = 2;
59
    public $ENTITY = 3;
60
    public $VOJD = 5;
61
    public $END = 6;
62
    public $level = 0;
63
    public $eor = 0;
64
    public $in_dquote;
65
    public $eot = false;
66
    public $pos = 0;
67
    public $c = 0;
68
    public $i = 0;
69
    public $i0 = 0;
70
    public $e;
71
72
    /**
73
     * @param Client $client
74
     */
75
    public function __construct(Client $client)
76
    {
77
        $this->handler = $client;
78
        $this->tag = [new ParserTag(), new ParserTag(), new ParserTag()];
79
        $this->len = $this->SLEN;
80
        $this->s = str_repeat(' ', $this->SLEN);
81
        $this->type = $this->VOJD;
82
    }
83
84
    private function RESET(): void
85
    {
86
        $this->type = $this->VOJD;
87
        $this->level = 0;
88
        $this->eor = 0;
89
        $this->in_dquote = false;
90
        $this->i = 0;
91
        $this->i0 = 0;
92
    }
93
94
    /**
95
     * @param string $c
96
     */
97
    protected function APPEND($c)
98
    {
99
        if ($this->i >= $this->len - 1) {
100
            $this->s = str_repeat($this->s, 2);
101
            $this->len *= 2;
102
        }
103
        $this->s[$this->i++] = $c;
104
    }
105
106
    protected function CALL_BEGIN()
107
    {
108
        $pt = &$this->tag[1]->strings;
109
        $st = &$this->tag[2]->strings;
110
        $t = &$this->tag[0]->strings[0];
111
        $name = $t->string[$t->off];
112
        $n = $this->tag[2]->n;
113
        $ar = [];
114
        for ($i = 0; $i < $n; ++$i) {
115
            $ar[$pt[$i]->getString()] = $st[$i]->getString();
116
        }
117
        $this->handler->begin($name, $ar);
118
    }
119
120
    private function CALL_END()
121
    {
122
        $t = &$this->tag[0]->strings[0];
123
        $name = $t->string[$t->off];
124
        $this->handler->end($name);
125
    }
126
127
    protected function PUSH($t)
128
    {
129
        $str = &$this->tag[$t]->strings;
130
        $n = &$this->tag[$t]->n;
131
        $this->s[$this->i] = '|';
132
        if (!isset($str[$n])) {
133
            $str[$n] = new ParserString();
134
        }
135
        $str[$n]->string = &$this->s;
136
        $str[$n]->off = $this->i0;
137
        $str[$n]->length = $this->i - $this->i0;
138
        ++$this->tag[$t]->n;
139
        $this->APPEND('|');
140
        $this->i0 = $this->i;
141
    }
142
143
    public function parse(): void
144
    {
145
        $java_recv_size = $this->handler->getParam('JAVA_RECV_SIZE');
146
        while ($this->eor == 0) {
147
            if ($this->c >= $this->pos) {
148
                $this->buf = $this->handler->read($java_recv_size);
149
                if (null === $this->buf || strlen($this->buf) == 0) {
150
                    $this->handler->protocol->handler->shutdownBrokenConnection('protocol error. Check the back end log for OutOfMemoryErrors.');
151
                }
152
                $this->pos = strlen($this->buf);
153
                if ($this->pos == 0) {
154
                    break;
155
                }
156
                $this->c = 0;
157
            }
158
            switch (($ch = $this->buf[$this->c])) {
159
                case '<':
160
                    if ($this->in_dquote) {
161
                        $this->APPEND($ch);
162
                        break;
163
                    }
164
                    ++$this->level;
165
                    $this->type = $this->BEGIN;
166
                    break;
167
                case '\t':
168
                case '\f':
169
                case '\n':
170
                case '\r':
171
                case ' ':
172
                    if ($this->in_dquote) {
173
                        $this->APPEND($ch);
174
                        break;
175
                    }
176
                    if ($this->type == $this->BEGIN) {
177
                        $this->PUSH($this->type);
178
                        $this->type = $this->KEY;
179
                    }
180
                    break;
181
                case '=':
182
                    if ($this->in_dquote) {
183
                        $this->APPEND($ch);
184
                        break;
185
                    }
186
                    $this->PUSH($this->type);
187
                    $this->type = $this->VAL;
188
                    break;
189
                case '/':
190
                    if ($this->in_dquote) {
191
                        $this->APPEND($ch);
192
                        break;
193
                    }
194
                    if ($this->type == $this->BEGIN) {
195
                        $this->type = $this->END;
196
                        --$this->level;
197
                    }
198
                    --$this->level;
199
                    $this->eot = true;
200
                    break;
201
                case '>':
202
                    if ($this->in_dquote) {
203
                        $this->APPEND($ch);
204
                        break;
205
                    }
206
                    if ($this->type == $this->END) {
207
                        $this->PUSH($this->BEGIN);
208
                        $this->CALL_END();
209
                    } else {
210
                        if ($this->type == $this->VAL) {
211
                            $this->PUSH($this->type);
212
                        }
213
                        $this->CALL_BEGIN();
214
                    }
215
                    $this->tag[0]->n = $this->tag[1]->n = $this->tag[2]->n = 0;
216
                    $this->i0 = $this->i = 0;
217
                    $this->type = $this->VOJD;
218
                    if ($this->level == 0) {
219
                        $this->eor = 1;
220
                    }
221
                    break;
222
                case ';':
223
                    if ($this->type == $this->ENTITY) {
224
                        switch ($this->s[$this->e + 1]) {
225
                            case 'l':
226
                                $this->s[$this->e] = '<';
227
                                $this->i = $this->e + 1;
228
                                break;
229
                            case 'g':
230
                                $this->s[$this->e] = '>';
231
                                $this->i = $this->e + 1;
232
                                break;
233
                            case 'a':
234
                                $this->s[$this->e] = ($this->s[$this->e + 2] == 'm' ? '&' : '\'');
235
                                $this->i = $this->e + 1;
236
                                break;
237
                            case 'q':
238
                                $this->s[$this->e] = '"';
239
                                $this->i = $this->e + 1;
240
                                break;
241
                            default:
242
                                $this->APPEND($ch);
243
                        }
244
                        $this->type = $this->VAL;
245
                    } else {
246
                        $this->APPEND($ch);
247
                    }
248
                    break;
249
                case '&':
250
                    $this->type = $this->ENTITY;
251
                    $this->e = $this->i;
252
                    $this->APPEND($ch);
253
                    break;
254
                case '"':
255
                    $this->in_dquote = !$this->in_dquote;
256
                    if (!$this->in_dquote && $this->type == $this->VAL) {
257
                        $this->PUSH($this->type);
258
                        $this->type = $this->KEY;
259
                    }
260
                    break;
261
                default:
262
                    $this->APPEND($ch);
263
            }
264
            ++$this->c;
265
        }
266
        $this->RESET();
267
    }
268
269
    public function getData(string $str): string
270
    {
271
        return $str;
272
    }
273
}
274