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 |
||
7370 | 11 | public static function str_longest_common_substring( |
|
7371 | string $str1, |
||
7372 | string $str2, |
||
7373 | string $encoding = 'UTF-8' |
||
7374 | ): string { |
||
7375 | 11 | if ($str1 === '' || $str2 === '') { |
|
7376 | 2 | return ''; |
|
7377 | } |
||
7378 | |||
7379 | // Uses dynamic programming to solve |
||
7380 | // http://en.wikipedia.org/wiki/Longest_common_substring_problem |
||
7381 | |||
7382 | 9 | if ($encoding === 'UTF-8') { |
|
7383 | 4 | $str_length = (int) \mb_strlen($str1); |
|
7384 | 4 | $other_length = (int) \mb_strlen($str2); |
|
7385 | } else { |
||
7386 | 5 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
|
7387 | |||
7388 | 5 | $str_length = (int) self::strlen($str1, $encoding); |
|
7389 | 5 | $other_length = (int) self::strlen($str2, $encoding); |
|
7390 | } |
||
7391 | |||
7392 | // Return if either string is empty |
||
7393 | 9 | if ($str_length === 0 || $other_length === 0) { |
|
7394 | return ''; |
||
7395 | } |
||
7396 | |||
7397 | 9 | $len = 0; |
|
7398 | 9 | $end = 0; |
|
7399 | 9 | $table = \array_fill( |
|
7400 | 9 | 0, |
|
7401 | 9 | $str_length + 1, |
|
7402 | 9 | \array_fill(0, $other_length + 1, 0) |
|
7403 | ); |
||
7404 | |||
7405 | 9 | if ($encoding === 'UTF-8') { |
|
7406 | 9 | for ($i = 1; $i <= $str_length; ++$i) { |
|
7407 | 9 | for ($j = 1; $j <= $other_length; ++$j) { |
|
7408 | 9 | $str_char = \mb_substr($str1, $i - 1, 1); |
|
7409 | 9 | $other_char = \mb_substr($str2, $j - 1, 1); |
|
7410 | |||
7411 | 9 | if ($str_char === $other_char) { |
|
7412 | 8 | $table[$i][$j] = $table[$i - 1][$j - 1] + 1; |
|
7413 | 8 | if ($table[$i][$j] > $len) { |
|
7414 | 8 | $len = $table[$i][$j]; |
|
7415 | 8 | $end = $i; |
|
7416 | } |
||
7417 | } else { |
||
7418 | 9 | $table[$i][$j] = 0; |
|
7419 | } |
||
7420 | } |
||
7421 | } |
||
7422 | } else { |
||
7423 | for ($i = 1; $i <= $str_length; ++$i) { |
||
7424 | for ($j = 1; $j <= $other_length; ++$j) { |
||
7425 | $str_char = self::substr($str1, $i - 1, 1, $encoding); |
||
7426 | $other_char = self::substr($str2, $j - 1, 1, $encoding); |
||
7427 | |||
7428 | if ($str_char === $other_char) { |
||
7429 | $table[$i][$j] = $table[$i - 1][$j - 1] + 1; |
||
7430 | if ($table[$i][$j] > $len) { |
||
7431 | $len = $table[$i][$j]; |
||
7432 | $end = $i; |
||
7433 | } |
||
7434 | } else { |
||
7435 | $table[$i][$j] = 0; |
||
7436 | } |
||
7437 | } |
||
7438 | } |
||
7439 | } |
||
7440 | |||
7441 | 9 | if ($encoding === 'UTF-8') { |
|
7442 | 9 | return (string) \mb_substr($str1, $end - $len, $len); |
|
7443 | } |
||
7444 | |||
7445 | return (string) self::substr($str1, $end - $len, $len, $encoding); |
||
7446 | } |
||
14812 |