Conditions | 12 |
Paths | 259 |
Total Lines | 47 |
Code Lines | 26 |
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 |
||
36 | public static function imageSmoother2(array $arr): array |
||
37 | { |
||
38 | $res = [[]]; |
||
39 | if (empty($arr)) { |
||
40 | return $res; |
||
41 | } |
||
42 | |||
43 | foreach ($arr as $i => $item) { |
||
44 | foreach ($item as $j => $v) { |
||
45 | $tmp = []; |
||
46 | |||
47 | // current row |
||
48 | $tmp[] = $v; |
||
49 | if (isset($item[$j - 1])) { |
||
50 | $tmp[] = $item[$j - 1]; |
||
51 | } |
||
52 | if (isset($item[$j + 1])) { |
||
53 | $tmp[] = $item[$j + 1]; |
||
54 | } |
||
55 | |||
56 | // previous row |
||
57 | if (isset($arr[$i - 1][$j])) { |
||
58 | $tmp[] = $arr[$i - 1][$j]; |
||
59 | } |
||
60 | if (isset($arr[$i - 1][$j - 1])) { |
||
61 | $tmp[] = $arr[$i - 1][$j - 1]; |
||
62 | } |
||
63 | if (isset($arr[$i - 1][$j + 1])) { |
||
64 | $tmp[] = $arr[$i - 1][$j + 1]; |
||
65 | } |
||
66 | |||
67 | // next row |
||
68 | if (isset($arr[$i + 1][$j])) { |
||
69 | $tmp[] = $arr[$i + 1][$j]; |
||
70 | } |
||
71 | if (isset($arr[$i + 1][$j - 1])) { |
||
72 | $tmp[] = $arr[$i + 1][$j - 1]; |
||
73 | } |
||
74 | if (isset($arr[$i + 1][$j + 1])) { |
||
75 | $tmp[] = $arr[$i + 1][$j + 1]; |
||
76 | } |
||
77 | $tmp = (int) floor(array_sum($tmp) / count($tmp)); |
||
78 | $res[$i][$j] = $tmp; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | return $res; |
||
83 | } |
||
85 |