Passed
Push — master ( b0cf56...bbf452 )
by Eugene
09:13
created

Error::getLine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
ccs 0
cts 2
cp 0
crap 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 83
    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 83
        $this->type = $type;
38 83
        $this->file = $file;
39 83
        $this->line = $line;
40 83
        $this->message = $message;
41 83
        $this->number = $number;
42 83
        $this->code = $code;
43 83
        $this->fields = $fields;
44 83
        $this->previous = $previous;
45
    }
46
47 83
    public static function fromMap(array $errorMap) : self
48
    {
49 83
        if (empty($errorMap[Keys::ERROR_STACK])) {
50
            throw new \InvalidArgumentException('The error map should contain a non-empty error stack');
51
        }
52
53 83
        $errorStack = $errorMap[Keys::ERROR_STACK];
54
55 83
        $first = \count($errorStack) - 1;
56 83
        $error = new self(
57 83
            $errorStack[$first][Keys::ERROR_TYPE],
58 83
            $errorStack[$first][Keys::ERROR_FILE],
59 83
            $errorStack[$first][Keys::ERROR_LINE],
60 83
            $errorStack[$first][Keys::ERROR_MESSAGE],
61 83
            $errorStack[$first][Keys::ERROR_NUMBER],
62 83
            $errorStack[$first][Keys::ERROR_CODE],
63 83
            $errorStack[$first][Keys::ERROR_FIELDS] ?? []
64
        );
65
66 83
        if (0 === $first) {
67 75
            return $error;
68
        }
69
70 8
        for ($i = $first - 1; $i >= 0; --$i) {
71 8
            $error = new self(
72 8
                $errorStack[$i][Keys::ERROR_TYPE],
73 8
                $errorStack[$i][Keys::ERROR_FILE],
74 8
                $errorStack[$i][Keys::ERROR_LINE],
75 8
                $errorStack[$i][Keys::ERROR_MESSAGE],
76 8
                $errorStack[$i][Keys::ERROR_NUMBER],
77 8
                $errorStack[$i][Keys::ERROR_CODE],
78 8
                $errorStack[$i][Keys::ERROR_FIELDS] ?? [],
79
                $error
80
            );
81
        }
82
83 8
        return $error;
84
    }
85
86 2
    public function toMap() : array
87
    {
88 2
        $error = $this;
89 2
        $errorStack = [];
90
91
        do {
92 2
            $errorStack[] = [
93 2
                Keys::ERROR_TYPE => $error->type,
94 2
                Keys::ERROR_FILE => $error->file,
95 2
                Keys::ERROR_LINE => $error->line,
96 2
                Keys::ERROR_MESSAGE => $error->message,
97 2
                Keys::ERROR_NUMBER => $error->number,
98 2
                Keys::ERROR_CODE => $error->code,
99 2
                Keys::ERROR_FIELDS => $error->fields,
100
            ];
101 2
        } while ($error = $error->getPrevious());
102
103 2
        return [Keys::ERROR_STACK => $errorStack];
104
    }
105
106 8
    public function getType() : string
107
    {
108 8
        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 17
    public function getMessage() : string
122
    {
123 17
        return $this->message;
124
    }
125
126
    public function getNumber() : int
127
    {
128
        return $this->number;
129
    }
130
131 17
    public function getCode() : int
132
    {
133 17
        return $this->code;
134
    }
135
136 8
    public function getFields() : array
137
    {
138 8
        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 12
    public function tryGetField(string $name, $default = null)
158
    {
159 12
        return \array_key_exists($name, $this->fields) ? $this->fields[$name] : $default;
160
    }
161
162 11
    public function getPrevious() : ?self
163
    {
164 11
        return $this->previous;
165
    }
166
}
167