Total Complexity | 46 |
Total Lines | 229 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like SimpleParser 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 SimpleParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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 |
||
272 | } |
||
273 | } |
||
274 |