Conditions | 13 |
Paths | 11 |
Total Lines | 27 |
Code Lines | 17 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 fourSumCount(array $a, array $b, array $c, array $d): int |
||
10 | { |
||
11 | if (empty($a) || empty($b) || empty($c) || empty($d)) { |
||
12 | return 0; |
||
13 | } |
||
14 | if (count($a) !== count($b) || |
||
15 | count($b) !== count($c) || |
||
16 | count($c) !== count($d) || |
||
17 | count($d) !== count($a)) { |
||
18 | return 0; |
||
19 | } |
||
20 | [$ans, $map] = [0, []]; |
||
21 | foreach ($a as $v1) { |
||
22 | foreach ($b as $v2) { |
||
23 | $sum = $v1 + $v2; |
||
24 | $map[$sum] = ($map[$sum] ?? 0) + 1; |
||
25 | } |
||
26 | } |
||
27 | |||
28 | foreach ($c as $v1) { |
||
29 | foreach ($d as $v2) { |
||
30 | $sum = -($v1 + $v2); |
||
31 | $ans += $map[$sum] ?? 0; |
||
32 | } |
||
33 | } |
||
34 | |||
35 | return $ans; |
||
36 | } |
||
38 |