| Conditions | 11 |
| Paths | 18 |
| Total Lines | 39 |
| Code Lines | 21 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 spiralOrder(array $matrix): array |
||
| 10 | { |
||
| 11 | if (empty($matrix) || empty($matrix[0])) { |
||
| 12 | return $matrix; |
||
| 13 | } |
||
| 14 | $ans = []; |
||
| 15 | [$rowi, $rown] = [0, count($matrix) - 1]; |
||
| 16 | [$coli, $coln] = [0, count($matrix[0]) - 1]; |
||
| 17 | while ($rowi <= $rown && $coli <= $coln) { |
||
| 18 | // col: i -> n -> [rowi][j] |
||
| 19 | for ($j = $coli; $j <= $coln; $j++) { |
||
| 20 | array_push($ans, $matrix[$rowi][$j]); |
||
| 21 | } |
||
| 22 | $rowi++; |
||
| 23 | |||
| 24 | // row: i -> n -> [j][coln] |
||
| 25 | for ($j = $rowi; $j <= $rown; $j++) { |
||
| 26 | array_push($ans, $matrix[$j][$coln]); |
||
| 27 | } |
||
| 28 | $coln--; |
||
| 29 | |||
| 30 | if ($rowi <= $rown) { |
||
| 31 | // col: n -> i -> [rown][j] |
||
| 32 | for ($j = $coln; $j >= $coli; $j--) { |
||
| 33 | array_push($ans, $matrix[$rown][$j]); |
||
| 34 | } |
||
| 35 | } |
||
| 36 | $rown--; |
||
| 37 | |||
| 38 | if ($coli <= $coln) { |
||
| 39 | // row: n -> i -> [j][coln] |
||
| 40 | for ($j = $rown; $j >= $rowi; $j--) { |
||
| 41 | array_push($ans, $matrix[$j][$coli]); |
||
| 42 | } |
||
| 43 | } |
||
| 44 | $coli++; |
||
| 45 | } |
||
| 46 | |||
| 47 | return $ans; |
||
| 48 | } |
||
| 91 |