Completed
Push — master ( 246dd7...ca0f65 )
by Kamil
03:43
created

Error::setContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 4
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Dazzle\Throwable;
4
5 View Code Duplication
class Error extends \Error
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
6
{
7
    /**
8
     * @var mixed[]
9
     */
10
    protected $context = [];
11
12
    /**
13
     * @param string $message
14
     * @param int $code
15
     * @param \Error|\Exception|null $previous
16
     */
17 31
    public function __construct($message = 'Unknown error', $code = 0, $previous = null)
18
    {
19 31
        parent::__construct($message, $code, $previous);
20 31
    }
21
22
    /**
23
     * @param mixed[] $context
24
     */
25
    public function setContext(array $context = [])
26
    {
27
        $this->context = $context;
28
    }
29
30
    /**
31
     * @return mixed[]
32
     */
33
    public function getContext()
34
    {
35
        return $this->context;
36
    }
37
38
    /**
39
     * @return string
40
     */
41 2
    public function __toString()
42
    {
43 2
        return static::toString($this);
44
    }
45
46
    /**
47
     * Return Error full trace in string format.
48
     *
49
     * @param \Error|\Exception $ex
50
     * @return string
51
     */
52 2
    public static function toString($ex)
53
    {
54 2
        return implode("\n", [
55 2
            "\t" . 'Throwable trace:',
56 2
            static::toThrowableString($ex),
57
            "\t" . 'Stack trace:',
58 2
            static::toStackString($ex)
59
        ]);
60
    }
61
62
    /**
63
     * Return Error full trace in array format.
64
     *
65
     * @param \Error|\Exception $ex
66
     * @return mixed
67
     */
68 1
    public static function toTrace($ex)
69
    {
70 1
        return Throwable::getThrowableStack($ex);
71
    }
72
73
    /**
74
     * Return Error stack trace in array format.
75
     *
76
     * @param \Error|\Exception $ex
77
     * @return string[]
78
     */
79 4
    public static function toStackTrace($ex)
80
    {
81 4
        $list = [];
82 4
        for ($stack = Throwable::getThrowableStack($ex); $stack !== null; $stack = $stack['prev'])
83
        {
84 4
            $list = array_merge($stack['trace'], $list);
85
        }
86
87 4
        return $list;
88
    }
89
90
    /**
91
     * Return Error throwable trace in array format.
92
     *
93
     * @param \Error|\Exception $ex
94
     * @return string[]
95
     */
96 4
    public static function toThrowableTrace($ex)
97
    {
98 4
        $list = [];
99 4
        for ($stack = Throwable::getThrowableStack($ex); $stack !== null; $stack = $stack['prev'])
100
        {
101 4
            $list[] = Throwable::parseThrowableMessage($stack);
102
        }
103
104 4
        return array_reverse($list);
105
    }
106
107
    /**
108
     * Return Error stack trace in string format.
109
     *
110
     * @param \Error|\Exception $ex
111
     * @return string
112
     */
113 3
    public static function toStackString($ex)
114
    {
115 3
        $stack = [];
116 3
        $i = 0;
117 3
        $trace = static::toStackTrace($ex);
118 3
        $pad = strlen(count($trace)) > 2 ?: 2;
119
120 3
        foreach ($trace as $element)
121
        {
122 3
            $stack[] = "\t" . str_pad('' . $i, $pad, ' ', STR_PAD_LEFT) . '. ' . $element;
123 3
            ++$i;
124
        }
125
126 3
        return implode("\n", $stack);
127
    }
128
129
    /**
130
     * Return Error throwable trace in string format.
131
     *
132
     * @param \Error|\Exception $ex
133
     * @return string
134
     */
135 3
    public static function toThrowableString($ex)
136
    {
137 3
        $stack = [];
138 3
        $i = 0;
139 3
        $trace = static::toThrowableTrace($ex);
140 3
        $pad = strlen(count($trace)) > 2 ?: 2;
141
142 3
        foreach ($trace as $element)
143
        {
144 3
            $stack[] = "\t" . str_pad('' . $i, $pad, ' ', STR_PAD_LEFT) . '. ' . $element;
145 3
            ++$i;
146
        }
147
148 3
        return implode("\n", $stack);
149
    }
150
}
151