| Conditions | 12 |
| Paths | 21 |
| Total Lines | 25 |
| Code Lines | 15 |
| 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 imageSmoother(array $arr): array |
||
| 10 | { |
||
| 11 | if (empty($arr)) { |
||
| 12 | return [[]]; |
||
| 13 | } |
||
| 14 | [$m, $n] = [count($arr), empty($arr[0]) ? 0 : count($arr[0])]; |
||
| 15 | $ans = array_fill(0, $m, array_fill(0, $n, 0)); |
||
| 16 | for ($i = 0; $i < $m; $i++) { |
||
| 17 | for ($j = 0; $j < $n; $j++) { |
||
| 18 | $cnt = 0; |
||
| 19 | for ($row = $i - 1; $row <= $i + 1; $row++) { |
||
| 20 | for ($col = $j - 1; $col <= $j + 1; $col++) { |
||
| 21 | if (0 <= $row && $row < $m && 0 <= $col && $col < $n) { |
||
| 22 | $ans[$i][$j] += $arr[$row][$col]; |
||
| 23 | $cnt++; |
||
| 24 | } |
||
| 25 | } |
||
| 26 | } |
||
| 27 | if ($cnt > 0) { |
||
| 28 | $ans[$i][$j] = (int) floor($ans[$i][$j] / $cnt); |
||
| 29 | } |
||
| 30 | } |
||
| 31 | } |
||
| 32 | |||
| 33 | return $ans; |
||
| 34 | } |
||
| 85 |