| Conditions | 10 |
| Paths | 33 |
| Total Lines | 31 |
| Code Lines | 21 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 9 | public static function findMedianSortedArrays(array $num1, array $num2): float |
||
| 10 | { |
||
| 11 | if (empty($num1) && empty($num2)) { |
||
| 12 | return 0; |
||
| 13 | } |
||
| 14 | $i = $j = $k = 0; |
||
| 15 | [$m, $n] = [count($num1), count($num2)]; |
||
| 16 | $res = array_fill(0, $m + $n, 0); |
||
| 17 | while ($i < $m && $j < $n) { |
||
| 18 | if ($num1[$i] < $num2[$j]) { |
||
| 19 | $res[$k++] = $num1[$i++]; |
||
| 20 | } elseif ($num1[$i] > $num2[$j]) { |
||
| 21 | $res[$k++] = $num2[$j++]; |
||
| 22 | } else { |
||
| 23 | $res[$k++] = $num1[$i++]; |
||
| 24 | $res[$k++] = $num2[$j++]; |
||
| 25 | } |
||
| 26 | } |
||
| 27 | while ($i < $m) { |
||
| 28 | $res[$k++] = $num1[$i++]; |
||
| 29 | } |
||
| 30 | while ($j < $n) { |
||
| 31 | $res[$k++] = $num2[$j++]; |
||
| 32 | } |
||
| 33 | if (($cnt = count($res)) % 2 === 0) { |
||
| 34 | $mid = $cnt / 2; |
||
| 35 | |||
| 36 | return (float) ($res[$mid] + $res[$mid - 1]) / 2; |
||
| 37 | } |
||
| 38 | |||
| 39 | return (float) $res[$k / 2]; |
||
| 40 | } |
||
| 77 |