| Conditions | 21 |
| Paths | 19 |
| Total Lines | 64 |
| Code Lines | 40 |
| 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 declare(strict_types=1); |
||
| 54 | function debugFormat(mixed $value, $indent = ''): string |
||
| 55 | { |
||
| 56 | switch (true) { |
||
| 57 | case \is_int($value) || \is_float($value): |
||
| 58 | return \var_export($value, true); |
||
| 59 | case [] === $value: |
||
| 60 | return '[]'; |
||
| 61 | case false === $value: |
||
| 62 | return 'false'; |
||
| 63 | case true === $value: |
||
| 64 | return 'true'; |
||
| 65 | case null === $value: |
||
| 66 | return 'null'; |
||
| 67 | case '' === $value: |
||
| 68 | return "''"; |
||
| 69 | case $value instanceof \UnitEnum: |
||
| 70 | return \ltrim(\var_export($value, true), '\\'); |
||
| 71 | } |
||
| 72 | $subIndent = $indent.' '; |
||
| 73 | |||
| 74 | if (\is_string($value)) { |
||
| 75 | return \sprintf("'%s'", \addcslashes($value, "'\\")); |
||
| 76 | } |
||
| 77 | |||
| 78 | if (\is_array($value)) { |
||
| 79 | $j = -1; |
||
| 80 | $code = ''; |
||
| 81 | |||
| 82 | foreach ($value as $k => $v) { |
||
| 83 | $code .= $subIndent; |
||
| 84 | |||
| 85 | if (!\is_int($k) || 1 !== $k - $j) { |
||
| 86 | $code .= debugFormat($k, $subIndent).' => '; |
||
| 87 | } |
||
| 88 | |||
| 89 | if (\is_int($k) && $k > $j) { |
||
| 90 | $j = $k; |
||
| 91 | } |
||
| 92 | $code .= debugFormat($v, $subIndent).",\n"; |
||
| 93 | } |
||
| 94 | |||
| 95 | return "[\n".$code.$indent.']'; |
||
| 96 | } |
||
| 97 | |||
| 98 | if (\is_object($value)) { |
||
| 99 | if ($value instanceof ResourceHandler) { |
||
| 100 | return 'new ResourceHandler('.debugFormat($value(''), $indent).')'; |
||
| 101 | } |
||
| 102 | |||
| 103 | if ($value instanceof \stdClass) { |
||
| 104 | return '(object) '.debugFormat((array) $value, $indent); |
||
| 105 | } |
||
| 106 | |||
| 107 | if (!$value instanceof \Closure) { |
||
| 108 | return $value::class; |
||
| 109 | } |
||
| 110 | $ref = new \ReflectionFunction($value); |
||
| 111 | |||
| 112 | if (0 === $ref->getNumberOfParameters()) { |
||
| 113 | return 'fn() => '.debugFormat($ref->invoke(), $indent); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | throw new \UnexpectedValueException(\sprintf('Cannot format value of type "%s".', \get_debug_type($value))); |
||
| 118 | } |
||
| 119 |