| Conditions | 13 |
| Paths | 7 |
| Total Lines | 39 |
| Code Lines | 29 |
| 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 |
||
| 50 | public static function spiralOrder2(array $matrix): array |
||
| 51 | { |
||
| 52 | if (empty($matrix) || empty($matrix[0])) { |
||
| 53 | return $matrix; |
||
| 54 | } |
||
| 55 | [$ans, $dir] = [[], 0]; |
||
| 56 | [$rowi, $rown] = [0, count($matrix) - 1]; |
||
| 57 | [$coli, $coln] = [0, count($matrix[0]) - 1]; |
||
| 58 | while ($rowi <= $rown && $coli <= $coln) { |
||
| 59 | switch ($dir) { |
||
| 60 | case 0: |
||
| 61 | for ($col = $coli; $col <= $coln; $col++) { |
||
| 62 | array_push($ans, $matrix[$rowi][$col]); |
||
| 63 | } |
||
| 64 | $rowi++; |
||
| 65 | break; |
||
| 66 | case 1: |
||
| 67 | for ($row = $rowi; $row <= $rown; $row++) { |
||
| 68 | array_push($ans, $matrix[$row][$coln]); |
||
| 69 | } |
||
| 70 | $coln--; |
||
| 71 | break; |
||
| 72 | case 2: |
||
| 73 | for ($col = $coln; $col >= $coli; $col--) { |
||
| 74 | array_push($ans, $matrix[$rown][$col]); |
||
| 75 | } |
||
| 76 | $rown--; |
||
| 77 | break; |
||
| 78 | case 3: |
||
| 79 | for ($row = $rown; $row >= $rowi; $row--) { |
||
| 80 | array_push($ans, $matrix[$row][$coli]); |
||
| 81 | } |
||
| 82 | $coli++; |
||
| 83 | break; |
||
| 84 | } |
||
| 85 | $dir = ($dir + 1) % 4; |
||
| 86 | } |
||
| 87 | |||
| 88 | return $ans; |
||
| 89 | } |
||
| 91 |