Conditions | 12 |
Paths | 24 |
Total Lines | 50 |
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 |
||
82 | public static function export($var, int $maxDepth) |
||
83 | { |
||
84 | $return = null; |
||
85 | $isObj = is_object($var); |
||
86 | |||
87 | if ($var instanceof Collection) { |
||
88 | $var = $var->toArray(); |
||
89 | } |
||
90 | |||
91 | if ($maxDepth === 0) { |
||
92 | return is_object($var) ? get_class($var) |
||
93 | : (is_array($var) ? 'Array(' . count($var) . ')' : $var); |
||
94 | } |
||
95 | |||
96 | if (is_array($var)) { |
||
97 | $return = []; |
||
98 | |||
99 | foreach ($var as $k => $v) { |
||
100 | $return[$k] = self::export($v, $maxDepth - 1); |
||
101 | } |
||
102 | |||
103 | return $return; |
||
104 | } |
||
105 | |||
106 | if (! $isObj) { |
||
107 | return $var; |
||
108 | } |
||
109 | |||
110 | $return = new stdClass(); |
||
111 | if ($var instanceof DateTimeInterface) { |
||
112 | $return->__CLASS__ = get_class($var); |
||
113 | $return->date = $var->format('c'); |
||
114 | $return->timezone = $var->getTimezone()->getName(); |
||
115 | |||
116 | return $return; |
||
117 | } |
||
118 | |||
119 | $return->__CLASS__ = self::getClass($var); |
||
120 | |||
121 | if ($var instanceof Proxy) { |
||
122 | $return->__IS_PROXY__ = true; |
||
123 | $return->__PROXY_INITIALIZED__ = $var->__isInitialized(); |
||
124 | } |
||
125 | |||
126 | if ($var instanceof ArrayObject || $var instanceof ArrayIterator) { |
||
127 | $return->__STORAGE__ = self::export($var->getArrayCopy(), $maxDepth - 1); |
||
128 | } |
||
129 | |||
130 | return self::fillReturnWithClassAttributes($var, $return, $maxDepth); |
||
131 | } |
||
132 | |||
177 |