Conditions | 14 |
Paths | 11 |
Total Lines | 28 |
Code Lines | 17 |
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 |
||
49 | public static function maxKilledEnemies2(array $grids): int |
||
50 | { |
||
51 | [$res, $m, $n] = [0, count($grids), count($grids[0])]; |
||
52 | if ($m <= 0 || $n <= 0) { |
||
53 | return $res; |
||
54 | } |
||
55 | $col = array_fill(0, $n, 0); |
||
56 | for ($i = 0; $i < $m; $i++) { |
||
57 | for ($j = 0; $j < $n; $j++) { |
||
58 | if ($j === 0 || $grids[$i][$j - 1] === 'W') { |
||
59 | $row = 0; |
||
60 | for ($k = $j; $k < $n && $grids[$i][$k] !== 'W'; $k++) { |
||
61 | $row += $grids[$i][$k] === 'E'; |
||
62 | } |
||
63 | } |
||
64 | if ($i === 0 || $grids[$i - 1][$j] === 'W') { |
||
65 | $col[$j] = 0; |
||
66 | for ($k = $i; $k < $m && $grids[$k][$j] !== 'W'; $k++) { |
||
67 | $col[$j] += $grids[$k][$j] === 'E'; |
||
68 | } |
||
69 | } |
||
70 | if ($grids[$i][$j] === '0') { |
||
71 | $res = max($res, $row + $col[$j]); |
||
|
|||
72 | } |
||
73 | } |
||
74 | } |
||
75 | |||
76 | return $res; |
||
77 | } |
||
79 |