vendor/symfony/var-dumper/Caster/ExceptionCaster.php line 187

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