Conditions | 19 |
Paths | 80 |
Total Lines | 107 |
Code Lines | 47 |
Lines | 0 |
Ratio | 0 % |
Tests | 24 |
CRAP Score | 50.1515 |
Changes | 13 | ||
Bugs | 8 | 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 |
||
567 | 21 | public static function chr($code_point, string $encoding = 'UTF-8') |
|
568 | { |
||
569 | // init |
||
570 | /** |
||
571 | * @psalm-suppress ImpureStaticVariable |
||
572 | * |
||
573 | * @var array<string,string> |
||
574 | */ |
||
575 | 21 | static $CHAR_CACHE = []; |
|
576 | |||
577 | 21 | if ($encoding !== 'UTF-8' && $encoding !== 'CP850') { |
|
578 | 5 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
|
579 | } |
||
580 | |||
581 | if ( |
||
582 | 21 | $encoding !== 'UTF-8' |
|
583 | && |
||
584 | 21 | $encoding !== 'ISO-8859-1' |
|
585 | && |
||
586 | 21 | $encoding !== 'WINDOWS-1252' |
|
587 | && |
||
588 | 21 | self::$SUPPORT['mbstring'] === false |
|
589 | ) { |
||
590 | /** |
||
591 | * @psalm-suppress ImpureFunctionCall - is is only a warning |
||
592 | */ |
||
593 | \trigger_error('UTF8::chr() without mbstring cannot handle "' . $encoding . '" encoding', \E_USER_WARNING); |
||
594 | } |
||
595 | |||
596 | 21 | if (!\is_int($code_point) || $code_point <= 0) { |
|
597 | 5 | return null; |
|
598 | } |
||
599 | |||
600 | 21 | $cache_key = $code_point . '_' . $encoding; |
|
601 | 21 | if (isset($CHAR_CACHE[$cache_key])) { |
|
602 | 19 | return $CHAR_CACHE[$cache_key]; |
|
603 | } |
||
604 | |||
605 | 10 | if ($code_point <= 0x80) { // only for "simple"-chars |
|
606 | |||
607 | 9 | if (self::$CHR === null) { |
|
608 | 1 | self::$CHR = self::getData('chr'); |
|
609 | } |
||
610 | |||
611 | /** |
||
612 | * @psalm-suppress PossiblyNullArrayAccess |
||
613 | */ |
||
614 | 9 | $chr = self::$CHR[$code_point]; |
|
615 | |||
616 | 9 | if ($encoding !== 'UTF-8') { |
|
617 | 1 | $chr = self::encode($encoding, $chr); |
|
618 | } |
||
619 | |||
620 | 9 | return $CHAR_CACHE[$cache_key] = $chr; |
|
621 | } |
||
622 | |||
623 | // |
||
624 | // fallback via "IntlChar" |
||
625 | // |
||
626 | |||
627 | 6 | if (self::$SUPPORT['intlChar'] === true) { |
|
628 | 6 | $chr = \IntlChar::chr($code_point); |
|
629 | |||
630 | 6 | if ($encoding !== 'UTF-8') { |
|
631 | $chr = self::encode($encoding, $chr); |
||
632 | } |
||
633 | |||
634 | 6 | return $CHAR_CACHE[$cache_key] = $chr; |
|
635 | } |
||
636 | |||
637 | // |
||
638 | // fallback via vanilla php |
||
639 | // |
||
640 | |||
641 | if (self::$CHR === null) { |
||
642 | self::$CHR = self::getData('chr'); |
||
643 | } |
||
644 | |||
645 | $code_point = (int) $code_point; |
||
646 | if ($code_point <= 0x7FF) { |
||
647 | /** |
||
648 | * @psalm-suppress PossiblyNullArrayAccess |
||
649 | */ |
||
650 | $chr = self::$CHR[($code_point >> 6) + 0xC0] . |
||
651 | self::$CHR[($code_point & 0x3F) + 0x80]; |
||
652 | } elseif ($code_point <= 0xFFFF) { |
||
653 | /** |
||
654 | * @psalm-suppress PossiblyNullArrayAccess |
||
655 | */ |
||
656 | $chr = self::$CHR[($code_point >> 12) + 0xE0] . |
||
657 | self::$CHR[(($code_point >> 6) & 0x3F) + 0x80] . |
||
658 | self::$CHR[($code_point & 0x3F) + 0x80]; |
||
659 | } else { |
||
660 | /** |
||
661 | * @psalm-suppress PossiblyNullArrayAccess |
||
662 | */ |
||
663 | $chr = self::$CHR[($code_point >> 18) + 0xF0] . |
||
664 | self::$CHR[(($code_point >> 12) & 0x3F) + 0x80] . |
||
665 | self::$CHR[(($code_point >> 6) & 0x3F) + 0x80] . |
||
666 | self::$CHR[($code_point & 0x3F) + 0x80]; |
||
667 | } |
||
668 | |||
669 | if ($encoding !== 'UTF-8') { |
||
670 | $chr = self::encode($encoding, $chr); |
||
671 | } |
||
672 | |||
673 | return $CHAR_CACHE[$cache_key] = $chr; |
||
674 | } |
||
13694 |