Conditions | 23 |
Paths | 23 |
Total Lines | 68 |
Code Lines | 42 |
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 (\in_array($k, ['path', 'prefix'], true) && \is_string($v)) { |
||
86 | $v = '/'.\ltrim($v, '/'); |
||
87 | } |
||
88 | |||
89 | if (!\is_int($k) || 1 !== $k - $j) { |
||
90 | $code .= debugFormat($k, $subIndent).' => '; |
||
91 | } |
||
92 | |||
93 | if (\is_int($k) && $k > $j) { |
||
94 | $j = $k; |
||
95 | } |
||
96 | $code .= debugFormat($v, $subIndent).",\n"; |
||
97 | } |
||
98 | |||
99 | return "[\n".$code.$indent.']'; |
||
100 | } |
||
101 | |||
102 | if (\is_object($value)) { |
||
103 | if ($value instanceof ResourceHandler) { |
||
104 | return 'new ResourceHandler('.debugFormat($value(''), $indent).')'; |
||
105 | } |
||
106 | |||
107 | if ($value instanceof \stdClass) { |
||
108 | return '(object) '.debugFormat((array) $value, $indent); |
||
109 | } |
||
110 | |||
111 | if (!$value instanceof \Closure) { |
||
112 | return $value::class; |
||
113 | } |
||
114 | $ref = new \ReflectionFunction($value); |
||
115 | |||
116 | if (0 === $ref->getNumberOfParameters()) { |
||
117 | return 'fn() => '.debugFormat($ref->invoke(), $indent); |
||
118 | } |
||
119 | } |
||
120 | |||
121 | throw new \UnexpectedValueException(\sprintf('Cannot format value of type "%s".', \get_debug_type($value))); |
||
122 | } |
||
123 |