Conditions | 11 |
Paths | 16 |
Total Lines | 32 |
Code Lines | 22 |
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 |
||
29 | public static function intersection2(array $p, array $q): array |
||
30 | { |
||
31 | if (empty($p) || empty($q)) { |
||
32 | return []; |
||
33 | } |
||
34 | sort($p); |
||
35 | sort($q); |
||
36 | $i = $j = 0; |
||
37 | [$m, $n] = [count($p), count($q)]; |
||
38 | $ans = $set = []; |
||
39 | while ($i < $m && $j < $n) { |
||
40 | [$v1, $v2] = [$p[$i], $q[$j]]; |
||
41 | if ($v1 < $v2) { |
||
42 | $i++; |
||
43 | } elseif ($v1 > $v2) { |
||
44 | $j++; |
||
45 | } else { |
||
46 | if (!self::contains($set, $v1)) { |
||
47 | array_push($set, $v1); |
||
48 | } |
||
49 | $i++; |
||
50 | $j++; |
||
51 | } |
||
52 | } |
||
53 | |||
54 | foreach ($p as $v) { |
||
55 | if (self::contains($set, $v) && !self::contains($ans, $v)) { |
||
56 | array_push($ans, $v); |
||
57 | } |
||
58 | } |
||
59 | |||
60 | return $ans; |
||
61 | } |
||
99 |