Conditions | 10 |
Paths | 12 |
Total Lines | 29 |
Code Lines | 22 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
16 | public static function toInt($input): int |
||
17 | { |
||
18 | $displayValue = gettype($input); |
||
19 | switch (gettype($input)) { |
||
20 | case 'integer': |
||
21 | return $input; |
||
22 | case 'boolean': |
||
23 | return $input ? 1 : 0; |
||
24 | case 'double': |
||
25 | if (round($input) === $input) { |
||
26 | return (int) $input; |
||
27 | } |
||
28 | $displayValue = $input; |
||
29 | break; |
||
30 | case 'object': |
||
31 | if (!is_callable([$input, '__toString'])) { |
||
32 | $displayValue = 'object ' . (new ReflectionClass($input))->getShortName(); |
||
33 | break; |
||
34 | } |
||
35 | $input = (string) $input; |
||
36 | // no break |
||
37 | case 'string': |
||
38 | if (!preg_match('/^\s*[1-9][0-9]*(\.0+){0,1}\s*$/', $input)) { |
||
39 | $displayValue = $input; |
||
40 | break; |
||
41 | } |
||
42 | return (int) $input; |
||
43 | } |
||
44 | throw new CouldNotConvertException('int', $displayValue); |
||
45 | } |
||
120 |