| Conditions | 23 |
| Paths | 4 |
| Total Lines | 81 |
| Code Lines | 53 |
| 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 | if (is_array($value)) { |
||
| 150 | $normalizedArray = $this->normalize($value); |
||
| 151 | |||
| 152 | if ($this->extractExtras) { |
||
| 153 | $normalized = array_merge($normalizedArray, $normalized); |
||
| 154 | } else { |
||
| 155 | $normalized['Extra'] = $normalizedArray; |
||
| 156 | } |
||
| 157 | } else { |
||
| 158 | $normalized[is_int($key) ? $key : SeqCompactJsonFormatter::ConvertSnakeCaseToPascalCase($key)] = $this->normalize($value); |
||
| 159 | } |
||
| 160 | break; |
||
| 161 | |||
| 162 | case 'context': |
||
| 163 | $exception = $this->extractException($value); |
||
| 164 | $normalizedArray = $this->normalize($value); |
||
| 165 | |||
| 166 | if ($this->extractContext) { |
||
| 167 | $normalized = array_merge($normalizedArray, $normalized); |
||
|
|
|||
| 168 | } else { |
||
| 169 | $normalized['Context'] = $normalizedArray; |
||
| 170 | } |
||
| 171 | |||
| 172 | if ($exception !== null) { |
||
| 173 | if (($exception instanceof Exception || $exception instanceof Throwable)) { |
||
| 174 | $exception = $this->normalizeException($exception); |
||
| 175 | } |
||
| 176 | |||
| 177 | $normalized['@x'] = $exception; |
||
| 178 | } |
||
| 179 | break; |
||
| 180 | |||
| 181 | default: |
||
| 182 | $normalized[is_int($key) ? $key : SeqCompactJsonFormatter::ConvertSnakeCaseToPascalCase($key)] = $this->normalize($value); |
||
| 183 | break; |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | return $normalized; |
||
| 188 | } |
||
| 189 | |||
| 190 | if ($data instanceof \Exception || $data instanceof \Throwable) { |
||
| 191 | return $this->normalizeException($data); |
||
| 192 | } |
||
| 193 | |||
| 194 | return $data; |
||
| 195 | } |
||
| 196 | } |