Total Complexity | 181 |
Total Lines | 732 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Mbstring 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 Mbstring, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
67 | final class Mbstring |
||
68 | { |
||
69 | const MB_CASE_FOLD = PHP_INT_MAX; |
||
70 | |||
71 | private static $encodingList = array('ASCII', 'UTF-8'); |
||
72 | private static $language = 'neutral'; |
||
73 | private static $internalEncoding = 'UTF-8'; |
||
74 | private static $caseFold = array( |
||
75 | array('µ', 'ſ', "\xCD\x85", 'ς', "\xCF\x90", "\xCF\x91", "\xCF\x95", "\xCF\x96", "\xCF\xB0", "\xCF\xB1", "\xCF\xB5", "\xE1\xBA\x9B", "\xE1\xBE\xBE"), |
||
76 | array('μ', 's', 'ι', 'σ', 'β', 'θ', 'φ', 'π', 'κ', 'ρ', 'ε', "\xE1\xB9\xA1", 'ι'), |
||
77 | ); |
||
78 | |||
79 | public static function mb_convert_encoding($s, $toEncoding, $fromEncoding = null) |
||
115 | } |
||
116 | |||
117 | public static function mb_convert_variables($toEncoding, $fromEncoding, &$a = null, &$b = null, &$c = null, &$d = null, &$e = null, &$f = null) |
||
129 | } |
||
130 | |||
131 | public static function mb_decode_mimeheader($s) |
||
132 | { |
||
133 | return iconv_mime_decode($s, 2, self::$internalEncoding); |
||
134 | } |
||
135 | |||
136 | public static function mb_encode_mimeheader($s, $charset = null, $transferEncoding = null, $linefeed = null, $indent = null) |
||
139 | } |
||
140 | |||
141 | public static function mb_decode_numericentity($s, $convmap, $encoding = null) |
||
142 | { |
||
143 | if (null !== $s && !\is_scalar($s) && !(\is_object($s) && \method_exists($s, '__toString'))) { |
||
144 | trigger_error('mb_decode_numericentity() expects parameter 1 to be string, '.\gettype($s).' given', E_USER_WARNING); |
||
145 | |||
146 | return null; |
||
147 | } |
||
148 | |||
149 | if (!\is_array($convmap) || !$convmap) { |
||
150 | return false; |
||
151 | } |
||
152 | |||
153 | if (null !== $encoding && !\is_scalar($encoding)) { |
||
154 | trigger_error('mb_decode_numericentity() expects parameter 3 to be string, '.\gettype($s).' given', E_USER_WARNING); |
||
155 | |||
156 | return ''; // Instead of null (cf. mb_encode_numericentity). |
||
157 | } |
||
158 | |||
159 | $s = (string) $s; |
||
160 | if ('' === $s) { |
||
161 | return ''; |
||
162 | } |
||
163 | |||
164 | $encoding = self::getEncoding($encoding); |
||
165 | |||
166 | if ('UTF-8' === $encoding) { |
||
167 | $encoding = null; |
||
168 | if (!preg_match('//u', $s)) { |
||
169 | $s = @iconv('UTF-8', 'UTF-8//IGNORE', $s); |
||
170 | } |
||
171 | } else { |
||
172 | $s = iconv($encoding, 'UTF-8//IGNORE', $s); |
||
173 | } |
||
174 | |||
175 | $cnt = floor(\count($convmap) / 4) * 4; |
||
176 | |||
177 | for ($i = 0; $i < $cnt; $i += 4) { |
||
178 | // collector_decode_htmlnumericentity ignores $convmap[$i + 3] |
||
179 | $convmap[$i] += $convmap[$i + 2]; |
||
180 | $convmap[$i + 1] += $convmap[$i + 2]; |
||
181 | } |
||
182 | |||
183 | $s = preg_replace_callback('/&#(?:0*([0-9]+)|x0*([0-9a-fA-F]+))(?!&);?/', function (array $m) use ($cnt, $convmap) { |
||
184 | $c = isset($m[2]) ? (int) hexdec($m[2]) : $m[1]; |
||
185 | for ($i = 0; $i < $cnt; $i += 4) { |
||
186 | if ($c >= $convmap[$i] && $c <= $convmap[$i + 1]) { |
||
187 | return Mbstring::mb_chr($c - $convmap[$i + 2]); |
||
188 | } |
||
189 | } |
||
190 | |||
191 | return $m[0]; |
||
192 | }, $s); |
||
193 | |||
194 | if (null === $encoding) { |
||
195 | return $s; |
||
196 | } |
||
197 | |||
198 | return iconv('UTF-8', $encoding.'//IGNORE', $s); |
||
199 | } |
||
200 | |||
201 | public static function mb_encode_numericentity($s, $convmap, $encoding = null, $is_hex = false) |
||
202 | { |
||
203 | if (null !== $s && !\is_scalar($s) && !(\is_object($s) && \method_exists($s, '__toString'))) { |
||
204 | trigger_error('mb_encode_numericentity() expects parameter 1 to be string, '.\gettype($s).' given', E_USER_WARNING); |
||
205 | |||
206 | return null; |
||
207 | } |
||
208 | |||
209 | if (!\is_array($convmap) || !$convmap) { |
||
210 | return false; |
||
211 | } |
||
212 | |||
213 | if (null !== $encoding && !\is_scalar($encoding)) { |
||
214 | trigger_error('mb_encode_numericentity() expects parameter 3 to be string, '.\gettype($s).' given', E_USER_WARNING); |
||
215 | |||
216 | return null; // Instead of '' (cf. mb_decode_numericentity). |
||
217 | } |
||
218 | |||
219 | if (null !== $is_hex && !\is_scalar($is_hex)) { |
||
220 | trigger_error('mb_encode_numericentity() expects parameter 4 to be boolean, '.\gettype($s).' given', E_USER_WARNING); |
||
221 | |||
222 | return null; |
||
223 | } |
||
224 | |||
225 | $s = (string) $s; |
||
226 | if ('' === $s) { |
||
227 | return ''; |
||
228 | } |
||
229 | |||
230 | $encoding = self::getEncoding($encoding); |
||
231 | |||
232 | if ('UTF-8' === $encoding) { |
||
233 | $encoding = null; |
||
234 | if (!preg_match('//u', $s)) { |
||
235 | $s = @iconv('UTF-8', 'UTF-8//IGNORE', $s); |
||
236 | } |
||
237 | } else { |
||
238 | $s = iconv($encoding, 'UTF-8//IGNORE', $s); |
||
239 | } |
||
240 | |||
241 | static $ulenMask = array("\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4); |
||
242 | |||
243 | $cnt = floor(\count($convmap) / 4) * 4; |
||
244 | $i = 0; |
||
245 | $len = \strlen($s); |
||
246 | $result = ''; |
||
247 | |||
248 | while ($i < $len) { |
||
249 | $ulen = $s[$i] < "\x80" ? 1 : $ulenMask[$s[$i] & "\xF0"]; |
||
250 | $uchr = substr($s, $i, $ulen); |
||
251 | $i += $ulen; |
||
252 | $c = self::mb_ord($uchr); |
||
253 | |||
254 | for ($j = 0; $j < $cnt; $j += 4) { |
||
255 | if ($c >= $convmap[$j] && $c <= $convmap[$j + 1]) { |
||
256 | $cOffset = ($c + $convmap[$j + 2]) & $convmap[$j + 3]; |
||
257 | $result .= $is_hex ? sprintf('&#x%X;', $cOffset) : '&#'.$cOffset.';'; |
||
258 | continue 2; |
||
259 | } |
||
260 | } |
||
261 | $result .= $uchr; |
||
262 | } |
||
263 | |||
264 | if (null === $encoding) { |
||
265 | return $result; |
||
266 | } |
||
267 | |||
268 | return iconv('UTF-8', $encoding.'//IGNORE', $result); |
||
269 | } |
||
270 | |||
271 | public static function mb_convert_case($s, $mode, $encoding = null) |
||
272 | { |
||
273 | $s = (string) $s; |
||
274 | if ('' === $s) { |
||
275 | return ''; |
||
276 | } |
||
277 | |||
278 | $encoding = self::getEncoding($encoding); |
||
279 | |||
280 | if ('UTF-8' === $encoding) { |
||
281 | $encoding = null; |
||
282 | if (!preg_match('//u', $s)) { |
||
283 | $s = @iconv('UTF-8', 'UTF-8//IGNORE', $s); |
||
284 | } |
||
285 | } else { |
||
286 | $s = iconv($encoding, 'UTF-8//IGNORE', $s); |
||
287 | } |
||
288 | |||
289 | if (MB_CASE_TITLE == $mode) { |
||
290 | static $titleRegexp = null; |
||
291 | if (null === $titleRegexp) { |
||
292 | $titleRegexp = self::getData('titleCaseRegexp'); |
||
293 | } |
||
294 | $s = preg_replace_callback($titleRegexp, array(__CLASS__, 'title_case'), $s); |
||
295 | } else { |
||
296 | if (MB_CASE_UPPER == $mode) { |
||
297 | static $upper = null; |
||
298 | if (null === $upper) { |
||
299 | $upper = self::getData('upperCase'); |
||
300 | } |
||
301 | $map = $upper; |
||
302 | } else { |
||
303 | if (self::MB_CASE_FOLD === $mode) { |
||
304 | $s = str_replace(self::$caseFold[0], self::$caseFold[1], $s); |
||
305 | } |
||
306 | |||
307 | static $lower = null; |
||
308 | if (null === $lower) { |
||
309 | $lower = self::getData('lowerCase'); |
||
310 | } |
||
311 | $map = $lower; |
||
312 | } |
||
313 | |||
314 | static $ulenMask = array("\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4); |
||
315 | |||
316 | $i = 0; |
||
317 | $len = \strlen($s); |
||
318 | |||
319 | while ($i < $len) { |
||
320 | $ulen = $s[$i] < "\x80" ? 1 : $ulenMask[$s[$i] & "\xF0"]; |
||
321 | $uchr = substr($s, $i, $ulen); |
||
322 | $i += $ulen; |
||
323 | |||
324 | if (isset($map[$uchr])) { |
||
325 | $uchr = $map[$uchr]; |
||
326 | $nlen = \strlen($uchr); |
||
327 | |||
328 | if ($nlen == $ulen) { |
||
329 | $nlen = $i; |
||
330 | do { |
||
331 | $s[--$nlen] = $uchr[--$ulen]; |
||
332 | } while ($ulen); |
||
333 | } else { |
||
334 | $s = substr_replace($s, $uchr, $i - $ulen, $ulen); |
||
335 | $len += $nlen - $ulen; |
||
336 | $i += $nlen - $ulen; |
||
337 | } |
||
338 | } |
||
339 | } |
||
340 | } |
||
341 | |||
342 | if (null === $encoding) { |
||
343 | return $s; |
||
344 | } |
||
345 | |||
346 | return iconv('UTF-8', $encoding.'//IGNORE', $s); |
||
347 | } |
||
348 | |||
349 | public static function mb_internal_encoding($encoding = null) |
||
350 | { |
||
351 | if (null === $encoding) { |
||
352 | return self::$internalEncoding; |
||
353 | } |
||
354 | |||
355 | $encoding = self::getEncoding($encoding); |
||
356 | |||
357 | if ('UTF-8' === $encoding || false !== @iconv($encoding, $encoding, ' ')) { |
||
358 | self::$internalEncoding = $encoding; |
||
359 | |||
360 | return true; |
||
361 | } |
||
362 | |||
363 | return false; |
||
364 | } |
||
365 | |||
366 | public static function mb_language($lang = null) |
||
367 | { |
||
368 | if (null === $lang) { |
||
369 | return self::$language; |
||
370 | } |
||
371 | |||
372 | switch ($lang = strtolower($lang)) { |
||
373 | case 'uni': |
||
374 | case 'neutral': |
||
375 | self::$language = $lang; |
||
376 | |||
377 | return true; |
||
378 | } |
||
379 | |||
380 | return false; |
||
381 | } |
||
382 | |||
383 | public static function mb_list_encodings() |
||
384 | { |
||
385 | return array('UTF-8'); |
||
386 | } |
||
387 | |||
388 | public static function mb_encoding_aliases($encoding) |
||
389 | { |
||
390 | switch (strtoupper($encoding)) { |
||
391 | case 'UTF8': |
||
392 | case 'UTF-8': |
||
393 | return array('utf8'); |
||
394 | } |
||
395 | |||
396 | return false; |
||
397 | } |
||
398 | |||
399 | public static function mb_check_encoding($var = null, $encoding = null) |
||
400 | { |
||
401 | if (null === $encoding) { |
||
402 | if (null === $var) { |
||
403 | return false; |
||
404 | } |
||
405 | $encoding = self::$internalEncoding; |
||
406 | } |
||
407 | |||
408 | return self::mb_detect_encoding($var, array($encoding)) || false !== @iconv($encoding, $encoding, $var); |
||
409 | } |
||
410 | |||
411 | public static function mb_detect_encoding($str, $encodingList = null, $strict = false) |
||
412 | { |
||
413 | if (null === $encodingList) { |
||
414 | $encodingList = self::$encodingList; |
||
415 | } else { |
||
416 | if (!\is_array($encodingList)) { |
||
417 | $encodingList = array_map('trim', explode(',', $encodingList)); |
||
418 | } |
||
419 | $encodingList = array_map('strtoupper', $encodingList); |
||
420 | } |
||
421 | |||
422 | foreach ($encodingList as $enc) { |
||
423 | switch ($enc) { |
||
424 | case 'ASCII': |
||
425 | if (!preg_match('/[\x80-\xFF]/', $str)) { |
||
426 | return $enc; |
||
427 | } |
||
428 | break; |
||
429 | |||
430 | case 'UTF8': |
||
431 | case 'UTF-8': |
||
432 | if (preg_match('//u', $str)) { |
||
433 | return 'UTF-8'; |
||
434 | } |
||
435 | break; |
||
436 | |||
437 | default: |
||
438 | if (0 === strncmp($enc, 'ISO-8859-', 9)) { |
||
439 | return $enc; |
||
440 | } |
||
441 | } |
||
442 | } |
||
443 | |||
444 | return false; |
||
445 | } |
||
446 | |||
447 | public static function mb_detect_order($encodingList = null) |
||
448 | { |
||
449 | if (null === $encodingList) { |
||
450 | return self::$encodingList; |
||
451 | } |
||
452 | |||
453 | if (!\is_array($encodingList)) { |
||
454 | $encodingList = array_map('trim', explode(',', $encodingList)); |
||
455 | } |
||
456 | $encodingList = array_map('strtoupper', $encodingList); |
||
457 | |||
458 | foreach ($encodingList as $enc) { |
||
459 | switch ($enc) { |
||
460 | default: |
||
461 | if (strncmp($enc, 'ISO-8859-', 9)) { |
||
462 | return false; |
||
463 | } |
||
464 | // no break |
||
465 | case 'ASCII': |
||
466 | case 'UTF8': |
||
467 | case 'UTF-8': |
||
468 | } |
||
469 | } |
||
470 | |||
471 | self::$encodingList = $encodingList; |
||
472 | |||
473 | return true; |
||
474 | } |
||
475 | |||
476 | public static function mb_strlen($s, $encoding = null) |
||
477 | { |
||
478 | $encoding = self::getEncoding($encoding); |
||
479 | if ('CP850' === $encoding || 'ASCII' === $encoding) { |
||
480 | return \strlen($s); |
||
481 | } |
||
482 | |||
483 | return @iconv_strlen($s, $encoding); |
||
484 | } |
||
485 | |||
486 | public static function mb_strpos($haystack, $needle, $offset = 0, $encoding = null) |
||
487 | { |
||
488 | $encoding = self::getEncoding($encoding); |
||
489 | if ('CP850' === $encoding || 'ASCII' === $encoding) { |
||
490 | return strpos($haystack, $needle, $offset); |
||
491 | } |
||
492 | |||
493 | $needle = (string) $needle; |
||
494 | if ('' === $needle) { |
||
495 | trigger_error(__METHOD__.': Empty delimiter', E_USER_WARNING); |
||
496 | |||
497 | return false; |
||
498 | } |
||
499 | |||
500 | return iconv_strpos($haystack, $needle, $offset, $encoding); |
||
501 | } |
||
502 | |||
503 | public static function mb_strrpos($haystack, $needle, $offset = 0, $encoding = null) |
||
504 | { |
||
505 | $encoding = self::getEncoding($encoding); |
||
506 | if ('CP850' === $encoding || 'ASCII' === $encoding) { |
||
507 | return strrpos($haystack, $needle, $offset); |
||
508 | } |
||
509 | |||
510 | if ($offset != (int) $offset) { |
||
511 | $offset = 0; |
||
512 | } elseif ($offset = (int) $offset) { |
||
513 | if ($offset < 0) { |
||
514 | $haystack = self::mb_substr($haystack, 0, $offset, $encoding); |
||
515 | $offset = 0; |
||
516 | } else { |
||
517 | $haystack = self::mb_substr($haystack, $offset, 2147483647, $encoding); |
||
518 | } |
||
519 | } |
||
520 | |||
521 | $pos = iconv_strrpos($haystack, $needle, $encoding); |
||
522 | |||
523 | return false !== $pos ? $offset + $pos : false; |
||
524 | } |
||
525 | |||
526 | public static function mb_strtolower($s, $encoding = null) |
||
527 | { |
||
528 | return self::mb_convert_case($s, MB_CASE_LOWER, $encoding); |
||
529 | } |
||
530 | |||
531 | public static function mb_strtoupper($s, $encoding = null) |
||
532 | { |
||
533 | return self::mb_convert_case($s, MB_CASE_UPPER, $encoding); |
||
534 | } |
||
535 | |||
536 | public static function mb_substitute_character($c = null) |
||
537 | { |
||
538 | if (0 === strcasecmp($c, 'none')) { |
||
539 | return true; |
||
540 | } |
||
541 | |||
542 | return null !== $c ? false : 'none'; |
||
543 | } |
||
544 | |||
545 | public static function mb_substr($s, $start, $length = null, $encoding = null) |
||
546 | { |
||
547 | $encoding = self::getEncoding($encoding); |
||
548 | if ('CP850' === $encoding || 'ASCII' === $encoding) { |
||
549 | return substr($s, $start, null === $length ? 2147483647 : $length); |
||
550 | } |
||
551 | |||
552 | if ($start < 0) { |
||
553 | $start = iconv_strlen($s, $encoding) + $start; |
||
554 | if ($start < 0) { |
||
555 | $start = 0; |
||
556 | } |
||
557 | } |
||
558 | |||
559 | if (null === $length) { |
||
560 | $length = 2147483647; |
||
561 | } elseif ($length < 0) { |
||
562 | $length = iconv_strlen($s, $encoding) + $length - $start; |
||
563 | if ($length < 0) { |
||
564 | return ''; |
||
565 | } |
||
566 | } |
||
567 | |||
568 | return (string) iconv_substr($s, $start, $length, $encoding); |
||
569 | } |
||
570 | |||
571 | public static function mb_stripos($haystack, $needle, $offset = 0, $encoding = null) |
||
572 | { |
||
573 | $haystack = self::mb_convert_case($haystack, self::MB_CASE_FOLD, $encoding); |
||
574 | $needle = self::mb_convert_case($needle, self::MB_CASE_FOLD, $encoding); |
||
575 | |||
576 | return self::mb_strpos($haystack, $needle, $offset, $encoding); |
||
577 | } |
||
578 | |||
579 | public static function mb_stristr($haystack, $needle, $part = false, $encoding = null) |
||
584 | } |
||
585 | |||
586 | public static function mb_strrchr($haystack, $needle, $part = false, $encoding = null) |
||
587 | { |
||
588 | $encoding = self::getEncoding($encoding); |
||
589 | if ('CP850' === $encoding || 'ASCII' === $encoding) { |
||
590 | return strrchr($haystack, $needle, $part); |
||
591 | } |
||
592 | $needle = self::mb_substr($needle, 0, 1, $encoding); |
||
593 | $pos = iconv_strrpos($haystack, $needle, $encoding); |
||
594 | |||
595 | return self::getSubpart($pos, $part, $haystack, $encoding); |
||
596 | } |
||
597 | |||
598 | public static function mb_strrichr($haystack, $needle, $part = false, $encoding = null) |
||
599 | { |
||
600 | $needle = self::mb_substr($needle, 0, 1, $encoding); |
||
601 | $pos = self::mb_strripos($haystack, $needle, $encoding); |
||
602 | |||
603 | return self::getSubpart($pos, $part, $haystack, $encoding); |
||
604 | } |
||
605 | |||
606 | public static function mb_strripos($haystack, $needle, $offset = 0, $encoding = null) |
||
607 | { |
||
608 | $haystack = self::mb_convert_case($haystack, self::MB_CASE_FOLD, $encoding); |
||
609 | $needle = self::mb_convert_case($needle, self::MB_CASE_FOLD, $encoding); |
||
610 | |||
611 | return self::mb_strrpos($haystack, $needle, $offset, $encoding); |
||
612 | } |
||
613 | |||
614 | public static function mb_strstr($haystack, $needle, $part = false, $encoding = null) |
||
615 | { |
||
616 | $pos = strpos($haystack, $needle); |
||
617 | if (false === $pos) { |
||
618 | return false; |
||
619 | } |
||
620 | if ($part) { |
||
621 | return substr($haystack, 0, $pos); |
||
622 | } |
||
623 | |||
624 | return substr($haystack, $pos); |
||
625 | } |
||
626 | |||
627 | public static function mb_get_info($type = 'all') |
||
628 | { |
||
629 | $info = array( |
||
630 | 'internal_encoding' => self::$internalEncoding, |
||
631 | 'http_output' => 'pass', |
||
632 | 'http_output_conv_mimetypes' => '^(text/|application/xhtml\+xml)', |
||
633 | 'func_overload' => 0, |
||
634 | 'func_overload_list' => 'no overload', |
||
635 | 'mail_charset' => 'UTF-8', |
||
636 | 'mail_header_encoding' => 'BASE64', |
||
637 | 'mail_body_encoding' => 'BASE64', |
||
638 | 'illegal_chars' => 0, |
||
639 | 'encoding_translation' => 'Off', |
||
640 | 'language' => self::$language, |
||
641 | 'detect_order' => self::$encodingList, |
||
642 | 'substitute_character' => 'none', |
||
643 | 'strict_detection' => 'Off', |
||
644 | ); |
||
645 | |||
646 | if ('all' === $type) { |
||
647 | return $info; |
||
648 | } |
||
649 | if (isset($info[$type])) { |
||
650 | return $info[$type]; |
||
651 | } |
||
652 | |||
653 | return false; |
||
654 | } |
||
655 | |||
656 | public static function mb_http_input($type = '') |
||
657 | { |
||
658 | return false; |
||
659 | } |
||
660 | |||
661 | public static function mb_http_output($encoding = null) |
||
662 | { |
||
663 | return null !== $encoding ? 'pass' === $encoding : 'pass'; |
||
664 | } |
||
665 | |||
666 | public static function mb_strwidth($s, $encoding = null) |
||
667 | { |
||
668 | $encoding = self::getEncoding($encoding); |
||
669 | |||
670 | if ('UTF-8' !== $encoding) { |
||
671 | $s = iconv($encoding, 'UTF-8//IGNORE', $s); |
||
672 | } |
||
673 | |||
674 | $s = preg_replace('/[\x{1100}-\x{115F}\x{2329}\x{232A}\x{2E80}-\x{303E}\x{3040}-\x{A4CF}\x{AC00}-\x{D7A3}\x{F900}-\x{FAFF}\x{FE10}-\x{FE19}\x{FE30}-\x{FE6F}\x{FF00}-\x{FF60}\x{FFE0}-\x{FFE6}\x{20000}-\x{2FFFD}\x{30000}-\x{3FFFD}]/u', '', $s, -1, $wide); |
||
675 | |||
676 | return ($wide << 1) + iconv_strlen($s, 'UTF-8'); |
||
677 | } |
||
678 | |||
679 | public static function mb_substr_count($haystack, $needle, $encoding = null) |
||
680 | { |
||
681 | return substr_count($haystack, $needle); |
||
682 | } |
||
683 | |||
684 | public static function mb_output_handler($contents, $status) |
||
685 | { |
||
686 | return $contents; |
||
687 | } |
||
688 | |||
689 | public static function mb_chr($code, $encoding = null) |
||
690 | { |
||
691 | if (0x80 > $code %= 0x200000) { |
||
692 | $s = \chr($code); |
||
693 | } elseif (0x800 > $code) { |
||
694 | $s = \chr(0xC0 | $code >> 6).\chr(0x80 | $code & 0x3F); |
||
695 | } elseif (0x10000 > $code) { |
||
696 | $s = \chr(0xE0 | $code >> 12).\chr(0x80 | $code >> 6 & 0x3F).\chr(0x80 | $code & 0x3F); |
||
697 | } else { |
||
698 | $s = \chr(0xF0 | $code >> 18).\chr(0x80 | $code >> 12 & 0x3F).\chr(0x80 | $code >> 6 & 0x3F).\chr(0x80 | $code & 0x3F); |
||
699 | } |
||
700 | |||
701 | if ('UTF-8' !== $encoding = self::getEncoding($encoding)) { |
||
702 | $s = mb_convert_encoding($s, $encoding, 'UTF-8'); |
||
703 | } |
||
704 | |||
705 | return $s; |
||
706 | } |
||
707 | |||
708 | public static function mb_ord($s, $encoding = null) |
||
709 | { |
||
710 | if ('UTF-8' !== $encoding = self::getEncoding($encoding)) { |
||
711 | $s = mb_convert_encoding($s, 'UTF-8', $encoding); |
||
712 | } |
||
713 | |||
714 | if (1 === \strlen($s)) { |
||
715 | return \ord($s); |
||
716 | } |
||
717 | |||
718 | $code = ($s = unpack('C*', substr($s, 0, 4))) ? $s[1] : 0; |
||
719 | if (0xF0 <= $code) { |
||
720 | return (($code - 0xF0) << 18) + (($s[2] - 0x80) << 12) + (($s[3] - 0x80) << 6) + $s[4] - 0x80; |
||
721 | } |
||
722 | if (0xE0 <= $code) { |
||
723 | return (($code - 0xE0) << 12) + (($s[2] - 0x80) << 6) + $s[3] - 0x80; |
||
724 | } |
||
725 | if (0xC0 <= $code) { |
||
726 | return (($code - 0xC0) << 6) + $s[2] - 0x80; |
||
727 | } |
||
728 | |||
729 | return $code; |
||
730 | } |
||
731 | |||
732 | private static function getSubpart($pos, $part, $haystack, $encoding) |
||
733 | { |
||
734 | if (false === $pos) { |
||
735 | return false; |
||
736 | } |
||
737 | if ($part) { |
||
738 | return self::mb_substr($haystack, 0, $pos, $encoding); |
||
739 | } |
||
740 | |||
741 | return self::mb_substr($haystack, $pos, null, $encoding); |
||
742 | } |
||
743 | |||
744 | private static function html_encoding_callback(array $m) |
||
745 | { |
||
746 | $i = 1; |
||
747 | $entities = ''; |
||
748 | $m = unpack('C*', htmlentities($m[0], ENT_COMPAT, 'UTF-8')); |
||
749 | |||
750 | while (isset($m[$i])) { |
||
751 | if (0x80 > $m[$i]) { |
||
752 | $entities .= \chr($m[$i++]); |
||
753 | continue; |
||
754 | } |
||
755 | if (0xF0 <= $m[$i]) { |
||
756 | $c = (($m[$i++] - 0xF0) << 18) + (($m[$i++] - 0x80) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80; |
||
757 | } elseif (0xE0 <= $m[$i]) { |
||
758 | $c = (($m[$i++] - 0xE0) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80; |
||
759 | } else { |
||
760 | $c = (($m[$i++] - 0xC0) << 6) + $m[$i++] - 0x80; |
||
761 | } |
||
762 | |||
763 | $entities .= '&#'.$c.';'; |
||
764 | } |
||
765 | |||
766 | return $entities; |
||
767 | } |
||
768 | |||
769 | private static function title_case(array $s) |
||
770 | { |
||
771 | return self::mb_convert_case($s[1], MB_CASE_UPPER, 'UTF-8').self::mb_convert_case($s[2], MB_CASE_LOWER, 'UTF-8'); |
||
772 | } |
||
773 | |||
774 | private static function getData($file) |
||
775 | { |
||
776 | if (file_exists($file = __DIR__.'/Resources/unidata/'.$file.'.php')) { |
||
777 | return require $file; |
||
778 | } |
||
779 | |||
780 | return false; |
||
781 | } |
||
782 | |||
783 | private static function getEncoding($encoding) |
||
799 | } |
||
800 | } |
||
801 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.