Conditions | 16 |
Paths | 12 |
Total Lines | 78 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
166 | private function parse() |
||
167 | { |
||
168 | if (!empty($this->chars)) { |
||
169 | return; |
||
170 | } |
||
171 | |||
172 | $len = \strlen($this->raw); |
||
173 | $inside = false; // are we "inside" of evaluating a valid UTF-8 char? |
||
174 | $invalid = false; |
||
175 | |||
176 | for ($offset = 0; $offset < $len; $offset++) { |
||
177 | $char = $this->raw{$offset}; |
||
178 | $ord = \ord($char); |
||
179 | |||
180 | if ($inside === false) { |
||
181 | $bytes = self::charLength($ord); |
||
182 | |||
183 | if ($bytes > 1 && $offset + $bytes <= $len && $invalid === false) { |
||
184 | // valid UTF-8 multibyte start |
||
185 | $inside = true; |
||
186 | $cache = $char; |
||
187 | $ordcache = ($ord & self::$spec[$bytes]['mask']) << (6 * ($bytes - 1)); |
||
188 | $originOffset = $offset; |
||
189 | } elseif ($ord < self::$spec[2]['start']) { |
||
190 | // ASCII 7-bit char |
||
191 | $this->chars[] = [$char, $ord]; |
||
192 | } else { |
||
193 | // either C0/C1 block or higher; map from cp1252 to utf8 or just convert |
||
194 | $ord = (isset(self::$winc1umap[$ord])) ? self::$winc1umap[$ord] : $ord; |
||
195 | $this->chars[] = [self::cpToUtf8Char($ord), $ord]; |
||
196 | $invalid = false; |
||
197 | } |
||
198 | continue; |
||
199 | } |
||
200 | |||
201 | // $inside === true, i.e. *should be* continuation character |
||
202 | if (($ord & 0b11000000) !== 0b10000000) { |
||
203 | // actually, it's not one, so now the whole UTF-8 char is invalid |
||
204 | // go back and force it to parse as ISO or 1252 |
||
205 | $inside = false; |
||
206 | $invalid = true; |
||
207 | $offset = $originOffset - 1; |
||
208 | continue; |
||
209 | } |
||
210 | |||
211 | // put this byte's data where it needs to go |
||
212 | $ordcache |= ($ord & 0b00111111) << (6 * ($bytes - 1 - ($offset - $originOffset))); |
||
213 | $cache .= $char; |
||
214 | |||
215 | if ($originOffset + ($bytes - 1) === $offset) { |
||
216 | // we're done parsing this char, now let's verify |
||
217 | $inside = false; |
||
218 | |||
219 | // check for overlong, surrogate, too large, BOM, or C0/C1 |
||
220 | $overlong = ($ordcache < self::$spec[$bytes]['start']); |
||
221 | $surrogate = ($ordcache & 0xFFFFF800 === 0xD800); |
||
222 | $toobig = ($ordcache > 0x10FFFF); |
||
223 | |||
224 | if ($overlong || $surrogate || $toobig) { |
||
225 | $invalid = true; |
||
226 | $offset = $originOffset - 1; |
||
227 | continue; |
||
228 | } |
||
229 | |||
230 | if ($ordcache === 0xFEFF) { // BOM |
||
231 | if ($originOffset !== 0) { |
||
232 | // if not at beginning, store as word joiner U+2060 |
||
233 | $this->chars[] = [\chr(0xE2) . \chr(0x81) . \chr(0xA0), 0x2060]; |
||
234 | } |
||
235 | // otherwise discard |
||
236 | continue; |
||
237 | } |
||
238 | |||
239 | // verification passed, now store it |
||
240 | $this->chars[] = [$cache, $ordcache]; |
||
241 | } |
||
242 | } |
||
243 | } |
||
244 | } |
||
245 |