Conditions | 10 |
Paths | 55 |
Total Lines | 33 |
Code Lines | 22 |
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 |
||
29 | public static function tictactoe2(array $moves): string |
||
30 | { |
||
31 | if (empty($moves)) { |
||
32 | return ''; |
||
33 | } |
||
34 | $n = 8; |
||
35 | $a = $b = array_fill(0, $n, 0); |
||
36 | foreach ($moves as $k => $v) { |
||
37 | [$r, $c] = [$v[0], $v[1]]; |
||
38 | if ($k % 2 === 0) { |
||
39 | $player = & $a; |
||
40 | } else { |
||
41 | $player = & $b; |
||
42 | } |
||
43 | $player[$r]++; |
||
44 | $player[$c + 3]++; |
||
45 | if ($r === $c) { |
||
46 | $player[6]++; |
||
47 | } |
||
48 | if ($r + $c === 2) { |
||
49 | $player[7]++; |
||
50 | } |
||
51 | } |
||
52 | for ($i = 0; $i < $n; $i++) { |
||
53 | if ($a[$i] === 3) { |
||
54 | return 'A'; |
||
55 | } |
||
56 | if ($b[$i] === 3) { |
||
57 | return 'B'; |
||
58 | } |
||
59 | } |
||
60 | |||
61 | return count($moves) === 9 ? 'Draw' : 'Pending'; |
||
62 | } |
||
64 |