Conditions | 12 |
Paths | 6 |
Total Lines | 35 |
Code Lines | 21 |
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 maximalSquare(array $matrix): int |
||
10 | { |
||
11 | [$m, $n, $ans] = [count($matrix), count($matrix[0]), 0]; |
||
12 | if ($m <= 0 || $n <= 0) { |
||
13 | return $ans; |
||
14 | } |
||
15 | $helper = static function (array $array, int $k) { |
||
16 | $n = count($array); |
||
17 | if ($n < $k) { |
||
18 | return 0; |
||
19 | } |
||
20 | $counter = 0; |
||
21 | for ($i = 0; $i < $n; $i++) { |
||
22 | $counter = $array[$i] !== $k ? 0 : ($counter + 1); |
||
23 | if ($counter === $k) { |
||
24 | return $k * $k; |
||
25 | } |
||
26 | } |
||
27 | |||
28 | return 0; |
||
29 | }; |
||
30 | |||
31 | for ($i = 0; $i < $m; $i++) { |
||
32 | $tmp = array_fill(0, $n, 0); |
||
33 | for ($j = $i; $j < $n; $j++) { |
||
34 | for ($k = 0; $k < $n; $k++) { |
||
35 | if (isset($matrix[$j][$k]) && $matrix[$j][$k] === 1) { |
||
36 | $tmp[$k]++; |
||
37 | } |
||
38 | } |
||
39 | $ans = max($ans, $helper($tmp, $j - $i + 1)); |
||
40 | } |
||
41 | } |
||
42 | |||
43 | return $ans; |
||
44 | } |
||
88 |