Conditions | 13 |
Paths | 28 |
Total Lines | 67 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
49 | public static function export($obj, $depth = 2, $short = false) |
||
50 | { |
||
51 | if (static::$exporter === null) { |
||
52 | static::$exporter = new Exporter(); |
||
53 | } |
||
54 | |||
55 | $depth = ((integer) $depth < 0) ? 0 : (integer) $depth; |
||
56 | $short = (boolean) $short; |
||
57 | $str = null; |
||
|
|||
58 | |||
59 | if (is_object($obj)) { |
||
60 | $className = static::getClass($obj, $short); |
||
61 | |||
62 | if ($depth <= 0) { |
||
63 | // Do not show properties |
||
64 | $content = '{...}'; |
||
65 | } else { |
||
66 | $arrayObject = static::$exporter->toArray($obj); |
||
67 | // Initial content without deep |
||
68 | $content = static::export($arrayObject, 1, $short); |
||
69 | // Replace array braces and separator |
||
70 | $content = str_replace([' => ', '[ ', ' ]'], [': ', '{ ', ' }'], $content); |
||
71 | |||
72 | // var_dump($content, $depth); |
||
73 | |||
74 | if ($depth > 1) { |
||
75 | foreach ($arrayObject as $key => $value) { |
||
76 | $format = '{0}: {1}'; |
||
77 | |||
78 | if (is_object($value)) { |
||
79 | $sclass = static::getClass($value, $short); |
||
80 | |||
81 | $sVal = "$sclass({...})"; |
||
82 | } elseif (is_array($value)) { |
||
83 | $sVal = '[...]'; |
||
84 | } else { |
||
85 | continue; |
||
86 | } |
||
87 | |||
88 | $search = String::format($format, $key, $sVal); |
||
89 | $replacement = String::format($format, $key, static::export($value, $depth - 1, $short)); |
||
90 | |||
91 | $content = str_replace($search, $replacement, $content); |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | |||
96 | $str = String::format('{0}({1})', $className, $content); |
||
97 | $str = str_replace(', }', ' }', $str); |
||
98 | } elseif (is_array($obj)) { |
||
99 | if ($depth <= 0) { |
||
100 | $str = count($obj) > 0 ? '[...]' : ''; |
||
101 | } else { |
||
102 | $str = ''; |
||
103 | foreach ($obj as $key => $value) { |
||
104 | // Export all items recursively |
||
105 | $str .= String::format('{0} => {1}, ', $key, static::export($value, $depth - 1, $short)); |
||
106 | } |
||
107 | $str = String::format('[ {0} ]', $str); |
||
108 | $str = str_replace(', ]', ' ]', $str); |
||
109 | } |
||
110 | } else { |
||
111 | $str = static::$exporter->export($obj); |
||
112 | } |
||
113 | |||
114 | return $str; |
||
115 | } |
||
116 | |||
142 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.