Completed
Push — dev ( d94d6c...4443cf )
by Markus
02:32
created

SeqCompactJsonFormatter::processLogRecord()   C

Complexity

Conditions 20
Paths 21

Size

Total Lines 63
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 20
eloc 42
nc 21
nop 4
dl 0
loc 63
rs 5.9952
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
            return $this->normalizeArray($data);
0 ignored issues
show
Bug introduced by
It seems like $data can also be of type Traversable; however, parameter $array of Msschl\Monolog\Formatter...atter::normalizeArray() 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

117
            return $this->normalizeArray(/** @scrutinizer ignore-type */ $data);
Loading history...
118
        }
119
120
        if ($data instanceof \Exception || $data instanceof \Throwable) {
121
            return $this->normalizeException($data);
122
        }
123
124
        return $data;
125
    }
126
127
    private function normalizeArray(array $array)
128
    {
129
        $normalized = array();
130
131
        $count = 1;
132
        foreach ($array as $key => $value) {
133
            if ($count++ >= 1000) {
134
                $normalized['...'] = 'Over 1000 items, aborting normalization';
135
                break;
136
            }
137
138
            $normalized = $this->processLogRecord($normalized, $key, $value, $count);
139
        }
140
141
        return $normalized;
142
    }
143
144
    private function processLogRecord($array, $key, $value, $count)
0 ignored issues
show
Unused Code introduced by
The parameter $count is not used and could be removed. ( Ignorable by Annotation )

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

144
    private function processLogRecord($array, $key, $value, /** @scrutinizer ignore-unused */ $count)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
145
    {
146
        switch ($key) {
147
            case 'message':
148
                $array['@m'] = $value;
149
                if (!(strpos($value, '{') === false)) {
150
                    $array['@mt'] = $value;
151
                }
152
                break;
153
154
            case 'datetime':
155
                if ($value instanceof \DateTime) {
156
                    $value = $value->format(DateTime::ISO8601);
157
                }
158
                $array['@t'] = $value;
159
                break;
160
161
            case 'level':
162
                $array['@l'] = $this->logLevelMap[$value];
163
                $array['LogLevelCode'] = $value;
164
                break;
165
            case 'level_name':
166
                break;
167
168
            case 'extra':
169
                if (is_array($value) && $normalizedArray = $this->normalize($value) && is_array($normalizedArray)) {
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: $normalizedArray = ($thi...rray($normalizedArray)), Probably Intended Meaning: ($normalizedArray = $thi...array($normalizedArray)
Loading history...
Comprehensibility Best Practice introduced by
The variable $normalizedArray seems to be never defined.
Loading history...
170
                    if ($this->extractExtras) {
171
                        $array = array_merge($normalizedArray, $array);
0 ignored issues
show
Bug introduced by
$normalizedArray of type true is incompatible with the type array expected by parameter $array1 of array_merge(). ( Ignorable by Annotation )

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

171
                        $array = array_merge(/** @scrutinizer ignore-type */ $normalizedArray, $array);
Loading history...
172
                    } else {
173
                        $array['Extra'] = $normalizedArray;
174
                    }
175
                }
176
                break;
177
178
            case 'context':
179
                if (is_array($value)) {
180
                    $exception = $this->extractException($value);
181
                    $normalizedArray = $this->normalize($value);
182
183
                    if (is_array($normalizedArray)) {
184
                        if ($this->extractContext) {
185
                            $array = array_merge($normalizedArray, $array);
186
                        } else {
187
                            $array['Context'] = $normalizedArray;
188
                        }
189
                    }
190
191
                    if ($exception !== null) {
192
                        if (($exception instanceof \Exception || $exception instanceof \Throwable)) {
0 ignored issues
show
introduced by
$exception is always a sub-type of Throwable.
Loading history...
193
                            $exception = $this->normalizeException($exception);
194
                        }
195
196
                        $array['@x'] = $exception;
197
                    }
198
                }
199
                break;
200
201
            default:
202
                $array[is_int($key) ? $key : SeqCompactJsonFormatter::ConvertSnakeCaseToPascalCase($key)] = $this->normalize($value);
203
                break;
204
        }
205
206
        return $array;
207
    }
208
}