1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2015-present Ganbaro Digital Ltd |
5
|
|
|
* All rights reserved. |
6
|
|
|
* |
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
8
|
|
|
* modification, are permitted provided that the following conditions |
9
|
|
|
* are met: |
10
|
|
|
* |
11
|
|
|
* * Redistributions of source code must retain the above copyright |
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
13
|
|
|
* |
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
16
|
|
|
* the documentation and/or other materials provided with the |
17
|
|
|
* distribution. |
18
|
|
|
* |
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
20
|
|
|
* contributors may be used to endorse or promote products derived |
21
|
|
|
* from this software without specific prior written permission. |
22
|
|
|
* |
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
35
|
|
|
* |
36
|
|
|
* @category Libraries |
37
|
|
|
* @package MissingBits/Traces |
38
|
|
|
* @author Stuart Herbert <[email protected]> |
39
|
|
|
* @copyright 2015-present Ganbaro Digital Ltd www.ganbarodigital.com |
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
41
|
|
|
* @link http://ganbarodigital.github.io/php-the-missing-bits |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
namespace GanbaroDigital\MissingBits\TraceInspectors; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* StackFrame holds details about a single entry in a PHP stack trace |
48
|
|
|
*/ |
49
|
|
|
class StackFrame |
50
|
|
|
{ |
51
|
|
|
/** |
52
|
|
|
* which class has been executed? |
53
|
|
|
* |
54
|
|
|
* @var string|null |
55
|
|
|
*/ |
56
|
|
|
private $class; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* which function or method has been executed? |
60
|
|
|
* |
61
|
|
|
* @var string|null |
62
|
|
|
*/ |
63
|
|
|
private $function; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* how was the function or method called? |
67
|
|
|
* |
68
|
|
|
* @var string|null |
69
|
|
|
*/ |
70
|
|
|
private $callType; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* which file was the code in? |
74
|
|
|
* |
75
|
|
|
* @var string|null |
76
|
|
|
*/ |
77
|
|
|
private $file; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* which line in $file was the code on? |
81
|
|
|
* |
82
|
|
|
* @var int|null |
83
|
|
|
*/ |
84
|
|
|
private $line; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* what were the contents of the call stack at the time? |
88
|
|
|
* |
89
|
|
|
* @var array |
90
|
|
|
*/ |
91
|
|
|
private $stack; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* constructor |
95
|
|
|
* |
96
|
|
|
* @param string|null $class |
97
|
|
|
* which class has been executed? |
98
|
|
|
* @param string|null $function |
99
|
|
|
* which function or method has been executed? |
100
|
|
|
* @param string|null $callType |
101
|
|
|
* how was $function called? |
102
|
|
|
* @param string|null $file |
103
|
|
|
* which file was the calling code in? |
104
|
|
|
* @param int|null $line |
105
|
|
|
* which line in $file was the calling code on? |
106
|
|
|
* @param array $stack |
107
|
|
|
* what was the call stack at the time? |
108
|
|
|
*/ |
109
|
|
|
public function __construct($class, $function, $callType, $file, $line, $stack = []) |
110
|
|
|
{ |
111
|
|
|
$this->class = $class; |
112
|
|
|
$this->function = $function; |
113
|
|
|
$this->callType = $callType; |
114
|
|
|
$this->file = $file; |
115
|
|
|
$this->line = $line; |
116
|
|
|
$this->stack = $stack; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* which class has been executed? |
121
|
|
|
* |
122
|
|
|
* @return string|null |
123
|
|
|
*/ |
124
|
|
|
public function getClass() |
125
|
|
|
{ |
126
|
|
|
return $this->class; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* which function or method has been executed? |
131
|
|
|
* |
132
|
|
|
* @return string|null |
133
|
|
|
*/ |
134
|
|
|
public function getFunction() |
135
|
|
|
{ |
136
|
|
|
return $this->function; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* which method has been executed? |
141
|
|
|
* |
142
|
|
|
* @return string|null |
143
|
|
|
*/ |
144
|
|
|
public function getMethod() |
145
|
|
|
{ |
146
|
|
|
if ($this->class === null) { |
147
|
|
|
return null; |
148
|
|
|
} |
149
|
|
|
return $this->function; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* how was the function|method called? |
154
|
|
|
* |
155
|
|
|
* @return string|null |
156
|
|
|
*/ |
157
|
|
|
public function getCallType() |
158
|
|
|
{ |
159
|
|
|
return $this->callType; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* which file was the executed code defined in? |
164
|
|
|
* |
165
|
|
|
* @return string|null |
166
|
|
|
*/ |
167
|
|
|
public function getFilename() |
168
|
|
|
{ |
169
|
|
|
return $this->file; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* which line in $this->getFile() was the executed code defined on? |
174
|
|
|
* |
175
|
|
|
* @return int|null |
176
|
|
|
*/ |
177
|
|
|
public function getLine() |
178
|
|
|
{ |
179
|
|
|
return $this->line; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* what were the contents of the call stack at the time? |
184
|
|
|
* |
185
|
|
|
* an empty array means that we weren't asked to save the call stack |
186
|
|
|
* (probably to save memory - the call stack can be large) |
187
|
|
|
* |
188
|
|
|
* @return array |
189
|
|
|
*/ |
190
|
|
|
public function getStack() |
191
|
|
|
{ |
192
|
|
|
return $this->stack; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
// ========================================================================= |
196
|
|
|
// |
197
|
|
|
// HELPERS |
198
|
|
|
// |
199
|
|
|
// ------------------------------------------------------------------------- |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* return our contents as a sensible, printable string |
203
|
|
|
* |
204
|
|
|
* @return string |
|
|
|
|
205
|
|
|
*/ |
206
|
|
|
public function getExecutedCodeSummary() |
207
|
|
|
{ |
208
|
|
|
// a class method, a global function, or a PHP script has been executed |
209
|
|
|
if (isset($this->class)) { |
210
|
|
|
$retval = $this->class . $this->callType . $this->function . '()'; |
211
|
|
|
} |
212
|
|
|
else if (isset($this->function)) { |
213
|
|
|
$retval = $this->function . '()'; |
214
|
|
|
} |
215
|
|
|
else { |
216
|
|
|
$retval = $this->file; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
// which line was the caller on? |
220
|
|
|
if (isset($this->line)) { |
221
|
|
|
$retval .= "@" . $this->line; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
// all done |
225
|
|
|
return $retval; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* return our contents as a sensible, printable string |
230
|
|
|
* |
231
|
|
|
* @return string |
|
|
|
|
232
|
|
|
*/ |
233
|
|
|
public function __toString() |
234
|
|
|
{ |
235
|
|
|
return $this->getExecutedCodeSummary(); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.