1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Whoops - php errors for cool kids |
4
|
|
|
* @author Filipe Dobreira <http://github.com/filp> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Whoops\Exception; |
8
|
|
|
|
9
|
|
|
class Inspector |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var \Exception | \Throwable |
13
|
|
|
*/ |
14
|
|
|
private $exception; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var \Whoops\Exception\FrameCollection |
18
|
|
|
*/ |
19
|
|
|
private $frames; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \Whoops\Exception\Inspector |
23
|
|
|
*/ |
24
|
|
|
private $previousExceptionInspector; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param \Exception | \Throwable $exception The exception to inspect |
28
|
|
|
*/ |
29
|
4 |
|
public function __construct($exception) |
30
|
|
|
{ |
31
|
4 |
|
$this->exception = $exception; |
32
|
4 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return \Exception | \Throwable |
36
|
|
|
*/ |
37
|
3 |
|
public function getException() |
38
|
|
|
{ |
39
|
3 |
|
return $this->exception; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
2 |
|
public function getExceptionName() |
46
|
|
|
{ |
47
|
2 |
|
return get_class($this->exception); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function getExceptionMessage() |
54
|
|
|
{ |
55
|
|
|
return $this->exception->getMessage(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Does the wrapped Exception have a previous Exception? |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
2 |
|
public function hasPreviousException() |
63
|
|
|
{ |
64
|
2 |
|
return $this->previousExceptionInspector || $this->exception->getPrevious(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns an Inspector for a previous Exception, if any. |
69
|
|
|
* @todo Clean this up a bit, cache stuff a bit better. |
70
|
|
|
* @return Inspector |
71
|
|
|
*/ |
72
|
2 |
|
public function getPreviousExceptionInspector() |
73
|
|
|
{ |
74
|
2 |
|
if ($this->previousExceptionInspector === null) { |
75
|
2 |
|
$previousException = $this->exception->getPrevious(); |
76
|
|
|
|
77
|
2 |
|
if ($previousException) { |
78
|
1 |
|
$this->previousExceptionInspector = new Inspector($previousException); |
79
|
1 |
|
} |
80
|
2 |
|
} |
81
|
|
|
|
82
|
2 |
|
return $this->previousExceptionInspector; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns an iterator for the inspected exception's |
87
|
|
|
* frames. |
88
|
|
|
* @return \Whoops\Exception\FrameCollection |
89
|
|
|
*/ |
90
|
3 |
|
public function getFrames() |
91
|
|
|
{ |
92
|
3 |
|
if ($this->frames === null) { |
93
|
3 |
|
$frames = $this->exception->getTrace(); |
94
|
|
|
|
95
|
|
|
// If we're handling an ErrorException thrown by Whoops, |
96
|
|
|
// get rid of the last frame, which matches the handleError method, |
97
|
|
|
// and do not add the current exception to trace. We ensure that |
98
|
|
|
// the next frame does have a filename / linenumber, though. |
99
|
3 |
|
if ($this->exception instanceof ErrorException && empty($frames[1]['line'])) { |
100
|
|
|
$frames = array($this->getFrameFromError($this->exception)); |
101
|
|
|
} else { |
102
|
3 |
|
$firstFrame = $this->getFrameFromException($this->exception); |
103
|
3 |
|
array_unshift($frames, $firstFrame); |
104
|
|
|
} |
105
|
3 |
|
$this->frames = new FrameCollection($frames); |
106
|
|
|
|
107
|
3 |
|
if ($previousInspector = $this->getPreviousExceptionInspector()) { |
108
|
|
|
// Keep outer frame on top of the inner one |
109
|
1 |
|
$outerFrames = $this->frames; |
110
|
1 |
|
$newFrames = clone $previousInspector->getFrames(); |
111
|
|
|
// I assume it will always be set, but let's be safe |
112
|
1 |
|
if (isset($newFrames[0])) { |
113
|
1 |
|
$newFrames[0]->addComment( |
114
|
1 |
|
$previousInspector->getExceptionMessage(), |
115
|
|
|
'Exception message:' |
116
|
1 |
|
); |
117
|
1 |
|
} |
118
|
1 |
|
$newFrames->prependFrames($outerFrames->topDiff($newFrames)); |
|
|
|
|
119
|
1 |
|
$this->frames = $newFrames; |
120
|
1 |
|
} |
121
|
3 |
|
} |
122
|
|
|
|
123
|
3 |
|
return $this->frames; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Given an exception, generates an array in the format |
128
|
|
|
* generated by Throwable::getTrace() |
129
|
|
|
* @param \Exception | \Throwable $exception |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
1 |
|
protected function getFrameFromException($exception) |
133
|
|
|
{ |
134
|
|
|
return array( |
135
|
1 |
|
'file' => $exception->getFile(), |
136
|
1 |
|
'line' => $exception->getLine(), |
137
|
1 |
|
'class' => get_class($exception), |
138
|
|
|
'args' => array( |
139
|
1 |
|
$exception->getMessage(), |
140
|
1 |
|
), |
141
|
1 |
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Given an error, generates an array in the format |
146
|
|
|
* generated by ErrorException |
147
|
|
|
* @param ErrorException $exception |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
|
|
protected function getFrameFromError(ErrorException $exception) |
151
|
|
|
{ |
152
|
|
|
return array( |
153
|
|
|
'file' => $exception->getFile(), |
154
|
|
|
'line' => $exception->getLine(), |
155
|
|
|
'class' => null, |
156
|
|
|
'args' => array(), |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: