| Conditions | 13 |
| Paths | 4 |
| Total Lines | 35 |
| Code Lines | 24 |
| 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 |
||
| 79 | public static function threeSum3(array $nums): array |
||
| 80 | { |
||
| 81 | [$ans, $n] = [[], count($nums)]; |
||
| 82 | if (empty($nums) || $n < 3) { |
||
| 83 | return $ans; |
||
| 84 | } |
||
| 85 | |||
| 86 | sort($nums); |
||
| 87 | for ($i = 0; $i < $n - 2; $i++) { |
||
| 88 | if ($i > 0 && $nums[$i] === $nums[$i - 1]) { |
||
| 89 | continue; |
||
| 90 | } |
||
| 91 | $low = $i + 1; |
||
| 92 | $high = $n - 1; |
||
| 93 | while ($low < $high) { |
||
| 94 | $sum = $nums[$i] + $nums[$low] + $nums[$high]; |
||
| 95 | if ($sum > 0) { |
||
| 96 | $high--; |
||
| 97 | } elseif ($sum < 0) { |
||
| 98 | $low++; |
||
| 99 | } else { |
||
| 100 | $ans[] = [$nums[$i], $nums[$low], $nums[$high]]; |
||
| 101 | while ($low < $high && $nums[$low] === $nums[$low + 1]) { |
||
| 102 | $low++; |
||
| 103 | } |
||
| 104 | while ($low < $high && $nums[$high] === $nums[$high - 1]) { |
||
| 105 | $high--; |
||
| 106 | } |
||
| 107 | $low++; |
||
| 108 | $high--; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | return $ans; |
||
| 114 | } |
||
| 116 |