Conditions | 16 |
Paths | 43 |
Total Lines | 76 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Tests | 28 |
CRAP Score | 22.912 |
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 |
||
6751 | public static function str_longest_common_substring( |
||
6752 | string $str1, |
||
6753 | string $str2, |
||
6754 | string $encoding = 'UTF-8' |
||
6755 | ): string { |
||
6756 | 11 | if ($str1 === '' || $str2 === '') { |
|
6757 | 2 | return ''; |
|
6758 | } |
||
6759 | |||
6760 | // Uses dynamic programming to solve |
||
6761 | // http://en.wikipedia.org/wiki/Longest_common_substring_problem |
||
6762 | |||
6763 | 9 | if ($encoding === 'UTF-8') { |
|
6764 | 4 | $str_length = (int) \mb_strlen($str1); |
|
6765 | 4 | $other_length = (int) \mb_strlen($str2); |
|
6766 | } else { |
||
6767 | 5 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
|
6768 | |||
6769 | 5 | $str_length = (int) self::strlen($str1, $encoding); |
|
6770 | 5 | $other_length = (int) self::strlen($str2, $encoding); |
|
6771 | } |
||
6772 | |||
6773 | // Return if either string is empty |
||
6774 | 9 | if ($str_length === 0 || $other_length === 0) { |
|
6775 | return ''; |
||
6776 | } |
||
6777 | |||
6778 | 9 | $len = 0; |
|
6779 | 9 | $end = 0; |
|
6780 | 9 | $table = \array_fill( |
|
6781 | 9 | 0, |
|
6782 | 9 | $str_length + 1, |
|
6783 | 9 | \array_fill(0, $other_length + 1, 0) |
|
6784 | ); |
||
6785 | |||
6786 | 9 | if ($encoding === 'UTF-8') { |
|
6787 | 9 | for ($i = 1; $i <= $str_length; ++$i) { |
|
6788 | 9 | for ($j = 1; $j <= $other_length; ++$j) { |
|
6789 | 9 | $str_char = \mb_substr($str1, $i - 1, 1); |
|
6790 | 9 | $other_char = \mb_substr($str2, $j - 1, 1); |
|
6791 | |||
6792 | 9 | if ($str_char === $other_char) { |
|
6793 | 8 | $table[$i][$j] = $table[$i - 1][$j - 1] + 1; |
|
6794 | 8 | if ($table[$i][$j] > $len) { |
|
6795 | 8 | $len = $table[$i][$j]; |
|
6796 | 8 | $end = $i; |
|
6797 | } |
||
6798 | } else { |
||
6799 | 9 | $table[$i][$j] = 0; |
|
6800 | } |
||
6801 | } |
||
6802 | } |
||
6803 | } else { |
||
6804 | for ($i = 1; $i <= $str_length; ++$i) { |
||
6805 | for ($j = 1; $j <= $other_length; ++$j) { |
||
6806 | $str_char = self::substr($str1, $i - 1, 1, $encoding); |
||
6807 | $other_char = self::substr($str2, $j - 1, 1, $encoding); |
||
6808 | |||
6809 | if ($str_char === $other_char) { |
||
6810 | $table[$i][$j] = $table[$i - 1][$j - 1] + 1; |
||
6811 | if ($table[$i][$j] > $len) { |
||
6812 | $len = $table[$i][$j]; |
||
6813 | $end = $i; |
||
6814 | } |
||
6815 | } else { |
||
6816 | $table[$i][$j] = 0; |
||
6817 | } |
||
6818 | } |
||
6819 | } |
||
6820 | } |
||
6821 | |||
6822 | 9 | if ($encoding === 'UTF-8') { |
|
6823 | 9 | return (string) \mb_substr($str1, $end - $len, $len); |
|
6824 | } |
||
6825 | |||
6826 | return (string) self::substr($str1, $end - $len, $len, $encoding); |
||
6827 | } |
||
13665 |