Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
| 61 | class Mbstring |
||
| 62 | { |
||
| 63 | const MB_CASE_FOLD = PHP_INT_MAX; |
||
| 64 | |||
| 65 | protected static $encoding_list = array( |
||
|
|
|||
| 66 | 'ASCII', |
||
| 67 | 'UTF-8', |
||
| 68 | ), |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | $language = 'neutral', |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | $internal_encoding = 'UTF-8', |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | $caseFold = array( |
||
| 84 | array( |
||
| 85 | 'µ', |
||
| 86 | 'ſ', |
||
| 87 | "\xCD\x85", |
||
| 88 | 'ς', |
||
| 89 | "\xCF\x90", |
||
| 90 | "\xCF\x91", |
||
| 91 | "\xCF\x95", |
||
| 92 | "\xCF\x96", |
||
| 93 | "\xCF\xB0", |
||
| 94 | "\xCF\xB1", |
||
| 95 | "\xCF\xB5", |
||
| 96 | "\xE1\xBA\x9B", |
||
| 97 | "\xE1\xBE\xBE", |
||
| 98 | ), |
||
| 99 | array( |
||
| 100 | 'μ', |
||
| 101 | 's', |
||
| 102 | 'ι', |
||
| 103 | 'σ', |
||
| 104 | 'β', |
||
| 105 | 'θ', |
||
| 106 | 'φ', |
||
| 107 | 'π', |
||
| 108 | 'κ', |
||
| 109 | 'ρ', |
||
| 110 | 'ε', |
||
| 111 | "\xE1\xB9\xA1", |
||
| 112 | 'ι', |
||
| 113 | ), |
||
| 114 | ); |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param string $str |
||
| 118 | * @param string $to_encoding |
||
| 119 | * @param string $from_encoding |
||
| 120 | * |
||
| 121 | * @return string|false |
||
| 122 | */ |
||
| 123 | 1 | public static function mb_convert_encoding($str, $to_encoding, $from_encoding = INF) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @param string $str |
||
| 176 | * |
||
| 177 | * @return bool|string |
||
| 178 | */ |
||
| 179 | public static function mb_decode_mimeheader($str) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @param $str |
||
| 186 | * @param $charset |
||
| 187 | * @param $transfer_encoding |
||
| 188 | * @param $linefeed |
||
| 189 | * @param $indent |
||
| 190 | */ |
||
| 191 | 1 | public static function mb_encode_mimeheader(/** @noinspection PhpUnusedParameterInspection */ |
|
| 196 | |||
| 197 | /** |
||
| 198 | * @param string $encoding |
||
| 199 | * |
||
| 200 | * @return bool|string |
||
| 201 | */ |
||
| 202 | 1 | public static function mb_internal_encoding($encoding = INF) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @param string $lang |
||
| 233 | * |
||
| 234 | * @return bool|string |
||
| 235 | */ |
||
| 236 | 1 | public static function mb_language($lang = INF) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * @return string[] |
||
| 255 | */ |
||
| 256 | 1 | public static function mb_list_encodings() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * @param string $encoding |
||
| 263 | * |
||
| 264 | * @return array|bool |
||
| 265 | */ |
||
| 266 | 1 | public static function mb_encoding_aliases($encoding) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @param string $var |
||
| 279 | * @param string $encoding |
||
| 280 | * |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | 1 | public static function mb_check_encoding($var = INF, $encoding = INF) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @param string $str |
||
| 297 | * @param string|array $encoding_list |
||
| 298 | * @param bool $strict |
||
| 299 | * |
||
| 300 | * @return bool |
||
| 301 | */ |
||
| 302 | 2 | public static function mb_detect_encoding($str, $encoding_list = INF, /** @noinspection PhpUnusedParameterInspection */ |
|
| 338 | |||
| 339 | /** |
||
| 340 | * @param string|array $encoding_list |
||
| 341 | * |
||
| 342 | * @return array|bool |
||
| 343 | */ |
||
| 344 | 1 | public static function mb_detect_order($encoding_list = INF) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * @param string $str |
||
| 374 | * @param string $encoding |
||
| 375 | * |
||
| 376 | * @return bool|int |
||
| 377 | */ |
||
| 378 | 1 | public static function mb_strlen($str, $encoding = INF) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * @param string $str |
||
| 387 | * @param string $encoding |
||
| 388 | * |
||
| 389 | * @return bool|mixed|string |
||
| 390 | */ |
||
| 391 | 1 | public static function mb_strtolower($str, $encoding = INF) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * @param string $str |
||
| 398 | * @param int $mode |
||
| 399 | * @param string $encoding |
||
| 400 | * |
||
| 401 | * @return bool|mixed|string |
||
| 402 | */ |
||
| 403 | 3 | public static function mb_convert_case($str, $mode, $encoding = INF) |
|
| 404 | { |
||
| 405 | 3 | if ('' === $str .= '') { |
|
| 406 | return ''; |
||
| 407 | } |
||
| 408 | |||
| 409 | 3 | if (INF === $encoding) { |
|
| 410 | 1 | $encoding = self::$internal_encoding; |
|
| 411 | 1 | } else { |
|
| 412 | 3 | $encoding = strtoupper($encoding); |
|
| 413 | } |
||
| 414 | |||
| 415 | 3 | if ('UTF-8' === $encoding || 'UTF8' === $encoding) { |
|
| 416 | 3 | $encoding = INF; |
|
| 417 | 3 | } else { |
|
| 418 | $str = iconv($encoding, 'UTF-8//IGNORE', $str); |
||
| 419 | } |
||
| 420 | |||
| 421 | 3 | if (MB_CASE_TITLE == $mode) { |
|
| 422 | 1 | $str = preg_replace_callback( |
|
| 423 | 1 | '/\b\p{Ll}/u', |
|
| 424 | array( |
||
| 425 | 1 | __CLASS__, |
|
| 426 | 1 | 'title_case_upper', |
|
| 427 | 1 | ), |
|
| 428 | $str |
||
| 429 | 1 | ); |
|
| 430 | 1 | $str = preg_replace_callback( |
|
| 431 | 1 | '/\B[\p{Lu}\p{Lt}]+/u', |
|
| 432 | array( |
||
| 433 | 1 | __CLASS__, |
|
| 434 | 1 | 'title_case_lower', |
|
| 435 | 1 | ), |
|
| 436 | $str |
||
| 437 | 1 | ); |
|
| 438 | 1 | } else { |
|
| 439 | 3 | if (MB_CASE_UPPER == $mode) { |
|
| 440 | 1 | static $upper; |
|
| 441 | 1 | isset($upper) || $upper = static::getData('upperCase'); |
|
| 442 | 1 | $map = $upper; |
|
| 443 | 1 | } else { |
|
| 444 | 3 | if (self::MB_CASE_FOLD === $mode) { |
|
| 445 | 2 | $str = str_replace(self::$caseFold[0], self::$caseFold[1], $str); |
|
| 446 | 2 | } |
|
| 447 | |||
| 448 | 3 | static $lower; |
|
| 449 | 3 | isset($lower) || $lower = static::getData('lowerCase'); |
|
| 450 | 3 | $map = $lower; |
|
| 451 | } |
||
| 452 | |||
| 453 | static $ulen_mask = array( |
||
| 454 | "\xC0" => 2, |
||
| 455 | "\xD0" => 2, |
||
| 456 | "\xE0" => 3, |
||
| 457 | "\xF0" => 4, |
||
| 458 | 3 | ); |
|
| 459 | |||
| 460 | 3 | $i = 0; |
|
| 461 | 3 | $len = strlen($str); |
|
| 462 | |||
| 463 | 3 | while ($i < $len) { |
|
| 464 | |||
| 465 | 3 | if ($str[$i] < "\x80") { |
|
| 466 | 3 | $ulen = 1; |
|
| 467 | 3 | } else { |
|
| 468 | 3 | $ulen = $ulen_mask[$str[$i] & "\xF0"]; |
|
| 469 | } |
||
| 470 | |||
| 471 | 3 | $uchr = substr($str, $i, $ulen); |
|
| 472 | 3 | $i += $ulen; |
|
| 473 | |||
| 474 | 3 | if (isset($map[$uchr])) { |
|
| 475 | 3 | $uchr = $map[$uchr]; |
|
| 476 | 3 | $nlen = strlen($uchr); |
|
| 477 | |||
| 478 | 3 | if ($nlen == $ulen) { |
|
| 479 | 3 | $nlen = $i; |
|
| 480 | do { |
||
| 481 | 3 | $str[--$nlen] = $uchr[--$ulen]; |
|
| 482 | 3 | } while ($ulen); |
|
| 483 | 3 | } else { |
|
| 484 | 1 | $str = substr_replace($str, $uchr, $i - $ulen, $ulen); |
|
| 485 | 1 | $len += $nlen - $ulen; |
|
| 486 | 1 | $i += $nlen - $ulen; |
|
| 487 | } |
||
| 488 | 3 | } |
|
| 489 | 3 | } |
|
| 490 | } |
||
| 491 | |||
| 492 | 3 | if (INF === $encoding) { |
|
| 493 | 3 | return $str; |
|
| 494 | } else { |
||
| 495 | return iconv('UTF-8', $encoding, $str); |
||
| 496 | } |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * get data |
||
| 501 | * |
||
| 502 | * @param string $file |
||
| 503 | * |
||
| 504 | * @return bool|mixed |
||
| 505 | */ |
||
| 506 | 1 | View Code Duplication | protected static function getData($file) |
| 515 | |||
| 516 | /** |
||
| 517 | * @param string $str |
||
| 518 | * @param string $encoding |
||
| 519 | * |
||
| 520 | * @return bool|mixed|string |
||
| 521 | */ |
||
| 522 | 1 | public static function mb_strtoupper($str, $encoding = INF) |
|
| 526 | |||
| 527 | /** |
||
| 528 | * @param string $char |
||
| 529 | * |
||
| 530 | * @return false|string |
||
| 531 | */ |
||
| 532 | 1 | public static function mb_substitute_character($char = INF) |
|
| 540 | |||
| 541 | /** |
||
| 542 | * @param string $haystack |
||
| 543 | * @param string $needle |
||
| 544 | * @param bool $part |
||
| 545 | * @param string $encoding |
||
| 546 | * |
||
| 547 | * @return false|string |
||
| 548 | */ |
||
| 549 | 1 | public static function mb_stristr($haystack, $needle, $part = false, $encoding = INF) |
|
| 555 | |||
| 556 | /** |
||
| 557 | * @param string $haystack |
||
| 558 | * @param string $needle |
||
| 559 | * @param int $offset |
||
| 560 | * @param string $encoding |
||
| 561 | * |
||
| 562 | * @return bool|int |
||
| 563 | */ |
||
| 564 | 2 | View Code Duplication | public static function mb_stripos($haystack, $needle, $offset = 0, $encoding = INF) |
| 572 | |||
| 573 | /** |
||
| 574 | * @param string $haystack |
||
| 575 | * @param string $needle |
||
| 576 | * @param int $offset |
||
| 577 | * @param string $encoding |
||
| 578 | * |
||
| 579 | * @return bool|int |
||
| 580 | */ |
||
| 581 | 4 | public static function mb_strpos($haystack, $needle, $offset = 0, $encoding = INF) |
|
| 592 | |||
| 593 | /** |
||
| 594 | * @param false|int $pos |
||
| 595 | * @param bool $part |
||
| 596 | * @param string $haystack |
||
| 597 | * @param string $encoding |
||
| 598 | * |
||
| 599 | * @return false|string |
||
| 600 | */ |
||
| 601 | 1 | protected static function getSubpart($pos, $part, $haystack, $encoding) |
|
| 614 | |||
| 615 | /** |
||
| 616 | * @param string $str |
||
| 617 | * @param int $start |
||
| 618 | * @param null|int $length |
||
| 619 | * @param string $encoding |
||
| 620 | * |
||
| 621 | * @return string |
||
| 622 | */ |
||
| 623 | 3 | public static function mb_substr($str, $start, $length = null, $encoding = INF) |
|
| 645 | |||
| 646 | /** |
||
| 647 | * @param string $type |
||
| 648 | * |
||
| 649 | * @return array|bool |
||
| 650 | */ |
||
| 651 | public static function mb_get_info($type = 'all') |
||
| 678 | |||
| 679 | /** |
||
| 680 | * @param string $type |
||
| 681 | * |
||
| 682 | * @return bool |
||
| 683 | */ |
||
| 684 | public static function mb_http_input(/** @noinspection PhpUnusedParameterInspection */ |
||
| 689 | |||
| 690 | /** |
||
| 691 | * @param string $encoding |
||
| 692 | * |
||
| 693 | * @return bool|string |
||
| 694 | */ |
||
| 695 | public static function mb_http_output($encoding = INF) |
||
| 703 | |||
| 704 | /** |
||
| 705 | * @param string $str |
||
| 706 | * @param string $encoding |
||
| 707 | * |
||
| 708 | * @return int |
||
| 709 | */ |
||
| 710 | 1 | public static function mb_strwidth($str, $encoding = INF) |
|
| 728 | |||
| 729 | /** |
||
| 730 | * @param string $haystack |
||
| 731 | * @param string $needle |
||
| 732 | * @param string $encoding |
||
| 733 | * |
||
| 734 | * @return int |
||
| 735 | */ |
||
| 736 | public static function mb_substr_count($haystack, $needle, /** @noinspection PhpUnusedParameterInspection */ |
||
| 741 | |||
| 742 | /** |
||
| 743 | * @param $contents |
||
| 744 | * @param $status |
||
| 745 | * |
||
| 746 | * @return mixed |
||
| 747 | */ |
||
| 748 | public static function mb_output_handler($contents, /** @noinspection PhpUnusedParameterInspection */ |
||
| 753 | |||
| 754 | /** |
||
| 755 | * @param string $haystack |
||
| 756 | * @param string $needle |
||
| 757 | * @param bool $part |
||
| 758 | * @param string $encoding |
||
| 759 | * |
||
| 760 | * @return false|string |
||
| 761 | */ |
||
| 762 | 1 | public static function mb_strrchr($haystack, $needle, $part = false, $encoding = INF) |
|
| 771 | |||
| 772 | /** |
||
| 773 | * @param string $haystack |
||
| 774 | * @param string $needle |
||
| 775 | * @param bool $part |
||
| 776 | * @param string $encoding |
||
| 777 | * |
||
| 778 | * @return false|string |
||
| 779 | */ |
||
| 780 | 1 | public static function mb_strrichr($haystack, $needle, $part = false, $encoding = INF) |
|
| 787 | |||
| 788 | /** |
||
| 789 | * @param string $haystack |
||
| 790 | * @param string $needle |
||
| 791 | * @param int $offset |
||
| 792 | * @param string $encoding |
||
| 793 | * |
||
| 794 | * @return bool|int |
||
| 795 | */ |
||
| 796 | 2 | View Code Duplication | public static function mb_strripos($haystack, $needle, $offset = 0, $encoding = INF) |
| 805 | |||
| 806 | /** |
||
| 807 | * @param string $haystack |
||
| 808 | * @param string $needle |
||
| 809 | * @param int $offset |
||
| 810 | * @param string $encoding |
||
| 811 | * |
||
| 812 | * @return bool|int |
||
| 813 | */ |
||
| 814 | 2 | public static function mb_strrpos($haystack, $needle, $offset = 0, $encoding = INF) |
|
| 839 | |||
| 840 | /** |
||
| 841 | * @param string $haystack |
||
| 842 | * @param string $needle |
||
| 843 | * @param bool $part |
||
| 844 | * @param string $encoding |
||
| 845 | * |
||
| 846 | * @return false|string |
||
| 847 | */ |
||
| 848 | 1 | public static function mb_strstr($haystack, $needle, $part = false, /** @noinspection PhpUnusedParameterInspection */ |
|
| 862 | |||
| 863 | /** |
||
| 864 | * @param array $m |
||
| 865 | * |
||
| 866 | * @return string |
||
| 867 | */ |
||
| 868 | 1 | protected static function html_encoding_callback($m) |
|
| 893 | |||
| 894 | /** |
||
| 895 | * @param string $str |
||
| 896 | * |
||
| 897 | * @return bool|mixed|string |
||
| 898 | */ |
||
| 899 | 1 | protected static function title_case_lower($str) |
|
| 903 | |||
| 904 | /** |
||
| 905 | * @param string $str |
||
| 906 | * |
||
| 907 | * @return bool|mixed|string |
||
| 908 | */ |
||
| 909 | 1 | protected static function title_case_upper($str) |
|
| 913 | } |
||
| 914 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.