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 |
||
6840 | 10 | public static function str_longest_common_suffix( |
|
6841 | string $str1, |
||
6842 | string $str2, |
||
6843 | string $encoding = 'UTF-8' |
||
6844 | ): string { |
||
6845 | 10 | if ($str1 === '' || $str2 === '') { |
|
6846 | 2 | return ''; |
|
6847 | } |
||
6848 | |||
6849 | 8 | if ($encoding === 'UTF-8') { |
|
6850 | 4 | $max_length = (int) \min( |
|
6851 | 4 | \mb_strlen($str1, $encoding), |
|
6852 | 4 | \mb_strlen($str2, $encoding) |
|
6853 | ); |
||
6854 | |||
6855 | 4 | $longest_common_suffix = ''; |
|
6856 | 4 | for ($i = 1; $i <= $max_length; ++$i) { |
|
6857 | 4 | $char = \mb_substr($str1, -$i, 1); |
|
6858 | |||
6859 | if ( |
||
6860 | 4 | $char !== false |
|
6861 | && |
||
6862 | 4 | $char === \mb_substr($str2, -$i, 1) |
|
6863 | ) { |
||
6864 | 3 | $longest_common_suffix = $char . $longest_common_suffix; |
|
6865 | } else { |
||
6866 | 3 | break; |
|
6867 | } |
||
6868 | } |
||
6869 | } else { |
||
6870 | 4 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
|
6871 | |||
6872 | 4 | $max_length = (int) \min( |
|
6873 | 4 | self::strlen($str1, $encoding), |
|
6874 | 4 | self::strlen($str2, $encoding) |
|
6875 | ); |
||
6876 | |||
6877 | 4 | $longest_common_suffix = ''; |
|
6878 | 4 | for ($i = 1; $i <= $max_length; ++$i) { |
|
6879 | 4 | $char = self::substr($str1, -$i, 1, $encoding); |
|
6880 | |||
6881 | if ( |
||
6882 | 4 | $char !== false |
|
6883 | && |
||
6884 | 4 | $char === self::substr($str2, -$i, 1, $encoding) |
|
6885 | ) { |
||
6886 | 3 | $longest_common_suffix = $char . $longest_common_suffix; |
|
6887 | } else { |
||
6888 | 3 | break; |
|
6889 | } |
||
6890 | } |
||
6891 | } |
||
6892 | |||
6893 | 8 | return $longest_common_suffix; |
|
6894 | } |
||
13666 |