Passed
Push — dev ( 4443cf...7bc15a )
by Markus
02:04
created

SeqCompactJsonFormatter::normalize()   C

Complexity

Conditions 21
Paths 4

Size

Total Lines 77
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 21
eloc 50
nc 4
nop 1
dl 0
loc 77
rs 5.1538
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace Msschl\Monolog\Formatter;
4
5
use DateTime;
6
use Monolog\Formatter\FormatterInterface;
7
use Monolog\Formatter\JsonFormatter;
8
use Throwable;
9
10
/**
11
 * This file is part of the msschl\monolog-seq-handler package.
12
 *
13
 * Copyright (c) 2018 Markus Schlotbohm
14
 *
15
 * For the full copyright and license information, please view the LICENSE.md
16
 * file that was distributed with this source code.
17
 */
18
class SeqCompactJsonFormatter extends SeqBaseFormatter
19
{
20
21
    /**
22
     * The extract context flag.
23
     * Whether to extract the context array to the root or not.
24
     *
25
     * @var bool
26
     */
27
    protected $extractContext;
28
29
    /**
30
     * The extract extras flag.
31
     * Whether to extract the extras array to the root or not.
32
     *
33
     * @var bool
34
     */
35
    protected $extractExtras;
36
37
    /**
38
     * Initializes a new instance of the {@see SeqCompactJsonFormatter} class.
39
     *
40
     * @param  bool $extractContext Flag that indicates whether to extract the extras array
41
     *                              to the root or not.
42
     * @param  bool $extractExtras  Flag that indicates whether to extract the context array
43
     *                              to the root or not.
44
     */
45
	public function __construct(bool $extractContext = true, bool $extractExtras = true)
46
	{
47
        $this->appendNewline = false;
48
        $this->batchMode = JsonFormatter::BATCH_MODE_NEWLINES;
49
        $this->extractContext = $extractContext;
50
        $this->extractExtras = $extractExtras;
51
	}
52
53
    /**
54
     * Returns a string with the content type for the seq-formatter.
55
     *
56
     * @return string
57
     */
58
    public function getContentType() : string {
59
        return 'application/vnd.serilog.clef';
60
    }
61
62
    /**
63
     * Gets whether the flag extract content is set or not.
64
     *
65
     * @return bool
66
     */
67
    public function getExtractContent() : bool
68
    {
69
        return $this->extractContext;
70
    }
71
72
    /**
73
     * Sets the flag extract content.
74
     *
75
     * @param  bool $value The flag.
76
     * @return self
77
     */
78
    public function setExtractContent(bool $value)
79
    {
80
        $this->extractContext = $value;
81
82
        return $this;
83
    }
84
85
    /**
86
     * Gets whether the flag extract extras is set or not.
87
     *
88
     * @return bool
89
     */
90
    public function getExtractExtras()
91
    {
92
        return $this->extractExtras;
93
    }
94
95
    /**
96
     * Sets the flag extract extras.
97
     *
98
     * @param  bool $value The flag.
99
     * @return self
100
     */
101
    public function setExtractExtras(bool $value)
102
    {
103
        $this->extractExtras = $value;
104
105
        return $this;
106
    }
107
108
    /**
109
     * Normalizes given $data.
110
     *
111
     * @param mixed $data The data to normalize.
112
     * @return mixed
113
     */
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);
0 ignored issues
show
Bug introduced by
It seems like $normalizedArray can also be of type string; however, parameter $array1 of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

152
                            $normalized = array_merge(/** @scrutinizer ignore-type */ $normalizedArray, $normalized);
Loading history...
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)) {
0 ignored issues
show
Bug introduced by
The type Msschl\Monolog\Formatter\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
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
}