| Conditions | 10 |
| Paths | 9 |
| Total Lines | 29 |
| Code Lines | 15 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 13 |
| CRAP Score | 10 |
| Changes | 2 | ||
| Bugs | 1 | 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 |
||
| 40 | private function varToString(mixed $var): string |
||
| 41 | 2 | { |
|
| 42 | 2 | if (null === $var) { |
|
| 43 | 2 | return 'null'; |
|
| 44 | 2 | } |
|
| 45 | 2 | ||
| 46 | if (\is_string($var)) { |
||
| 47 | return '"'.$var.'"'; |
||
| 48 | } |
||
| 49 | |||
| 50 | if (\is_bool($var)) { |
||
| 51 | return $var ? 'true' : 'false'; |
||
| 52 | 3 | } |
|
| 53 | |||
| 54 | if (\is_float($var)) { |
||
| 55 | return 0.0 === $var |
||
|
|
|||
| 56 | ? '0.0' |
||
| 57 | 2 | : (string) $var; |
|
| 58 | } |
||
| 59 | 2 | ||
| 60 | 1 | if (\is_object($var) && method_exists($var, '__toString')) { |
|
| 61 | return (string) $var; |
||
| 62 | } |
||
| 63 | 2 | ||
| 64 | 2 | if (\is_scalar($var)) { |
|
| 65 | return (string) $var; |
||
| 66 | } |
||
| 67 | 1 | ||
| 68 | 1 | throw new \Exception('Unable to transform var to string.'); |
|
| 69 | } |
||
| 71 |