Conditions | 29 |
Paths | 41 |
Total Lines | 134 |
Code Lines | 78 |
Lines | 0 |
Ratio | 0 % |
Tests | 42 |
CRAP Score | 60.1388 |
Changes | 7 | ||
Bugs | 1 | 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 |
||
7777 | public static function str_split( |
||
7778 | $input, |
||
7779 | int $length = 1, |
||
7780 | bool $clean_utf8 = false, |
||
7781 | bool $try_to_use_mb_functions = true |
||
7782 | ): array { |
||
7783 | 90 | if ($length <= 0) { |
|
7784 | 3 | return []; |
|
7785 | } |
||
7786 | |||
7787 | // this is only an old fallback |
||
7788 | /** @noinspection PhpSillyAssignmentInspection - hack for phpstan */ |
||
7789 | /** @var int|int[]|string|string[] $input */ |
||
7790 | 89 | $input = $input; |
|
7791 | 89 | if (\is_array($input)) { |
|
7792 | /** @psalm-suppress InvalidReturnStatement */ |
||
7793 | /** @phpstan-ignore-next-line - old code :/ */ |
||
7794 | return self::str_split_array( |
||
7795 | $input, |
||
7796 | $length, |
||
7797 | $clean_utf8, |
||
7798 | $try_to_use_mb_functions |
||
7799 | ); |
||
7800 | } |
||
7801 | |||
7802 | // init |
||
7803 | 89 | $input = (string) $input; |
|
7804 | |||
7805 | 89 | if ($input === '') { |
|
7806 | 14 | return []; |
|
7807 | } |
||
7808 | |||
7809 | 86 | if ($clean_utf8) { |
|
7810 | 19 | $input = self::clean($input); |
|
7811 | } |
||
7812 | |||
7813 | if ( |
||
7814 | 86 | $try_to_use_mb_functions |
|
7815 | && |
||
7816 | 86 | self::$SUPPORT['mbstring'] === true |
|
7817 | ) { |
||
7818 | 82 | if (\function_exists('mb_str_split')) { |
|
7819 | /** |
||
7820 | * @psalm-suppress ImpureFunctionCall - why? |
||
7821 | */ |
||
7822 | 82 | $return = \mb_str_split($input, $length); |
|
7823 | 82 | if ($return !== false) { |
|
7824 | 82 | return $return; |
|
7825 | } |
||
7826 | } |
||
7827 | |||
7828 | $i_max = \mb_strlen($input); |
||
7829 | if ($i_max <= 127) { |
||
7830 | $ret = []; |
||
7831 | for ($i = 0; $i < $i_max; ++$i) { |
||
7832 | $ret[] = \mb_substr($input, $i, 1); |
||
7833 | } |
||
7834 | } else { |
||
7835 | $return_array = []; |
||
7836 | \preg_match_all('/./us', $input, $return_array); |
||
7837 | $ret = $return_array[0] ?? []; |
||
7838 | } |
||
7839 | 23 | } elseif (self::$SUPPORT['pcre_utf8'] === true) { |
|
7840 | 17 | $return_array = []; |
|
7841 | 17 | \preg_match_all('/./us', $input, $return_array); |
|
7842 | 17 | $ret = $return_array[0] ?? []; |
|
7843 | } else { |
||
7844 | |||
7845 | // fallback |
||
7846 | |||
7847 | 8 | $ret = []; |
|
7848 | 8 | $len = \strlen($input); |
|
7849 | |||
7850 | 8 | for ($i = 0; $i < $len; ++$i) { |
|
7851 | 8 | if (($input[$i] & "\x80") === "\x00") { |
|
7852 | 8 | $ret[] = $input[$i]; |
|
7853 | } elseif ( |
||
7854 | 8 | isset($input[$i + 1]) |
|
7855 | && |
||
7856 | 8 | ($input[$i] & "\xE0") === "\xC0" |
|
7857 | ) { |
||
7858 | 4 | if (($input[$i + 1] & "\xC0") === "\x80") { |
|
7859 | 4 | $ret[] = $input[$i] . $input[$i + 1]; |
|
7860 | |||
7861 | 4 | ++$i; |
|
7862 | } |
||
7863 | } elseif ( |
||
7864 | 6 | isset($input[$i + 2]) |
|
7865 | && |
||
7866 | 6 | ($input[$i] & "\xF0") === "\xE0" |
|
7867 | ) { |
||
7868 | if ( |
||
7869 | 6 | ($input[$i + 1] & "\xC0") === "\x80" |
|
7870 | && |
||
7871 | 6 | ($input[$i + 2] & "\xC0") === "\x80" |
|
7872 | ) { |
||
7873 | 6 | $ret[] = $input[$i] . $input[$i + 1] . $input[$i + 2]; |
|
7874 | |||
7875 | 6 | $i += 2; |
|
7876 | } |
||
7877 | } elseif ( |
||
7878 | isset($input[$i + 3]) |
||
7879 | && |
||
7880 | ($input[$i] & "\xF8") === "\xF0" |
||
7881 | ) { |
||
7882 | if ( |
||
7883 | ($input[$i + 1] & "\xC0") === "\x80" |
||
7884 | && |
||
7885 | ($input[$i + 2] & "\xC0") === "\x80" |
||
7886 | && |
||
7887 | ($input[$i + 3] & "\xC0") === "\x80" |
||
7888 | ) { |
||
7889 | $ret[] = $input[$i] . $input[$i + 1] . $input[$i + 2] . $input[$i + 3]; |
||
7890 | |||
7891 | $i += 3; |
||
7892 | } |
||
7893 | } |
||
7894 | } |
||
7895 | } |
||
7896 | |||
7897 | 23 | if ($length > 1) { |
|
7898 | 2 | return \array_map( |
|
7899 | static function (array $item): string { |
||
7900 | 2 | return \implode('', $item); |
|
7901 | 2 | }, |
|
7902 | 2 | \array_chunk($ret, $length) |
|
7903 | ); |
||
7904 | } |
||
7905 | |||
7906 | 23 | if (isset($ret[0]) && $ret[0] === '') { |
|
7907 | return []; |
||
7908 | } |
||
7909 | |||
7910 | 23 | return $ret; |
|
7911 | } |
||
13723 |