| Conditions | 13 |
| Paths | 25 |
| Total Lines | 23 |
| Code Lines | 15 |
| 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 |
||
| 9 | public static function myAtoi(string $s): int |
||
| 10 | { |
||
| 11 | $s = trim($s); |
||
| 12 | $n = strlen($s); |
||
| 13 | if ($n === 0) { |
||
| 14 | return 0; |
||
| 15 | } |
||
| 16 | [$sign, $base, $i] = [1, 0, 0]; |
||
| 17 | while ($i < $n && $s[$i] === ' ') { |
||
| 18 | $i++; |
||
| 19 | } |
||
| 20 | if ($i < $n && ($s[$i] === '+' || $s[$i] === '-')) { |
||
| 21 | $sign = ($s[$i++] === '+') ? 1 : -1; |
||
| 22 | } |
||
| 23 | [$min, $max] = [-2 ** 31, 2 ** 31 - 1]; // -2147483648~2147483647 |
||
| 24 | while ($i < $n && is_numeric($s[$i])) { |
||
| 25 | if ($base > $max || $base < $min) { |
||
| 26 | return $sign === 1 ? $max : $min; |
||
| 27 | } |
||
| 28 | $base = 10 * $base + ($s[$i++] - '0'); |
||
| 29 | } |
||
| 30 | |||
| 31 | return $base * $sign; |
||
| 32 | } |
||
| 34 |