Exception::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 1
b 0
f 0
nc 2
nop 8
dl 0
loc 24
ccs 12
cts 12
cp 1
crap 3
rs 9.9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace ZoiloMora\ElasticAPM\Events\Error;
4
5
use ZoiloMora\ElasticAPM\Events\Common\StacktraceFrame;
6
use ZoiloMora\ElasticAPM\Helper\Encoding;
7
use ZoiloMora\ElasticAPM\Utils\Assert;
8
9
/**
10
 * Information about the originally thrown error.
11
 */
12
final class Exception implements \JsonSerializable
13
{
14
    /**
15
     * The error code set when the error happened, e.g. database error code.
16
     *
17
     * @var string|int|null
18
     */
19
    private $code;
20
21
    /**
22
     * The original error message.
23
     *
24
     * @var string|null
25
     */
26
    private $message;
27
28
    /**
29
     * Describes the exception type's module namespace.
30
     *
31
     * @var string|null
32
     */
33
    private $module;
34
35
    /**
36
     * @var object|null
37
     */
38
    private $attributes;
39
40
    /**
41
     * @var array|null
42
     */
43
    private $stacktrace;
44
45
    /**
46
     * @var string|null
47
     */
48
    private $type;
49
50
    /**
51
     * @var bool|null
52
     */
53
    private $handled;
54
55
    /**
56
     * @var array|null
57
     */
58
    private $cause;
59
60
    /**
61
     * @param string|int|null $code
62
     * @param string|null $message
63
     * @param string|null $module
64
     * @param object|null $attributes
65
     * @param array|null $stacktrace
66
     * @param string|null $type
67
     * @param bool|null $handled
68
     * @param array|null $cause
69
     */
70 7
    public function __construct(
71
        $code = null,
72
        $message = null,
73
        $module = null,
74
        $attributes = null,
75
        array $stacktrace = null,
76
        $type = null,
77
        $handled = null,
78
        array $cause = null
79
    ) {
80 7
        if (null === $message && null === $type) {
81 1
            throw new \InvalidArgumentException('At least one of the fields (message, type) must be a string.');
82
        }
83
84 6
        $this->assertInstanceOfElements(StacktraceFrame::CLASS_NAME, $stacktrace);
85
86 5
        $this->code = $code;
87 5
        $this->message = $message;
88 5
        $this->module = $module;
89 5
        $this->attributes = $attributes;
90 5
        $this->stacktrace = $stacktrace;
91 5
        $this->type = $type;
92 5
        $this->handled = $handled;
93 5
        $this->cause = $cause;
94 5
    }
95
96
    /**
97
     * @param string $class
98
     * @param array|null $elements
99
     *
100
     * @return void
101
     *
102
     * @throws \InvalidArgumentException
103
     */
104 6
    private function assertInstanceOfElements($class, array $elements = null)
105
    {
106 6
        if (null === $elements) {
107 1
            return;
108
        }
109
110 5
        foreach ($elements as $item) {
111 5
            if (false === $item instanceof $class) {
112 1
                throw new \InvalidArgumentException(
113 1
                    sprintf(
114 1
                        'All elements must be instances of %s',
115
                        $class
116 1
                    )
117 1
                );
118
            }
119 4
        }
120 4
    }
121
122
    /**
123
     * @param \Exception $exception
124
     *
125
     * @return Exception
126
     */
127 4
    public static function fromException($exception)
128
    {
129 4
        Assert::throwable($exception);
130
131 4
        return new self(
132 4
            $exception->getCode(),
133 4
            $exception->getMessage(),
134 4
            null,
135 4
            null,
136 4
            self::mapStacktrace($exception),
137 4
            get_class($exception),
138 4
            null,
139
            null
140 4
        );
141
    }
142
143
    /**
144
     * @param \Exception $exception
145
     *
146
     * @return array
147
     */
148 4
    private static function mapStacktrace($exception)
149
    {
150 4
        $stacktrace = [];
151
152 4
        foreach ($exception->getTrace() as $trace) {
153 4
            $stacktrace[] = StacktraceFrame::fromDebugBacktrace($trace);
154 4
        }
155
156 4
        return $stacktrace;
157
    }
158
159
    /**
160
     * @return array
161
     */
162 1
    public function jsonSerialize()
163
    {
164
        return [
165 1
            'code' => Encoding::keywordField($this->code),
166 1
            'message' => $this->message,
167 1
            'module' => Encoding::keywordField($this->module),
168 1
            'attributes' => $this->attributes,
169 1
            'stacktrace' => $this->stacktrace,
170 1
            'type' => Encoding::keywordField($this->type),
171 1
            'handled' => $this->handled,
172 1
            'cause' => $this->cause,
173 1
        ];
174
    }
175
}
176