| Conditions | 11 |
| Paths | 7 |
| Total Lines | 18 |
| Code Lines | 12 |
| 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 tictactoe(array $moves): string |
||
| 10 | { |
||
| 11 | if (empty($moves)) { |
||
| 12 | return ''; |
||
| 13 | } |
||
| 14 | $row = $col = array_fill(0, 2, array_fill(0, 3, 0)); |
||
| 15 | $d1 = $d2 = array_fill(0, 2, 0); |
||
| 16 | foreach ($moves as $k => $v) { |
||
| 17 | [$r, $c, $i] = [$v[0], $v[1], $k % 2]; |
||
| 18 | if (++$row[$i][$r] === 3 || |
||
| 19 | ++$col[$i][$c] === 3 || |
||
| 20 | ($r === $c && ++$d1[$i] === 3) || |
||
| 21 | ($r + $c === 2 && ++$d2[$i] === 3)) { |
||
| 22 | return $i === 0 ? 'A' : 'B'; |
||
| 23 | } |
||
| 24 | } |
||
| 25 | |||
| 26 | return count($moves) === 9 ? 'Draw' : 'Pending'; |
||
| 27 | } |
||
| 64 |