Conditions | 16 |
Paths | 43 |
Total Lines | 76 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Tests | 29 |
CRAP Score | 22.4196 |
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 |
||
6778 | 11 | public static function str_longest_common_substring( |
|
6779 | string $str1, |
||
6780 | string $str2, |
||
6781 | string $encoding = 'UTF-8' |
||
6782 | ): string { |
||
6783 | 11 | if ($str1 === '' || $str2 === '') { |
|
6784 | 2 | return ''; |
|
6785 | } |
||
6786 | |||
6787 | // Uses dynamic programming to solve |
||
6788 | // http://en.wikipedia.org/wiki/Longest_common_substring_problem |
||
6789 | |||
6790 | 9 | if ($encoding === 'UTF-8') { |
|
6791 | 4 | $str_length = (int) \mb_strlen($str1); |
|
6792 | 4 | $other_length = (int) \mb_strlen($str2); |
|
6793 | } else { |
||
6794 | 5 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
|
6795 | |||
6796 | 5 | $str_length = (int) self::strlen($str1, $encoding); |
|
6797 | 5 | $other_length = (int) self::strlen($str2, $encoding); |
|
6798 | } |
||
6799 | |||
6800 | // Return if either string is empty |
||
6801 | 9 | if ($str_length === 0 || $other_length === 0) { |
|
6802 | return ''; |
||
6803 | } |
||
6804 | |||
6805 | 9 | $len = 0; |
|
6806 | 9 | $end = 0; |
|
6807 | 9 | $table = \array_fill( |
|
6808 | 9 | 0, |
|
6809 | 9 | $str_length + 1, |
|
6810 | 9 | \array_fill(0, $other_length + 1, 0) |
|
6811 | ); |
||
6812 | |||
6813 | 9 | if ($encoding === 'UTF-8') { |
|
6814 | 9 | for ($i = 1; $i <= $str_length; ++$i) { |
|
6815 | 9 | for ($j = 1; $j <= $other_length; ++$j) { |
|
6816 | 9 | $str_char = \mb_substr($str1, $i - 1, 1); |
|
6817 | 9 | $other_char = \mb_substr($str2, $j - 1, 1); |
|
6818 | |||
6819 | 9 | if ($str_char === $other_char) { |
|
6820 | 8 | $table[$i][$j] = $table[$i - 1][$j - 1] + 1; |
|
6821 | 8 | if ($table[$i][$j] > $len) { |
|
6822 | 8 | $len = $table[$i][$j]; |
|
6823 | 8 | $end = $i; |
|
6824 | } |
||
6825 | } else { |
||
6826 | 9 | $table[$i][$j] = 0; |
|
6827 | } |
||
6828 | } |
||
6829 | } |
||
6830 | } else { |
||
6831 | for ($i = 1; $i <= $str_length; ++$i) { |
||
6832 | for ($j = 1; $j <= $other_length; ++$j) { |
||
6833 | $str_char = self::substr($str1, $i - 1, 1, $encoding); |
||
6834 | $other_char = self::substr($str2, $j - 1, 1, $encoding); |
||
6835 | |||
6836 | if ($str_char === $other_char) { |
||
6837 | $table[$i][$j] = $table[$i - 1][$j - 1] + 1; |
||
6838 | if ($table[$i][$j] > $len) { |
||
6839 | $len = $table[$i][$j]; |
||
6840 | $end = $i; |
||
6841 | } |
||
6842 | } else { |
||
6843 | $table[$i][$j] = 0; |
||
6844 | } |
||
6845 | } |
||
6846 | } |
||
6847 | } |
||
6848 | |||
6849 | 9 | if ($encoding === 'UTF-8') { |
|
6850 | 9 | return (string) \mb_substr($str1, $end - $len, $len); |
|
6851 | } |
||
6852 | |||
6853 | return (string) self::substr($str1, $end - $len, $len, $encoding); |
||
6854 | } |
||
13694 |