Conditions | 14 |
Paths | 27 |
Total Lines | 45 |
Code Lines | 32 |
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 |
||
43 | public static function export(mixed $value, string $indent = ''): string |
||
44 | { |
||
45 | switch (true) { |
||
46 | case [] === $value: |
||
47 | return '[]'; |
||
48 | case \is_array($value): |
||
49 | $j = -1; |
||
50 | $code = ($t = \count($value, \COUNT_RECURSIVE)) > 15 ? "[\n" : '['; |
||
51 | $subIndent = $t > 15 ? $indent.' ' : $indent = ''; |
||
52 | |||
53 | foreach ($value as $k => $v) { |
||
54 | $code .= $subIndent; |
||
55 | |||
56 | if (!\is_int($k) || $k !== ++$j) { |
||
57 | $code .= self::export($k, $subIndent).' => '; |
||
58 | } |
||
59 | |||
60 | $code .= self::export($v, $subIndent).($t > 15 ? ",\n" : ', '); |
||
61 | } |
||
62 | |||
63 | return \rtrim($code, ', ').$indent.']'; |
||
64 | case $value instanceof ResourceHandler: |
||
65 | return $value::class.'('.self::export($value(''), $indent).')'; |
||
66 | case $value instanceof \stdClass: |
||
67 | return '(object) '.self::export((array) $value, $indent); |
||
68 | case $value instanceof RouteCollection: |
||
69 | return $value::class.'::__set_state('.self::export([ |
||
70 | 'routes' => $value->getRoutes(), |
||
71 | 'defaultIndex' => $value->count() - 1, |
||
72 | 'sorted' => true, |
||
73 | ], $indent).')'; |
||
74 | case \is_object($value): |
||
75 | if (\method_exists($value, '__set_state')) { |
||
76 | return $value::class.'::__set_state('.self::export( |
||
77 | \array_merge(...\array_map(function (\ReflectionProperty $v) use ($value): array { |
||
78 | $v->setAccessible(true); |
||
79 | |||
80 | return [$v->getName() => $v->getValue($value)]; |
||
81 | }, (new \ReflectionObject($value))->getProperties())) |
||
82 | ); |
||
83 | } |
||
84 | return 'unserialize(\''.\serialize($value).'\')'; |
||
85 | } |
||
86 | |||
87 | return \var_export($value, true); |
||
88 | } |
||
168 |