Conditions | 10 |
Paths | 6 |
Total Lines | 54 |
Code Lines | 32 |
Lines | 0 |
Ratio | 0 % |
Tests | 26 |
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 |
||
6913 | 10 | public static function str_longest_common_suffix( |
|
6914 | string $str1, |
||
6915 | string $str2, |
||
6916 | string $encoding = 'UTF-8' |
||
6917 | ): string { |
||
6918 | 10 | if ($str1 === '' || $str2 === '') { |
|
6919 | 2 | return ''; |
|
6920 | } |
||
6921 | |||
6922 | 8 | if ($encoding === 'UTF-8') { |
|
6923 | 4 | $max_length = (int) \min( |
|
6924 | 4 | \mb_strlen($str1, $encoding), |
|
6925 | 4 | \mb_strlen($str2, $encoding) |
|
6926 | ); |
||
6927 | |||
6928 | 4 | $longest_common_suffix = ''; |
|
6929 | 4 | for ($i = 1; $i <= $max_length; ++$i) { |
|
6930 | 4 | $char = \mb_substr($str1, -$i, 1); |
|
6931 | |||
6932 | if ( |
||
6933 | 4 | $char !== false |
|
6934 | && |
||
6935 | 4 | $char === \mb_substr($str2, -$i, 1) |
|
6936 | ) { |
||
6937 | 3 | $longest_common_suffix = $char . $longest_common_suffix; |
|
6938 | } else { |
||
6939 | 3 | break; |
|
6940 | } |
||
6941 | } |
||
6942 | } else { |
||
6943 | 4 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
|
6944 | |||
6945 | 4 | $max_length = (int) \min( |
|
6946 | 4 | self::strlen($str1, $encoding), |
|
6947 | 4 | self::strlen($str2, $encoding) |
|
6948 | ); |
||
6949 | |||
6950 | 4 | $longest_common_suffix = ''; |
|
6951 | 4 | for ($i = 1; $i <= $max_length; ++$i) { |
|
6952 | 4 | $char = self::substr($str1, -$i, 1, $encoding); |
|
6953 | |||
6954 | if ( |
||
6955 | 4 | $char !== false |
|
6956 | && |
||
6957 | 4 | $char === self::substr($str2, -$i, 1, $encoding) |
|
6958 | ) { |
||
6959 | 3 | $longest_common_suffix = $char . $longest_common_suffix; |
|
6960 | } else { |
||
6961 | 3 | break; |
|
6962 | } |
||
6963 | } |
||
6964 | } |
||
6965 | |||
6966 | 8 | return $longest_common_suffix; |
|
6967 | } |
||
13722 |