Conditions | 13 |
Paths | 13 |
Total Lines | 23 |
Code Lines | 16 |
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 thirdMax(array $nums): int |
||
10 | { |
||
11 | if (empty($nums)) { |
||
12 | return 0; |
||
13 | } |
||
14 | $a = $b = $c = null; |
||
15 | foreach ($nums as $num) { |
||
16 | if ($num === $a || $num === $b || $num === $c) { |
||
17 | continue; |
||
18 | } |
||
19 | if ($a === null || $num > $a) { |
||
20 | $c = $b; |
||
21 | $b = $a; |
||
22 | $a = $num; |
||
23 | } elseif ($b === null || $num > $b) { |
||
24 | $c = $b; |
||
25 | $b = $num; |
||
26 | } elseif ($c === null || $num > $c) { |
||
27 | $c = $num; |
||
28 | } |
||
29 | } |
||
30 | |||
31 | return $c === null ? $a : $c; |
||
|
|||
32 | } |
||
34 |