Error   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Test Coverage

Coverage 84.93%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 67
c 1
b 0
f 0
dl 0
loc 149
ccs 62
cts 73
cp 0.8493
rs 10
wmc 19

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getLine() 0 3 1
A getType() 0 3 1
A getFile() 0 3 1
A getCode() 0 3 1
A tryGetField() 0 3 2
A getPrevious() 0 3 1
A getNumber() 0 3 1
A __construct() 0 18 1
A getFields() 0 3 1
A getMessage() 0 3 1
A getField() 0 7 2
A fromMap() 0 37 4
A toMap() 0 18 2
1
<?php
2
3
/**
4
 * This file is part of the tarantool/client package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Tarantool\Client;
15
16
final class Error
17
{
18
    private $type;
19
    private $file;
20
    private $line;
21
    private $message;
22
    private $number;
23
    private $code;
24
    private $fields;
25
    private $previous;
26
27 113
    public function __construct(
28
        string $type,
29
        string $file,
30
        int $line,
31
        string $message,
32
        int $number,
33
        int $code,
34
        array $fields = [],
35
        ?self $previous = null
36
    ) {
37 113
        $this->type = $type;
38 113
        $this->file = $file;
39 113
        $this->line = $line;
40 113
        $this->message = $message;
41 113
        $this->number = $number;
42 113
        $this->code = $code;
43 113
        $this->fields = $fields;
44 113
        $this->previous = $previous;
45
    }
46
47 113
    public static function fromMap(array $errorMap) : self
48
    {
49 113
        if (empty($errorMap[Keys::ERROR_STACK])) {
50
            throw new \InvalidArgumentException('The error map should contain a non-empty error stack');
51
        }
52
53 113
        $errorStack = $errorMap[Keys::ERROR_STACK];
54
55 113
        $first = \count($errorStack) - 1;
56 113
        $error = new self(
57 113
            $errorStack[$first][Keys::ERROR_TYPE],
58 113
            $errorStack[$first][Keys::ERROR_FILE],
59 113
            $errorStack[$first][Keys::ERROR_LINE],
60 113
            $errorStack[$first][Keys::ERROR_MESSAGE],
61 113
            $errorStack[$first][Keys::ERROR_NUMBER],
62 113
            $errorStack[$first][Keys::ERROR_CODE],
63 113
            $errorStack[$first][Keys::ERROR_FIELDS] ?? []
64 113
        );
65
66 113
        if (0 === $first) {
67 104
            return $error;
68
        }
69
70 9
        for ($i = $first - 1; $i >= 0; --$i) {
71 9
            $error = new self(
72 9
                $errorStack[$i][Keys::ERROR_TYPE],
73 9
                $errorStack[$i][Keys::ERROR_FILE],
74 9
                $errorStack[$i][Keys::ERROR_LINE],
75 9
                $errorStack[$i][Keys::ERROR_MESSAGE],
76 9
                $errorStack[$i][Keys::ERROR_NUMBER],
77 9
                $errorStack[$i][Keys::ERROR_CODE],
78 9
                $errorStack[$i][Keys::ERROR_FIELDS] ?? [],
79 9
                $error
80 9
            );
81
        }
82
83 9
        return $error;
84
    }
85
86 1
    public function toMap() : array
87
    {
88 1
        $error = $this;
89 1
        $errorStack = [];
90
91
        do {
92 1
            $errorStack[] = [
93 1
                Keys::ERROR_TYPE => $error->type,
94 1
                Keys::ERROR_FILE => $error->file,
95 1
                Keys::ERROR_LINE => $error->line,
96 1
                Keys::ERROR_MESSAGE => $error->message,
97 1
                Keys::ERROR_NUMBER => $error->number,
98 1
                Keys::ERROR_CODE => $error->code,
99 1
                Keys::ERROR_FIELDS => $error->fields,
100 1
            ];
101 1
        } while ($error = $error->getPrevious());
102
103 1
        return [Keys::ERROR_STACK => $errorStack];
104
    }
105
106 9
    public function getType() : string
107
    {
108 9
        return $this->type;
109
    }
110
111
    public function getFile() : string
112
    {
113
        return $this->file;
114
    }
115
116
    public function getLine() : int
117
    {
118
        return $this->line;
119
    }
120
121 21
    public function getMessage() : string
122
    {
123 21
        return $this->message;
124
    }
125
126
    public function getNumber() : int
127
    {
128
        return $this->number;
129
    }
130
131 21
    public function getCode() : int
132
    {
133 21
        return $this->code;
134
    }
135
136 9
    public function getFields() : array
137
    {
138 9
        return $this->fields;
139
    }
140
141
    /**
142
     * @return mixed
143
     */
144
    public function getField(string $name)
145
    {
146
        if (\array_key_exists($name, $this->fields)) {
147
            return $this->fields[$name];
148
        }
149
150
        throw new \OutOfRangeException(\sprintf('The field "%s" does not exist', $name));
151
    }
152
153
    /**
154
     * @param mixed $default
155
     * @return mixed
156
     */
157 16
    public function tryGetField(string $name, $default = null)
158
    {
159 16
        return \array_key_exists($name, $this->fields) ? $this->fields[$name] : $default;
160
    }
161
162 13
    public function getPrevious() : ?self
163
    {
164 13
        return $this->previous;
165
    }
166
}
167