comparison vendor/symfony/var-dumper/Caster/ExceptionCaster.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children
comparison
equal deleted inserted replaced
3:307d7a7fd348 4:a9cd425dd02b
10 */ 10 */
11 11
12 namespace Symfony\Component\VarDumper\Caster; 12 namespace Symfony\Component\VarDumper\Caster;
13 13
14 use Symfony\Component\Debug\Exception\SilencedErrorContext; 14 use Symfony\Component\Debug\Exception\SilencedErrorContext;
15 use Symfony\Component\VarDumper\Cloner\Stub;
15 use Symfony\Component\VarDumper\Exception\ThrowingCasterException; 16 use Symfony\Component\VarDumper\Exception\ThrowingCasterException;
16 use Symfony\Component\VarDumper\Cloner\Stub;
17 17
18 /** 18 /**
19 * Casts common Exception classes to array representation. 19 * Casts common Exception classes to array representation.
20 * 20 *
21 * @author Nicolas Grekas <p@tchwork.com> 21 * @author Nicolas Grekas <p@tchwork.com>
22 */ 22 */
23 class ExceptionCaster 23 class ExceptionCaster
24 { 24 {
25 public static $srcContext = 1; 25 public static $srcContext = 1;
26 public static $traceArgs = true; 26 public static $traceArgs = true;
27 public static $errorTypes = array( 27 public static $errorTypes = [
28 E_DEPRECATED => 'E_DEPRECATED', 28 E_DEPRECATED => 'E_DEPRECATED',
29 E_USER_DEPRECATED => 'E_USER_DEPRECATED', 29 E_USER_DEPRECATED => 'E_USER_DEPRECATED',
30 E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR', 30 E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
31 E_ERROR => 'E_ERROR', 31 E_ERROR => 'E_ERROR',
32 E_WARNING => 'E_WARNING', 32 E_WARNING => 'E_WARNING',
38 E_COMPILE_WARNING => 'E_COMPILE_WARNING', 38 E_COMPILE_WARNING => 'E_COMPILE_WARNING',
39 E_USER_ERROR => 'E_USER_ERROR', 39 E_USER_ERROR => 'E_USER_ERROR',
40 E_USER_WARNING => 'E_USER_WARNING', 40 E_USER_WARNING => 'E_USER_WARNING',
41 E_USER_NOTICE => 'E_USER_NOTICE', 41 E_USER_NOTICE => 'E_USER_NOTICE',
42 E_STRICT => 'E_STRICT', 42 E_STRICT => 'E_STRICT',
43 ); 43 ];
44 44
45 private static $framesCache = array(); 45 private static $framesCache = [];
46 46
47 public static function castError(\Error $e, array $a, Stub $stub, $isNested, $filter = 0) 47 public static function castError(\Error $e, array $a, Stub $stub, $isNested, $filter = 0)
48 { 48 {
49 return self::filterExceptionArray($stub->class, $a, "\0Error\0", $filter); 49 return self::filterExceptionArray($stub->class, $a, "\0Error\0", $filter);
50 } 50 }
69 $prefix = Caster::PREFIX_PROTECTED; 69 $prefix = Caster::PREFIX_PROTECTED;
70 $xPrefix = "\0Exception\0"; 70 $xPrefix = "\0Exception\0";
71 71
72 if (isset($a[$xPrefix.'previous'], $a[$trace]) && $a[$xPrefix.'previous'] instanceof \Exception) { 72 if (isset($a[$xPrefix.'previous'], $a[$trace]) && $a[$xPrefix.'previous'] instanceof \Exception) {
73 $b = (array) $a[$xPrefix.'previous']; 73 $b = (array) $a[$xPrefix.'previous'];
74 self::traceUnshift($b[$xPrefix.'trace'], get_class($a[$xPrefix.'previous']), $b[$prefix.'file'], $b[$prefix.'line']); 74 self::traceUnshift($b[$xPrefix.'trace'], \get_class($a[$xPrefix.'previous']), $b[$prefix.'file'], $b[$prefix.'line']);
75 $a[$trace] = new TraceStub($b[$xPrefix.'trace'], false, 0, -count($a[$trace]->value)); 75 $a[$trace] = new TraceStub($b[$xPrefix.'trace'], false, 0, -\count($a[$trace]->value));
76 } 76 }
77 77
78 unset($a[$xPrefix.'previous'], $a[$prefix.'code'], $a[$prefix.'file'], $a[$prefix.'line']); 78 unset($a[$xPrefix.'previous'], $a[$prefix.'code'], $a[$prefix.'file'], $a[$prefix.'line']);
79 79
80 return $a; 80 return $a;
90 90
91 if (isset(self::$errorTypes[$a[$s]])) { 91 if (isset(self::$errorTypes[$a[$s]])) {
92 $a[$s] = new ConstStub(self::$errorTypes[$a[$s]], $a[$s]); 92 $a[$s] = new ConstStub(self::$errorTypes[$a[$s]], $a[$s]);
93 } 93 }
94 94
95 $trace = array(array( 95 $trace = [[
96 'file' => $a[$sPrefix.'file'], 96 'file' => $a[$sPrefix.'file'],
97 'line' => $a[$sPrefix.'line'], 97 'line' => $a[$sPrefix.'line'],
98 )); 98 ]];
99 99
100 if (isset($a[$sPrefix.'trace'])) { 100 if (isset($a[$sPrefix.'trace'])) {
101 $trace = array_merge($trace, $a[$sPrefix.'trace']); 101 $trace = array_merge($trace, $a[$sPrefix.'trace']);
102 } 102 }
103 103
115 $stub->class = ''; 115 $stub->class = '';
116 $stub->handle = 0; 116 $stub->handle = 0;
117 $frames = $trace->value; 117 $frames = $trace->value;
118 $prefix = Caster::PREFIX_VIRTUAL; 118 $prefix = Caster::PREFIX_VIRTUAL;
119 119
120 $a = array(); 120 $a = [];
121 $j = count($frames); 121 $j = \count($frames);
122 if (0 > $i = $trace->sliceOffset) { 122 if (0 > $i = $trace->sliceOffset) {
123 $i = max(0, $j + $i); 123 $i = max(0, $j + $i);
124 } 124 }
125 if (!isset($trace->value[$i])) { 125 if (!isset($trace->value[$i])) {
126 return array(); 126 return [];
127 } 127 }
128 $lastCall = isset($frames[$i]['function']) ? (isset($frames[$i]['class']) ? $frames[0]['class'].$frames[$i]['type'] : '').$frames[$i]['function'].'()' : ''; 128 $lastCall = isset($frames[$i]['function']) ? (isset($frames[$i]['class']) ? $frames[0]['class'].$frames[$i]['type'] : '').$frames[$i]['function'].'()' : '';
129 $frames[] = array('function' => ''); 129 $frames[] = ['function' => ''];
130 $collapse = false; 130 $collapse = false;
131 131
132 for ($j += $trace->numberingOffset - $i++; isset($frames[$i]); ++$i, --$j) { 132 for ($j += $trace->numberingOffset - $i++; isset($frames[$i]); ++$i, --$j) {
133 $f = $frames[$i]; 133 $f = $frames[$i];
134 $call = isset($f['function']) ? (isset($f['class']) ? $f['class'].$f['type'] : '').$f['function'] : '???'; 134 $call = isset($f['function']) ? (isset($f['class']) ? $f['class'].$f['type'] : '').$f['function'] : '???';
135 135
136 $frame = new FrameStub( 136 $frame = new FrameStub(
137 array( 137 [
138 'object' => isset($f['object']) ? $f['object'] : null, 138 'object' => isset($f['object']) ? $f['object'] : null,
139 'class' => isset($f['class']) ? $f['class'] : null, 139 'class' => isset($f['class']) ? $f['class'] : null,
140 'type' => isset($f['type']) ? $f['type'] : null, 140 'type' => isset($f['type']) ? $f['type'] : null,
141 'function' => isset($f['function']) ? $f['function'] : null, 141 'function' => isset($f['function']) ? $f['function'] : null,
142 ) + $frames[$i - 1], 142 ] + $frames[$i - 1],
143 false, 143 false,
144 true 144 true
145 ); 145 );
146 $f = self::castFrameStub($frame, array(), $frame, true); 146 $f = self::castFrameStub($frame, [], $frame, true);
147 if (isset($f[$prefix.'src'])) { 147 if (isset($f[$prefix.'src'])) {
148 foreach ($f[$prefix.'src']->value as $label => $frame) { 148 foreach ($f[$prefix.'src']->value as $label => $frame) {
149 if (0 === strpos($label, "\0~collapse=0")) { 149 if (0 === strpos($label, "\0~collapse=0")) {
150 if ($collapse) { 150 if ($collapse) {
151 $label = substr_replace($label, '1', 11, 1); 151 $label = substr_replace($label, '1', 11, 1);
173 $a[substr_replace($label, sprintf('separator=%s&', $frame instanceof EnumStub ? ' ' : ':'), 2, 0)] = $frame; 173 $a[substr_replace($label, sprintf('separator=%s&', $frame instanceof EnumStub ? ' ' : ':'), 2, 0)] = $frame;
174 174
175 $lastCall = $call; 175 $lastCall = $call;
176 } 176 }
177 if (null !== $trace->sliceLength) { 177 if (null !== $trace->sliceLength) {
178 $a = array_slice($a, 0, $trace->sliceLength, true); 178 $a = \array_slice($a, 0, $trace->sliceLength, true);
179 } 179 }
180 180
181 return $a; 181 return $a;
182 } 182 }
183 183
197 197
198 if (isset(self::$framesCache[$cacheKey])) { 198 if (isset(self::$framesCache[$cacheKey])) {
199 $a[$prefix.'src'] = self::$framesCache[$cacheKey]; 199 $a[$prefix.'src'] = self::$framesCache[$cacheKey];
200 } else { 200 } else {
201 if (preg_match('/\((\d+)\)(?:\([\da-f]{32}\))? : (?:eval\(\)\'d code|runtime-created function)$/', $f['file'], $match)) { 201 if (preg_match('/\((\d+)\)(?:\([\da-f]{32}\))? : (?:eval\(\)\'d code|runtime-created function)$/', $f['file'], $match)) {
202 $f['file'] = substr($f['file'], 0, -strlen($match[0])); 202 $f['file'] = substr($f['file'], 0, -\strlen($match[0]));
203 $f['line'] = (int) $match[1]; 203 $f['line'] = (int) $match[1];
204 } 204 }
205 $caller = isset($f['function']) ? sprintf('in %s() on line %d', (isset($f['class']) ? $f['class'].$f['type'] : '').$f['function'], $f['line']) : null; 205 $caller = isset($f['function']) ? sprintf('in %s() on line %d', (isset($f['class']) ? $f['class'].$f['type'] : '').$f['function'], $f['line']) : null;
206 $src = $f['line']; 206 $src = $f['line'];
207 $srcKey = $f['file']; 207 $srcKey = $f['file'];
210 $ellipsisTail = isset($ellipsis->attr['ellipsis-tail']) ? $ellipsis->attr['ellipsis-tail'] : 0; 210 $ellipsisTail = isset($ellipsis->attr['ellipsis-tail']) ? $ellipsis->attr['ellipsis-tail'] : 0;
211 $ellipsis = isset($ellipsis->attr['ellipsis']) ? $ellipsis->attr['ellipsis'] : 0; 211 $ellipsis = isset($ellipsis->attr['ellipsis']) ? $ellipsis->attr['ellipsis'] : 0;
212 212
213 if (file_exists($f['file']) && 0 <= self::$srcContext) { 213 if (file_exists($f['file']) && 0 <= self::$srcContext) {
214 if (!empty($f['class']) && (is_subclass_of($f['class'], 'Twig\Template') || is_subclass_of($f['class'], 'Twig_Template')) && method_exists($f['class'], 'getDebugInfo')) { 214 if (!empty($f['class']) && (is_subclass_of($f['class'], 'Twig\Template') || is_subclass_of($f['class'], 'Twig_Template')) && method_exists($f['class'], 'getDebugInfo')) {
215 $template = isset($f['object']) ? $f['object'] : unserialize(sprintf('O:%d:"%s":0:{}', strlen($f['class']), $f['class'])); 215 $template = isset($f['object']) ? $f['object'] : unserialize(sprintf('O:%d:"%s":0:{}', \strlen($f['class']), $f['class']));
216 216
217 $ellipsis = 0; 217 $ellipsis = 0;
218 $templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : ''); 218 $templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : '');
219 $templateInfo = $template->getDebugInfo(); 219 $templateInfo = $template->getDebugInfo();
220 if (isset($templateInfo[$f['line']])) { 220 if (isset($templateInfo[$f['line']])) {
229 } 229 }
230 if ($srcKey == $f['file']) { 230 if ($srcKey == $f['file']) {
231 $src = self::extractSource(file_get_contents($f['file']), $f['line'], self::$srcContext, $caller, 'php', $f['file']); 231 $src = self::extractSource(file_get_contents($f['file']), $f['line'], self::$srcContext, $caller, 'php', $f['file']);
232 $srcKey .= ':'.$f['line']; 232 $srcKey .= ':'.$f['line'];
233 if ($ellipsis) { 233 if ($ellipsis) {
234 $ellipsis += 1 + strlen($f['line']); 234 $ellipsis += 1 + \strlen($f['line']);
235 } 235 }
236 } 236 }
237 $srcAttr .= '&separator= '; 237 $srcAttr .= '&separator= ';
238 } else { 238 } else {
239 $srcAttr .= '&separator=:'; 239 $srcAttr .= '&separator=:';
240 } 240 }
241 $srcAttr .= $ellipsis ? '&ellipsis-type=path&ellipsis='.$ellipsis.'&ellipsis-tail='.$ellipsisTail : ''; 241 $srcAttr .= $ellipsis ? '&ellipsis-type=path&ellipsis='.$ellipsis.'&ellipsis-tail='.$ellipsisTail : '';
242 self::$framesCache[$cacheKey] = $a[$prefix.'src'] = new EnumStub(array("\0~$srcAttr\0$srcKey" => $src)); 242 self::$framesCache[$cacheKey] = $a[$prefix.'src'] = new EnumStub(["\0~$srcAttr\0$srcKey" => $src]);
243 } 243 }
244 } 244 }
245 245
246 unset($a[$prefix.'args'], $a[$prefix.'line'], $a[$prefix.'file']); 246 unset($a[$prefix.'args'], $a[$prefix.'line'], $a[$prefix.'file']);
247 if ($frame->inTraceStub) { 247 if ($frame->inTraceStub) {
263 { 263 {
264 if (isset($a[$xPrefix.'trace'])) { 264 if (isset($a[$xPrefix.'trace'])) {
265 $trace = $a[$xPrefix.'trace']; 265 $trace = $a[$xPrefix.'trace'];
266 unset($a[$xPrefix.'trace']); // Ensures the trace is always last 266 unset($a[$xPrefix.'trace']); // Ensures the trace is always last
267 } else { 267 } else {
268 $trace = array(); 268 $trace = [];
269 } 269 }
270 270
271 if (!($filter & Caster::EXCLUDE_VERBOSE) && $trace) { 271 if (!($filter & Caster::EXCLUDE_VERBOSE) && $trace) {
272 if (isset($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line'])) { 272 if (isset($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line'])) {
273 self::traceUnshift($trace, $xClass, $a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line']); 273 self::traceUnshift($trace, $xClass, $a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line']);
289 private static function traceUnshift(&$trace, $class, $file, $line) 289 private static function traceUnshift(&$trace, $class, $file, $line)
290 { 290 {
291 if (isset($trace[0]['file'], $trace[0]['line']) && $trace[0]['file'] === $file && $trace[0]['line'] === $line) { 291 if (isset($trace[0]['file'], $trace[0]['line']) && $trace[0]['file'] === $file && $trace[0]['line'] === $line) {
292 return; 292 return;
293 } 293 }
294 array_unshift($trace, array( 294 array_unshift($trace, [
295 'function' => $class ? 'new '.$class : null, 295 'function' => $class ? 'new '.$class : null,
296 'file' => $file, 296 'file' => $file,
297 'line' => $line, 297 'line' => $line,
298 )); 298 ]);
299 } 299 }
300 300
301 private static function extractSource($srcLines, $line, $srcContext, $title, $lang, $file = null) 301 private static function extractSource($srcLines, $line, $srcContext, $title, $lang, $file = null)
302 { 302 {
303 $srcLines = explode("\n", $srcLines); 303 $srcLines = explode("\n", $srcLines);
304 $src = array(); 304 $src = [];
305 305
306 for ($i = $line - 1 - $srcContext; $i <= $line - 1 + $srcContext; ++$i) { 306 for ($i = $line - 1 - $srcContext; $i <= $line - 1 + $srcContext; ++$i) {
307 $src[] = (isset($srcLines[$i]) ? $srcLines[$i] : '')."\n"; 307 $src[] = (isset($srcLines[$i]) ? $srcLines[$i] : '')."\n";
308 } 308 }
309 309
310 $srcLines = array(); 310 $srcLines = [];
311 $ltrim = 0; 311 $ltrim = 0;
312 do { 312 do {
313 $pad = null; 313 $pad = null;
314 for ($i = $srcContext << 1; $i >= 0; --$i) { 314 for ($i = $srcContext << 1; $i >= 0; --$i) {
315 if (isset($src[$i][$ltrim]) && "\r" !== ($c = $src[$i][$ltrim]) && "\n" !== $c) { 315 if (isset($src[$i][$ltrim]) && "\r" !== ($c = $src[$i][$ltrim]) && "\n" !== $c) {