| Conditions | 12 |
| Paths | 26 |
| Total Lines | 39 |
| Code Lines | 20 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 39 | function lexicalDistance(string $aStr, string $bStr): int |
||
| 40 | { |
||
| 41 | if ($aStr === $bStr) { |
||
| 42 | return 0; |
||
| 43 | } |
||
| 44 | |||
| 45 | $d = []; |
||
| 46 | $a = strtolower($aStr); |
||
| 47 | $b = strtolower($bStr); |
||
| 48 | $aLength = strlen($a); |
||
| 49 | $bLength = strlen($b); |
||
| 50 | |||
| 51 | if ($a === $b) { |
||
| 52 | return 1; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** @noinspection ForeachInvariantsInspection */ |
||
| 56 | for ($i = 0; $i <= $aLength; $i++) { |
||
| 57 | $d[$i] = [$i]; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** @noinspection ForeachInvariantsInspection */ |
||
| 61 | for ($j = 1; $j <= $bLength; $j++) { |
||
| 62 | $d[0][$j] = $i; |
||
| 63 | } |
||
| 64 | |||
| 65 | for ($i = 1; $i <= $aLength; $i++) { |
||
| 66 | for ($j = 1; $j <= $bLength; $j++) { |
||
| 67 | $cost = $a[$i - 1] === $b[$j - 1] ? 0 : 1; |
||
| 68 | |||
| 69 | $d[$i][$j] = min($d[$i - 1][$j] + 1, $d[$i][$j - 1] + 1, $d[$i - 1][$j - 1] + $cost); |
||
| 70 | |||
| 71 | if ($i > 1 && $j > 1 && $a[$i - 1] === $b[$j - 2] && $a[$i - 2] === $b[$j - 1]) { |
||
| 72 | $d[$i][$j] = min($d[$i][$j], $d[$i - 2][$j - 2] + $cost); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | return $d[$aLength][$bLength]; |
||
| 78 | } |
||
| 79 |