AmqpCaster.php
2 years ago
ArgsStub.php
1 year ago
Caster.php
1 year ago
ClassStub.php
1 year ago
ConstStub.php
2 years ago
CutArrayStub.php
2 years ago
CutStub.php
1 year ago
DOMCaster.php
1 year ago
DateCaster.php
2 years ago
DoctrineCaster.php
2 years ago
DsCaster.php
2 years ago
DsPairStub.php
2 years ago
EnumStub.php
1 year ago
ExceptionCaster.php
1 year ago
FiberCaster.php
2 years ago
FrameStub.php
1 year ago
GmpCaster.php
2 years ago
ImagineCaster.php
2 years ago
ImgStub.php
2 years ago
IntlCaster.php
2 years ago
LinkStub.php
1 year ago
MemcachedCaster.php
2 years ago
MysqliCaster.php
2 years ago
PdoCaster.php
2 years ago
PgSqlCaster.php
1 year ago
ProxyManagerCaster.php
2 years ago
RdKafkaCaster.php
1 year ago
RedisCaster.php
2 years ago
ReflectionCaster.php
1 year ago
ResourceCaster.php
1 year ago
SplCaster.php
1 year ago
StubCaster.php
2 years ago
SymfonyCaster.php
2 years ago
TraceStub.php
1 year ago
UuidCaster.php
2 years ago
XmlReaderCaster.php
1 year ago
XmlResourceCaster.php
2 years ago
ExceptionCaster.php
312 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace Matomo\Dependencies\Symfony\Component\VarDumper\Caster; |
| 12 | |
| 13 | use Matomo\Dependencies\Symfony\Component\ErrorHandler\Exception\SilencedErrorContext; |
| 14 | use Matomo\Dependencies\Symfony\Component\VarDumper\Cloner\Stub; |
| 15 | use Matomo\Dependencies\Symfony\Component\VarDumper\Exception\ThrowingCasterException; |
| 16 | /** |
| 17 | * Casts common Exception classes to array representation. |
| 18 | * |
| 19 | * @author Nicolas Grekas <p@tchwork.com> |
| 20 | * |
| 21 | * @final |
| 22 | */ |
| 23 | class ExceptionCaster |
| 24 | { |
| 25 | public static $srcContext = 1; |
| 26 | public static $traceArgs = \true; |
| 27 | public static $errorTypes = [\E_DEPRECATED => 'E_DEPRECATED', \E_USER_DEPRECATED => 'E_USER_DEPRECATED', \E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR', \E_ERROR => 'E_ERROR', \E_WARNING => 'E_WARNING', \E_PARSE => 'E_PARSE', \E_NOTICE => 'E_NOTICE', \E_CORE_ERROR => 'E_CORE_ERROR', \E_CORE_WARNING => 'E_CORE_WARNING', \E_COMPILE_ERROR => 'E_COMPILE_ERROR', \E_COMPILE_WARNING => 'E_COMPILE_WARNING', \E_USER_ERROR => 'E_USER_ERROR', \E_USER_WARNING => 'E_USER_WARNING', \E_USER_NOTICE => 'E_USER_NOTICE', 2048 => 'E_STRICT']; |
| 28 | private static $framesCache = []; |
| 29 | public static function castError(\Error $e, array $a, Stub $stub, bool $isNested, int $filter = 0) |
| 30 | { |
| 31 | return self::filterExceptionArray($stub->class, $a, "\x00Error\x00", $filter); |
| 32 | } |
| 33 | public static function castException(\Exception $e, array $a, Stub $stub, bool $isNested, int $filter = 0) |
| 34 | { |
| 35 | return self::filterExceptionArray($stub->class, $a, "\x00Exception\x00", $filter); |
| 36 | } |
| 37 | public static function castErrorException(\ErrorException $e, array $a, Stub $stub, bool $isNested) |
| 38 | { |
| 39 | if (isset($a[$s = Caster::PREFIX_PROTECTED . 'severity'], self::$errorTypes[$a[$s]])) { |
| 40 | $a[$s] = new ConstStub(self::$errorTypes[$a[$s]], $a[$s]); |
| 41 | } |
| 42 | return $a; |
| 43 | } |
| 44 | public static function castThrowingCasterException(ThrowingCasterException $e, array $a, Stub $stub, bool $isNested) |
| 45 | { |
| 46 | $trace = Caster::PREFIX_VIRTUAL . 'trace'; |
| 47 | $prefix = Caster::PREFIX_PROTECTED; |
| 48 | $xPrefix = "\x00Exception\x00"; |
| 49 | if (isset($a[$xPrefix . 'previous'], $a[$trace]) && $a[$xPrefix . 'previous'] instanceof \Exception) { |
| 50 | $b = (array) $a[$xPrefix . 'previous']; |
| 51 | $class = get_debug_type($a[$xPrefix . 'previous']); |
| 52 | self::traceUnshift($b[$xPrefix . 'trace'], $class, $b[$prefix . 'file'], $b[$prefix . 'line']); |
| 53 | $a[$trace] = new TraceStub($b[$xPrefix . 'trace'], \false, 0, -\count($a[$trace]->value)); |
| 54 | } |
| 55 | unset($a[$xPrefix . 'previous'], $a[$prefix . 'code'], $a[$prefix . 'file'], $a[$prefix . 'line']); |
| 56 | return $a; |
| 57 | } |
| 58 | public static function castSilencedErrorContext(SilencedErrorContext $e, array $a, Stub $stub, bool $isNested) |
| 59 | { |
| 60 | $sPrefix = "\x00" . SilencedErrorContext::class . "\x00"; |
| 61 | if (!isset($a[$s = $sPrefix . 'severity'])) { |
| 62 | return $a; |
| 63 | } |
| 64 | if (isset(self::$errorTypes[$a[$s]])) { |
| 65 | $a[$s] = new ConstStub(self::$errorTypes[$a[$s]], $a[$s]); |
| 66 | } |
| 67 | $trace = [['file' => $a[$sPrefix . 'file'], 'line' => $a[$sPrefix . 'line']]]; |
| 68 | if (isset($a[$sPrefix . 'trace'])) { |
| 69 | $trace = array_merge($trace, $a[$sPrefix . 'trace']); |
| 70 | } |
| 71 | unset($a[$sPrefix . 'file'], $a[$sPrefix . 'line'], $a[$sPrefix . 'trace']); |
| 72 | $a[Caster::PREFIX_VIRTUAL . 'trace'] = new TraceStub($trace, self::$traceArgs); |
| 73 | return $a; |
| 74 | } |
| 75 | public static function castTraceStub(TraceStub $trace, array $a, Stub $stub, bool $isNested) |
| 76 | { |
| 77 | if (!$isNested) { |
| 78 | return $a; |
| 79 | } |
| 80 | $stub->class = ''; |
| 81 | $stub->handle = 0; |
| 82 | $frames = $trace->value; |
| 83 | $prefix = Caster::PREFIX_VIRTUAL; |
| 84 | $a = []; |
| 85 | $j = \count($frames); |
| 86 | if (0 > ($i = $trace->sliceOffset)) { |
| 87 | $i = max(0, $j + $i); |
| 88 | } |
| 89 | if (!isset($trace->value[$i])) { |
| 90 | return []; |
| 91 | } |
| 92 | $lastCall = isset($frames[$i]['function']) ? (isset($frames[$i]['class']) ? $frames[0]['class'] . $frames[$i]['type'] : '') . $frames[$i]['function'] . '()' : ''; |
| 93 | $frames[] = ['function' => '']; |
| 94 | $collapse = \false; |
| 95 | for ($j += $trace->numberingOffset - $i++; isset($frames[$i]); ++$i, --$j) { |
| 96 | $f = $frames[$i]; |
| 97 | $call = isset($f['function']) ? (isset($f['class']) ? $f['class'] . $f['type'] : '') . $f['function'] : '???'; |
| 98 | $frame = new FrameStub(['object' => $f['object'] ?? null, 'class' => $f['class'] ?? null, 'type' => $f['type'] ?? null, 'function' => $f['function'] ?? null] + $frames[$i - 1], \false, \true); |
| 99 | $f = self::castFrameStub($frame, [], $frame, \true); |
| 100 | if (isset($f[$prefix . 'src'])) { |
| 101 | foreach ($f[$prefix . 'src']->value as $label => $frame) { |
| 102 | if (str_starts_with($label, "\x00~collapse=0")) { |
| 103 | if ($collapse) { |
| 104 | $label = substr_replace($label, '1', 11, 1); |
| 105 | } else { |
| 106 | $collapse = \true; |
| 107 | } |
| 108 | } |
| 109 | $label = substr_replace($label, "title=Stack level {$j}.&", 2, 0); |
| 110 | } |
| 111 | $f = $frames[$i - 1]; |
| 112 | if ($trace->keepArgs && !empty($f['args']) && $frame instanceof EnumStub) { |
| 113 | $frame->value['arguments'] = new ArgsStub($f['args'], $f['function'] ?? null, $f['class'] ?? null); |
| 114 | } |
| 115 | } elseif ('???' !== $lastCall) { |
| 116 | $label = new ClassStub($lastCall); |
| 117 | if (isset($label->attr['ellipsis'])) { |
| 118 | $label->attr['ellipsis'] += 2; |
| 119 | $label = substr_replace($prefix, "ellipsis-type=class&ellipsis={$label->attr['ellipsis']}&ellipsis-tail=1&title=Stack level {$j}.", 2, 0) . $label->value . '()'; |
| 120 | } else { |
| 121 | $label = substr_replace($prefix, "title=Stack level {$j}.", 2, 0) . $label->value . '()'; |
| 122 | } |
| 123 | } else { |
| 124 | $label = substr_replace($prefix, "title=Stack level {$j}.", 2, 0) . $lastCall; |
| 125 | } |
| 126 | $a[substr_replace($label, sprintf('separator=%s&', $frame instanceof EnumStub ? ' ' : ':'), 2, 0)] = $frame; |
| 127 | $lastCall = $call; |
| 128 | } |
| 129 | if (null !== $trace->sliceLength) { |
| 130 | $a = \array_slice($a, 0, $trace->sliceLength, \true); |
| 131 | } |
| 132 | return $a; |
| 133 | } |
| 134 | public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, bool $isNested) |
| 135 | { |
| 136 | if (!$isNested) { |
| 137 | return $a; |
| 138 | } |
| 139 | $f = $frame->value; |
| 140 | $prefix = Caster::PREFIX_VIRTUAL; |
| 141 | if (isset($f['file'], $f['line'])) { |
| 142 | $cacheKey = $f; |
| 143 | unset($cacheKey['object'], $cacheKey['args']); |
| 144 | $cacheKey[] = self::$srcContext; |
| 145 | $cacheKey = implode('-', $cacheKey); |
| 146 | if (isset(self::$framesCache[$cacheKey])) { |
| 147 | $a[$prefix . 'src'] = self::$framesCache[$cacheKey]; |
| 148 | } else { |
| 149 | if (preg_match('/\\((\\d+)\\)(?:\\([\\da-f]{32}\\))? : (?:eval\\(\\)\'d code|runtime-created function)$/', $f['file'], $match)) { |
| 150 | $f['file'] = substr($f['file'], 0, -\strlen($match[0])); |
| 151 | $f['line'] = (int) $match[1]; |
| 152 | } |
| 153 | $src = $f['line']; |
| 154 | $srcKey = $f['file']; |
| 155 | $ellipsis = new LinkStub($srcKey, 0); |
| 156 | $srcAttr = 'collapse=' . (int) $ellipsis->inVendor; |
| 157 | $ellipsisTail = $ellipsis->attr['ellipsis-tail'] ?? 0; |
| 158 | $ellipsis = $ellipsis->attr['ellipsis'] ?? 0; |
| 159 | if (is_file($f['file']) && 0 <= self::$srcContext) { |
| 160 | if (!empty($f['class']) && (is_subclass_of($f['class'], 'Matomo\\Dependencies\\Twig\\Template') || is_subclass_of($f['class'], 'Matomo\\Dependencies\\Twig_Template')) && method_exists($f['class'], 'getDebugInfo')) { |
| 161 | $template = null; |
| 162 | if (isset($f['object'])) { |
| 163 | $template = $f['object']; |
| 164 | } elseif ((new \ReflectionClass($f['class']))->isInstantiable()) { |
| 165 | $template = unserialize(sprintf('O:%d:"%s":0:{}', \strlen($f['class']), $f['class'])); |
| 166 | } |
| 167 | if (null !== $template) { |
| 168 | $ellipsis = 0; |
| 169 | $templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : ''); |
| 170 | $templateInfo = $template->getDebugInfo(); |
| 171 | if (isset($templateInfo[$f['line']])) { |
| 172 | if (!method_exists($template, 'getSourceContext') || !is_file($templatePath = $template->getSourceContext()->getPath())) { |
| 173 | $templatePath = null; |
| 174 | } |
| 175 | if ($templateSrc) { |
| 176 | $src = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext, 'twig', $templatePath, $f); |
| 177 | $srcKey = ($templatePath ?: $template->getTemplateName()) . ':' . $templateInfo[$f['line']]; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | if ($srcKey == $f['file']) { |
| 183 | $src = self::extractSource(file_get_contents($f['file']), $f['line'], self::$srcContext, 'php', $f['file'], $f); |
| 184 | $srcKey .= ':' . $f['line']; |
| 185 | if ($ellipsis) { |
| 186 | $ellipsis += 1 + \strlen($f['line']); |
| 187 | } |
| 188 | } |
| 189 | $srcAttr .= sprintf('&separator= &file=%s&line=%d', rawurlencode($f['file']), $f['line']); |
| 190 | } else { |
| 191 | $srcAttr .= '&separator=:'; |
| 192 | } |
| 193 | $srcAttr .= $ellipsis ? '&ellipsis-type=path&ellipsis=' . $ellipsis . '&ellipsis-tail=' . $ellipsisTail : ''; |
| 194 | self::$framesCache[$cacheKey] = $a[$prefix . 'src'] = new EnumStub(["\x00~{$srcAttr}\x00{$srcKey}" => $src]); |
| 195 | } |
| 196 | } |
| 197 | unset($a[$prefix . 'args'], $a[$prefix . 'line'], $a[$prefix . 'file']); |
| 198 | if ($frame->inTraceStub) { |
| 199 | unset($a[$prefix . 'class'], $a[$prefix . 'type'], $a[$prefix . 'function']); |
| 200 | } |
| 201 | foreach ($a as $k => $v) { |
| 202 | if (!$v) { |
| 203 | unset($a[$k]); |
| 204 | } |
| 205 | } |
| 206 | if ($frame->keepArgs && !empty($f['args'])) { |
| 207 | $a[$prefix . 'arguments'] = new ArgsStub($f['args'], $f['function'], $f['class']); |
| 208 | } |
| 209 | return $a; |
| 210 | } |
| 211 | private static function filterExceptionArray(string $xClass, array $a, string $xPrefix, int $filter) : array |
| 212 | { |
| 213 | if (isset($a[$xPrefix . 'trace'])) { |
| 214 | $trace = $a[$xPrefix . 'trace']; |
| 215 | unset($a[$xPrefix . 'trace']); |
| 216 | // Ensures the trace is always last |
| 217 | } else { |
| 218 | $trace = []; |
| 219 | } |
| 220 | if (!($filter & Caster::EXCLUDE_VERBOSE) && $trace) { |
| 221 | if (isset($a[Caster::PREFIX_PROTECTED . 'file'], $a[Caster::PREFIX_PROTECTED . 'line'])) { |
| 222 | self::traceUnshift($trace, $xClass, $a[Caster::PREFIX_PROTECTED . 'file'], $a[Caster::PREFIX_PROTECTED . 'line']); |
| 223 | } |
| 224 | $a[Caster::PREFIX_VIRTUAL . 'trace'] = new TraceStub($trace, self::$traceArgs); |
| 225 | } |
| 226 | if (empty($a[$xPrefix . 'previous'])) { |
| 227 | unset($a[$xPrefix . 'previous']); |
| 228 | } |
| 229 | unset($a[$xPrefix . 'string'], $a[Caster::PREFIX_DYNAMIC . 'xdebug_message'], $a[Caster::PREFIX_DYNAMIC . '__destructorException']); |
| 230 | if (isset($a[Caster::PREFIX_PROTECTED . 'message']) && str_contains($a[Caster::PREFIX_PROTECTED . 'message'], "@anonymous\x00")) { |
| 231 | $a[Caster::PREFIX_PROTECTED . 'message'] = preg_replace_callback('/[a-zA-Z_\\x7f-\\xff][\\\\a-zA-Z0-9_\\x7f-\\xff]*+@anonymous\\x00.*?\\.php(?:0x?|:[0-9]++\\$)?[0-9a-fA-F]++/', function ($m) { |
| 232 | return class_exists($m[0], \false) ? ((get_parent_class($m[0]) ?: key(class_implements($m[0]))) ?: 'class') . '@anonymous' : $m[0]; |
| 233 | }, $a[Caster::PREFIX_PROTECTED . 'message']); |
| 234 | } |
| 235 | if (isset($a[Caster::PREFIX_PROTECTED . 'file'], $a[Caster::PREFIX_PROTECTED . 'line'])) { |
| 236 | $a[Caster::PREFIX_PROTECTED . 'file'] = new LinkStub($a[Caster::PREFIX_PROTECTED . 'file'], $a[Caster::PREFIX_PROTECTED . 'line']); |
| 237 | } |
| 238 | return $a; |
| 239 | } |
| 240 | private static function traceUnshift(array &$trace, ?string $class, string $file, int $line) : void |
| 241 | { |
| 242 | if (isset($trace[0]['file'], $trace[0]['line']) && $trace[0]['file'] === $file && $trace[0]['line'] === $line) { |
| 243 | return; |
| 244 | } |
| 245 | array_unshift($trace, ['function' => $class ? 'new ' . $class : null, 'file' => $file, 'line' => $line]); |
| 246 | } |
| 247 | private static function extractSource(string $srcLines, int $line, int $srcContext, string $lang, ?string $file, array $frame) : EnumStub |
| 248 | { |
| 249 | $srcLines = explode("\n", $srcLines); |
| 250 | $src = []; |
| 251 | for ($i = $line - 1 - $srcContext; $i <= $line - 1 + $srcContext; ++$i) { |
| 252 | $src[] = ($srcLines[$i] ?? '') . "\n"; |
| 253 | } |
| 254 | if ($frame['function'] ?? \false) { |
| 255 | $stub = new CutStub(new \stdClass()); |
| 256 | $stub->class = (isset($frame['class']) ? $frame['class'] . $frame['type'] : '') . $frame['function']; |
| 257 | $stub->type = Stub::TYPE_OBJECT; |
| 258 | $stub->attr['cut_hash'] = \true; |
| 259 | $stub->attr['file'] = $frame['file']; |
| 260 | $stub->attr['line'] = $frame['line']; |
| 261 | try { |
| 262 | $caller = isset($frame['class']) ? new \ReflectionMethod($frame['class'], $frame['function']) : new \ReflectionFunction($frame['function']); |
| 263 | $stub->class .= ReflectionCaster::getSignature(ReflectionCaster::castFunctionAbstract($caller, [], $stub, \true, Caster::EXCLUDE_VERBOSE)); |
| 264 | if ($f = $caller->getFileName()) { |
| 265 | $stub->attr['file'] = $f; |
| 266 | $stub->attr['line'] = $caller->getStartLine(); |
| 267 | } |
| 268 | } catch (\ReflectionException $e) { |
| 269 | // ignore fake class/function |
| 270 | } |
| 271 | $srcLines = ["\x00~separator=\x00" => $stub]; |
| 272 | } else { |
| 273 | $stub = null; |
| 274 | $srcLines = []; |
| 275 | } |
| 276 | $ltrim = 0; |
| 277 | do { |
| 278 | $pad = null; |
| 279 | for ($i = $srcContext << 1; $i >= 0; --$i) { |
| 280 | if (isset($src[$i][$ltrim]) && "\r" !== ($c = $src[$i][$ltrim]) && "\n" !== $c) { |
| 281 | if (null === $pad) { |
| 282 | $pad = $c; |
| 283 | } |
| 284 | if (' ' !== $c && "\t" !== $c || $pad !== $c) { |
| 285 | break; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | ++$ltrim; |
| 290 | } while (0 > $i && null !== $pad); |
| 291 | --$ltrim; |
| 292 | foreach ($src as $i => $c) { |
| 293 | if ($ltrim) { |
| 294 | $c = isset($c[$ltrim]) && "\r" !== $c[$ltrim] ? substr($c, $ltrim) : ltrim($c, " \t"); |
| 295 | } |
| 296 | $c = substr($c, 0, -1); |
| 297 | if ($i !== $srcContext) { |
| 298 | $c = new ConstStub('default', $c); |
| 299 | } else { |
| 300 | $c = new ConstStub($c, $stub ? 'in ' . $stub->class : ''); |
| 301 | if (null !== $file) { |
| 302 | $c->attr['file'] = $file; |
| 303 | $c->attr['line'] = $line; |
| 304 | } |
| 305 | } |
| 306 | $c->attr['lang'] = $lang; |
| 307 | $srcLines[sprintf("\x00~separator=› &%d\x00", $i + $line - $srcContext)] = $c; |
| 308 | } |
| 309 | return new EnumStub($srcLines); |
| 310 | } |
| 311 | } |
| 312 |