Conditions | 10 |
Paths | 6 |
Total Lines | 54 |
Code Lines | 32 |
Lines | 0 |
Ratio | 0 % |
Tests | 15 |
CRAP Score | 10 |
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 |
||
6893 | 3 | public static function str_longest_common_suffix( |
|
6894 | string $str1, |
||
6895 | string $str2, |
||
6896 | string $encoding = 'UTF-8' |
||
6897 | 4 | ): string { |
|
6898 | if ($str1 === '' || $str2 === '') { |
||
6899 | 4 | return ''; |
|
6900 | 4 | } |
|
6901 | 4 | ||
6902 | if ($encoding === 'UTF-8') { |
||
6903 | $max_length = (int) \min( |
||
6904 | 4 | \mb_strlen($str1, $encoding), |
|
6905 | 4 | \mb_strlen($str2, $encoding) |
|
6906 | 4 | ); |
|
6907 | |||
6908 | $longest_common_suffix = ''; |
||
6909 | 4 | for ($i = 1; $i <= $max_length; ++$i) { |
|
6910 | $char = \mb_substr($str1, -$i, 1); |
||
6911 | 4 | ||
6912 | if ( |
||
6913 | 3 | $char !== false |
|
6914 | && |
||
6915 | 3 | $char === \mb_substr($str2, -$i, 1) |
|
6916 | ) { |
||
6917 | $longest_common_suffix = $char . $longest_common_suffix; |
||
6918 | } else { |
||
6919 | break; |
||
6920 | 8 | } |
|
6921 | } |
||
6922 | } else { |
||
6923 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
||
6924 | |||
6925 | $max_length = (int) \min( |
||
6926 | self::strlen($str1, $encoding), |
||
6927 | self::strlen($str2, $encoding) |
||
6928 | ); |
||
6929 | |||
6930 | $longest_common_suffix = ''; |
||
6931 | for ($i = 1; $i <= $max_length; ++$i) { |
||
6932 | $char = self::substr($str1, -$i, 1, $encoding); |
||
6933 | |||
6934 | 10 | if ( |
|
6935 | $char !== false |
||
6936 | 10 | && |
|
6937 | $char === self::substr($str2, -$i, 1, $encoding) |
||
6938 | ) { |
||
6939 | $longest_common_suffix = $char . $longest_common_suffix; |
||
6940 | } else { |
||
6941 | break; |
||
6942 | } |
||
6943 | } |
||
6944 | } |
||
6945 | |||
6946 | return $longest_common_suffix; |
||
6947 | } |
||
13757 |