| Conditions | 11 |
| Paths | 45 |
| Total Lines | 34 |
| Code Lines | 24 |
| 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 |
||
| 49 | public static function minWindow2(string $s, string $t): string |
||
| 50 | { |
||
| 51 | [$m, $n, $map] = [strlen($s), strlen($t), []]; |
||
| 52 | if ($m === 0 || $n === 0) { |
||
| 53 | return ''; |
||
| 54 | } |
||
| 55 | for ($i = 0; $i < $n; $i++) { |
||
| 56 | $k = $t[$i]; |
||
| 57 | $map[$k] = ($map[$k] ?? 0) + 1; |
||
| 58 | } |
||
| 59 | $left = $right = $start = 0; |
||
| 60 | $len = PHP_INT_MAX; |
||
| 61 | while ($right < $m) { |
||
| 62 | $c = $s[$right]; |
||
| 63 | $right++; |
||
| 64 | if (isset($map[$c]) && $map[$c] > 0) { |
||
| 65 | $n--; |
||
| 66 | } |
||
| 67 | $map[$c] = ($map[$c] ?? 0) - 1; |
||
| 68 | while ($n === 0) { |
||
| 69 | if ($right - $left < $len) { |
||
| 70 | $len = $right - $left; |
||
| 71 | $start = $left; |
||
| 72 | } |
||
| 73 | $d = $s[$left]; |
||
| 74 | $left++; |
||
| 75 | $map[$d] = ($map[$d] ?? 0) + 1; |
||
| 76 | if ($map[$d] > 0) { |
||
| 77 | $n++; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | return $len === PHP_INT_MAX ? '' : substr($s, $start, $len); |
||
| 83 | } |
||
| 85 |