| Conditions | 13 |
| Paths | 28 |
| 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 |
||
| 53 | public static function export($obj, int $depth = 2, bool $short = false): string |
||
| 54 | { |
||
| 55 | if (static::$exporter === null) { |
||
| 56 | static::$exporter = new Exporter(); |
||
| 57 | } |
||
| 58 | |||
| 59 | $depth = ((int) $depth < 0) ? 0 : (int) $depth; |
||
| 60 | $short = (bool) $short; |
||
| 61 | $str = null; |
||
| 62 | |||
| 63 | if (is_object($obj)) { |
||
| 64 | $className = static::getClass($obj, $short); |
||
| 65 | |||
| 66 | if ($depth <= 0) { |
||
| 67 | // Do not show properties |
||
| 68 | $content = '{...}'; |
||
| 69 | } else { |
||
| 70 | $arrayObject = static::$exporter->toArray($obj); |
||
| 71 | // Initial content without deep |
||
| 72 | $content = static::export($arrayObject, 1, $short); |
||
| 73 | // Replace array braces and separator |
||
| 74 | $content = str_replace([' => ', '[ ', ' ]'], [': ', '{ ', ' }'], $content); |
||
| 75 | |||
| 76 | if ($depth > 1) { |
||
| 77 | foreach ($arrayObject as $key => $value) { |
||
| 78 | $format = '{0}: {1}'; |
||
| 79 | |||
| 80 | if (is_object($value)) { |
||
| 81 | $sclass = static::getClass($value, $short); |
||
| 82 | |||
| 83 | $sVal = $sclass . '({...})'; |
||
| 84 | } elseif (is_array($value)) { |
||
| 85 | $sVal = '[...]'; |
||
| 86 | } else { |
||
| 87 | continue; |
||
| 88 | } |
||
| 89 | |||
| 90 | $search = Text::format($format, $key, $sVal); |
||
| 91 | $replacement = Text::format($format, $key, static::export($value, $depth - 1, $short)); |
||
| 92 | |||
| 93 | $content = str_replace($search, $replacement, $content); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | $str = Text::format('{0}({1})', $className, $content); |
||
| 99 | $str = str_replace(', }', ' }', $str); |
||
| 100 | } elseif (is_array($obj)) { |
||
| 101 | if ($depth <= 0) { |
||
| 102 | $str = count($obj) > 0 ? '[...]' : ''; |
||
| 103 | } else { |
||
| 104 | $str = ''; |
||
| 105 | foreach ($obj as $key => $value) { |
||
| 106 | // Export all items recursively |
||
| 107 | $str .= Text::format('{0} => {1}, ', $key, static::export($value, $depth - 1, $short)); |
||
| 108 | } |
||
| 109 | $str = Text::format('[ {0} ]', $str); |
||
| 110 | $str = str_replace(', ]', ' ]', $str); |
||
| 111 | } |
||
| 112 | } else { |
||
| 113 | $str = static::$exporter->export($obj); |
||
| 114 | } |
||
| 115 | |||
| 116 | return $str; |
||
| 117 | } |
||
| 144 |