| Conditions | 10 |
| Paths | 7 |
| Total Lines | 35 |
| Code Lines | 20 |
| 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 threeSum(array $nums): array |
||
| 10 | { |
||
| 11 | [$ans, $n] = [[], count($nums)]; |
||
| 12 | if (empty($nums) || $n < 3) { |
||
| 13 | return $ans; |
||
| 14 | } |
||
| 15 | |||
| 16 | $isSameArray = static function (array $needle, array $haystack = []) { |
||
| 17 | sort($needle); |
||
| 18 | foreach ($haystack as $value) { |
||
| 19 | sort($value); |
||
| 20 | if ($needle === $value) { |
||
| 21 | return true; |
||
| 22 | } |
||
| 23 | } |
||
| 24 | |||
| 25 | return false; |
||
| 26 | }; |
||
| 27 | |||
| 28 | for ($i = 0; $i < $n; $i++) { |
||
| 29 | for ($j = $i + 1; $j < $n; $j++) { |
||
| 30 | for ($k = $j + 1; $k < $n; $k++) { |
||
| 31 | if ($nums[$i] + $nums[$j] + $nums[$k] === 0) { |
||
| 32 | $needle = [$nums[$i], $nums[$j], $nums[$k]]; |
||
| 33 | sort($needle); |
||
| 34 | if ($isSameArray($needle, $ans)) { |
||
| 35 | continue; |
||
| 36 | } |
||
| 37 | $ans[] = $needle; |
||
| 38 | } |
||
| 39 | } |
||
| 40 | } |
||
| 41 | } |
||
| 42 | |||
| 43 | return array_reverse($ans); |
||
| 44 | } |
||
| 116 |