Conditions | 21 |
Paths | 4 |
Total Lines | 77 |
Code 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 |
||
114 | protected function normalize($data) |
||
115 | { |
||
116 | if (is_array($data) || $data instanceof \Traversable) { |
||
117 | $normalized = array(); |
||
118 | |||
119 | $count = 1; |
||
120 | foreach ($data as $key => $value) { |
||
121 | if ($count++ >= 1000) { |
||
122 | $normalized['...'] = 'Over 1000 items, aborting normalization'; |
||
123 | break; |
||
124 | } |
||
125 | |||
126 | switch ($key) { |
||
127 | case 'message': |
||
128 | $normalized['@m'] = $value; |
||
129 | if (!(strpos($value, '{') === false)) { |
||
130 | $normalized['@mt'] = $value; |
||
131 | } |
||
132 | break; |
||
133 | |||
134 | case 'datetime': |
||
135 | if ($value instanceof \DateTime) { |
||
136 | $value = $value->format(DateTime::ISO8601); |
||
137 | } |
||
138 | $normalized['@t'] = $value; |
||
139 | break; |
||
140 | |||
141 | case 'level': |
||
142 | $normalized['@l'] = $this->logLevelMap[$value]; |
||
143 | $normalized['LogLevelCode'] = $value; |
||
144 | break; |
||
145 | case 'level_name': |
||
146 | break; |
||
147 | |||
148 | case 'extra': |
||
149 | $normalizedArray = $this->normalize($value); |
||
150 | |||
151 | if ($this->extractExtras) { |
||
152 | $normalized = array_merge($normalizedArray, $normalized); |
||
|
|||
153 | } else { |
||
154 | $normalized['Extra'] = $normalizedArray; |
||
155 | } |
||
156 | break; |
||
157 | |||
158 | case 'context': |
||
159 | $exception = $this->extractException($value); |
||
160 | $normalizedArray = $this->normalize($value); |
||
161 | |||
162 | if ($this->extractContext) { |
||
163 | $normalized = array_merge($normalizedArray, $normalized); |
||
164 | } else { |
||
165 | $normalized['Context'] = $normalizedArray; |
||
166 | } |
||
167 | |||
168 | if ($exception !== null) { |
||
169 | if (($exception instanceof Exception || $exception instanceof Throwable)) { |
||
170 | $exception = $this->normalizeException($exception); |
||
171 | } |
||
172 | |||
173 | $normalized['@x'] = $exception; |
||
174 | } |
||
175 | break; |
||
176 | |||
177 | default: |
||
178 | $normalized[is_int($key) ? $key : SeqCompactJsonFormatter::ConvertSnakeCaseToPascalCase($key)] = $this->normalize($value); |
||
179 | break; |
||
180 | } |
||
181 | } |
||
182 | |||
183 | return $normalized; |
||
184 | } |
||
185 | |||
186 | if ($data instanceof Exception || $data instanceof Throwable) { |
||
187 | return $this->normalizeException($data); |
||
188 | } |
||
189 | |||
190 | return $data; |
||
191 | } |
||
192 | } |