Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / var-dumper / Caster / ExceptionCaster.php
index 3554becaea7ddd2e99d52cd5f700abaeb32250fe..df61aefb6f2fc0f9d63f483ea155d0531bc42500 100644 (file)
@@ -11,6 +11,7 @@
 
 namespace Symfony\Component\VarDumper\Caster;
 
+use Symfony\Component\Debug\Exception\SilencedErrorContext;
 use Symfony\Component\VarDumper\Exception\ThrowingCasterException;
 use Symfony\Component\VarDumper\Cloner\Stub;
 
@@ -41,6 +42,8 @@ class ExceptionCaster
         E_STRICT => 'E_STRICT',
     );
 
+    private static $framesCache = array();
+
     public static function castError(\Error $e, array $a, Stub $stub, $isNested, $filter = 0)
     {
         return self::filterExceptionArray($stub->class, $a, "\0Error\0", $filter);
@@ -62,17 +65,14 @@ class ExceptionCaster
 
     public static function castThrowingCasterException(ThrowingCasterException $e, array $a, Stub $stub, $isNested)
     {
+        $trace = Caster::PREFIX_VIRTUAL.'trace';
         $prefix = Caster::PREFIX_PROTECTED;
         $xPrefix = "\0Exception\0";
 
-        if (isset($a[$xPrefix.'previous'], $a[$xPrefix.'trace']) && $a[$xPrefix.'previous'] instanceof \Exception) {
+        if (isset($a[$xPrefix.'previous'], $a[$trace]) && $a[$xPrefix.'previous'] instanceof \Exception) {
             $b = (array) $a[$xPrefix.'previous'];
-            array_unshift($b[$xPrefix.'trace'], array(
-                'function' => 'new '.get_class($a[$xPrefix.'previous']),
-                'file' => $b[$prefix.'file'],
-                'line' => $b[$prefix.'line'],
-            ));
-            $a[$xPrefix.'trace'] = new TraceStub($b[$xPrefix.'trace'], false, 0, -1 - count($a[$xPrefix.'trace']->value));
+            self::traceUnshift($b[$xPrefix.'trace'], get_class($a[$xPrefix.'previous']), $b[$prefix.'file'], $b[$prefix.'line']);
+            $a[$trace] = new TraceStub($b[$xPrefix.'trace'], false, 0, -count($a[$trace]->value));
         }
 
         unset($a[$xPrefix.'previous'], $a[$prefix.'code'], $a[$prefix.'file'], $a[$prefix.'line']);
@@ -80,6 +80,33 @@ class ExceptionCaster
         return $a;
     }
 
+    public static function castSilencedErrorContext(SilencedErrorContext $e, array $a, Stub $stub, $isNested)
+    {
+        $sPrefix = "\0".SilencedErrorContext::class."\0";
+
+        if (!isset($a[$s = $sPrefix.'severity'])) {
+            return $a;
+        }
+
+        if (isset(self::$errorTypes[$a[$s]])) {
+            $a[$s] = new ConstStub(self::$errorTypes[$a[$s]], $a[$s]);
+        }
+
+        $trace = array(array(
+            'file' => $a[$sPrefix.'file'],
+            'line' => $a[$sPrefix.'line'],
+        ));
+
+        if (isset($a[$sPrefix.'trace'])) {
+            $trace = array_merge($trace, $a[$sPrefix.'trace']);
+        }
+
+        unset($a[$sPrefix.'file'], $a[$sPrefix.'line'], $a[$sPrefix.'trace']);
+        $a[Caster::PREFIX_VIRTUAL.'trace'] = new TraceStub($trace, self::$traceArgs);
+
+        return $a;
+    }
+
     public static function castTraceStub(TraceStub $trace, array $a, Stub $stub, $isNested)
     {
         if (!$isNested) {
@@ -88,6 +115,7 @@ class ExceptionCaster
         $stub->class = '';
         $stub->handle = 0;
         $frames = $trace->value;
+        $prefix = Caster::PREFIX_VIRTUAL;
 
         $a = array();
         $j = count($frames);
@@ -97,34 +125,55 @@ class ExceptionCaster
         if (!isset($trace->value[$i])) {
             return array();
         }
-        $lastCall = isset($frames[$i]['function']) ? ' ==> '.(isset($frames[$i]['class']) ? $frames[0]['class'].$frames[$i]['type'] : '').$frames[$i]['function'].'()' : '';
+        $lastCall = isset($frames[$i]['function']) ? (isset($frames[$i]['class']) ? $frames[0]['class'].$frames[$i]['type'] : '').$frames[$i]['function'].'()' : '';
+        $frames[] = array('function' => '');
+        $collapse = false;
 
         for ($j += $trace->numberingOffset - $i++; isset($frames[$i]); ++$i, --$j) {
-            $call = isset($frames[$i]['function']) ? (isset($frames[$i]['class']) ? $frames[$i]['class'].$frames[$i]['type'] : '').$frames[$i]['function'].'()' : '???';
+            $f = $frames[$i];
+            $call = isset($f['function']) ? (isset($f['class']) ? $f['class'].$f['type'] : '').$f['function'] : '???';
 
-            $a[Caster::PREFIX_VIRTUAL.$j.'. '.$call.$lastCall] = new FrameStub(
+            $frame = new FrameStub(
                 array(
-                    'object' => isset($frames[$i]['object']) ? $frames[$i]['object'] : null,
-                    'class' => isset($frames[$i]['class']) ? $frames[$i]['class'] : null,
-                    'type' => isset($frames[$i]['type']) ? $frames[$i]['type'] : null,
-                    'function' => isset($frames[$i]['function']) ? $frames[$i]['function'] : null,
+                    'object' => isset($f['object']) ? $f['object'] : null,
+                    'class' => isset($f['class']) ? $f['class'] : null,
+                    'type' => isset($f['type']) ? $f['type'] : null,
+                    'function' => isset($f['function']) ? $f['function'] : null,
                 ) + $frames[$i - 1],
-                $trace->keepArgs,
+                false,
                 true
             );
+            $f = self::castFrameStub($frame, array(), $frame, true);
+            if (isset($f[$prefix.'src'])) {
+                foreach ($f[$prefix.'src']->value as $label => $frame) {
+                    if (0 === strpos($label, "\0~collapse=0")) {
+                        if ($collapse) {
+                            $label = substr_replace($label, '1', 11, 1);
+                        } else {
+                            $collapse = true;
+                        }
+                    }
+                    $label = substr_replace($label, "title=Stack level $j.&", 2, 0);
+                }
+                $f = $frames[$i - 1];
+                if ($trace->keepArgs && !empty($f['args']) && $frame instanceof EnumStub) {
+                    $frame->value['arguments'] = new ArgsStub($f['args'], isset($f['function']) ? $f['function'] : null, isset($f['class']) ? $f['class'] : null);
+                }
+            } elseif ('???' !== $lastCall) {
+                $label = new ClassStub($lastCall);
+                if (isset($label->attr['ellipsis'])) {
+                    $label->attr['ellipsis'] += 2;
+                    $label = substr_replace($prefix, "ellipsis-type=class&ellipsis={$label->attr['ellipsis']}&ellipsis-tail=1&title=Stack level $j.", 2, 0).$label->value.'()';
+                } else {
+                    $label = substr_replace($prefix, "title=Stack level $j.", 2, 0).$label->value.'()';
+                }
+            } else {
+                $label = substr_replace($prefix, "title=Stack level $j.", 2, 0).$lastCall;
+            }
+            $a[substr_replace($label, sprintf('separator=%s&', $frame instanceof EnumStub ? ' ' : ':'), 2, 0)] = $frame;
 
-            $lastCall = ' ==> '.$call;
+            $lastCall = $call;
         }
-        $a[Caster::PREFIX_VIRTUAL.$j.'. {main}'.$lastCall] = new FrameStub(
-            array(
-                'object' => null,
-                'class' => null,
-                'type' => null,
-                'function' => '{main}',
-            ) + $frames[$i - 1],
-            $trace->keepArgs,
-            true
-        );
         if (null !== $trace->sliceLength) {
             $a = array_slice($a, 0, $trace->sliceLength, true);
         }
@@ -141,35 +190,57 @@ class ExceptionCaster
         $prefix = Caster::PREFIX_VIRTUAL;
 
         if (isset($f['file'], $f['line'])) {
-            if (preg_match('/\((\d+)\)(?:\([\da-f]{32}\))? : (?:eval\(\)\'d code|runtime-created function)$/', $f['file'], $match)) {
-                $f['file'] = substr($f['file'], 0, -strlen($match[0]));
-                $f['line'] = (int) $match[1];
-            }
-            if (file_exists($f['file']) && 0 <= self::$srcContext) {
-                $src[$f['file'].':'.$f['line']] = self::extractSource(explode("\n", file_get_contents($f['file'])), $f['line'], self::$srcContext);
-
-                if (!empty($f['class']) && (is_subclass_of($f['class'], 'Twig\Template') || is_subclass_of($f['class'], 'Twig_Template')) && method_exists($f['class'], 'getDebugInfo')) {
-                    $template = isset($f['object']) ? $f['object'] : unserialize(sprintf('O:%d:"%s":0:{}', strlen($f['class']), $f['class']));
-
-                    $templateName = $template->getTemplateName();
-                    $templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : '');
-                    $templateInfo = $template->getDebugInfo();
-                    if (isset($templateInfo[$f['line']])) {
-                        if (method_exists($template, 'getSourceContext')) {
-                            $templateName = $template->getSourceContext()->getPath() ?: $templateName;
+            $cacheKey = $f;
+            unset($cacheKey['object'], $cacheKey['args']);
+            $cacheKey[] = self::$srcContext;
+            $cacheKey = implode('-', $cacheKey);
+
+            if (isset(self::$framesCache[$cacheKey])) {
+                $a[$prefix.'src'] = self::$framesCache[$cacheKey];
+            } else {
+                if (preg_match('/\((\d+)\)(?:\([\da-f]{32}\))? : (?:eval\(\)\'d code|runtime-created function)$/', $f['file'], $match)) {
+                    $f['file'] = substr($f['file'], 0, -strlen($match[0]));
+                    $f['line'] = (int) $match[1];
+                }
+                $caller = isset($f['function']) ? sprintf('in %s() on line %d', (isset($f['class']) ? $f['class'].$f['type'] : '').$f['function'], $f['line']) : null;
+                $src = $f['line'];
+                $srcKey = $f['file'];
+                $ellipsis = new LinkStub($srcKey, 0);
+                $srcAttr = 'collapse='.(int) $ellipsis->inVendor;
+                $ellipsisTail = isset($ellipsis->attr['ellipsis-tail']) ? $ellipsis->attr['ellipsis-tail'] : 0;
+                $ellipsis = isset($ellipsis->attr['ellipsis']) ? $ellipsis->attr['ellipsis'] : 0;
+
+                if (file_exists($f['file']) && 0 <= self::$srcContext) {
+                    if (!empty($f['class']) && (is_subclass_of($f['class'], 'Twig\Template') || is_subclass_of($f['class'], 'Twig_Template')) && method_exists($f['class'], 'getDebugInfo')) {
+                        $template = isset($f['object']) ? $f['object'] : unserialize(sprintf('O:%d:"%s":0:{}', strlen($f['class']), $f['class']));
+
+                        $ellipsis = 0;
+                        $templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : '');
+                        $templateInfo = $template->getDebugInfo();
+                        if (isset($templateInfo[$f['line']])) {
+                            if (!method_exists($template, 'getSourceContext') || !file_exists($templatePath = $template->getSourceContext()->getPath())) {
+                                $templatePath = null;
+                            }
+                            if ($templateSrc) {
+                                $src = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext, $caller, 'twig', $templatePath);
+                                $srcKey = ($templatePath ?: $template->getTemplateName()).':'.$templateInfo[$f['line']];
+                            }
                         }
-                        if ($templateSrc) {
-                            $templateSrc = explode("\n", $templateSrc);
-                            $src[$templateName.':'.$templateInfo[$f['line']]] = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext);
-                        } else {
-                            $src[$templateName] = $templateInfo[$f['line']];
+                    }
+                    if ($srcKey == $f['file']) {
+                        $src = self::extractSource(file_get_contents($f['file']), $f['line'], self::$srcContext, $caller, 'php', $f['file']);
+                        $srcKey .= ':'.$f['line'];
+                        if ($ellipsis) {
+                            $ellipsis += 1 + strlen($f['line']);
                         }
                     }
+                    $srcAttr .= '&separator= ';
+                } else {
+                    $srcAttr .= '&separator=:';
                 }
-            } else {
-                $src[$f['file']] = $f['line'];
+                $srcAttr .= $ellipsis ? '&ellipsis-type=path&ellipsis='.$ellipsis.'&ellipsis-tail='.$ellipsisTail : '';
+                self::$framesCache[$cacheKey] = $a[$prefix.'src'] = new EnumStub(array("\0~$srcAttr\0$srcKey" => $src));
             }
-            $a[$prefix.'src'] = new EnumStub($src);
         }
 
         unset($a[$prefix.'args'], $a[$prefix.'line'], $a[$prefix.'file']);
@@ -181,49 +252,13 @@ class ExceptionCaster
                 unset($a[$k]);
             }
         }
-        if ($frame->keepArgs && isset($f['args'])) {
-            $a[$prefix.'args'] = $f['args'];
+        if ($frame->keepArgs && !empty($f['args'])) {
+            $a[$prefix.'arguments'] = new ArgsStub($f['args'], $f['function'], $f['class']);
         }
 
         return $a;
     }
 
-    /**
-     * @deprecated since 2.8, to be removed in 3.0. Use the castTraceStub method instead.
-     */
-    public static function filterTrace(&$trace, $dumpArgs, $offset = 0)
-    {
-        @trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0. Use the castTraceStub method instead.', E_USER_DEPRECATED);
-
-        if (0 > $offset || empty($trace[$offset])) {
-            return $trace = null;
-        }
-
-        $t = $trace[$offset];
-
-        if (empty($t['class']) && isset($t['function'])) {
-            if ('user_error' === $t['function'] || 'trigger_error' === $t['function']) {
-                ++$offset;
-            }
-        }
-
-        if ($offset) {
-            array_splice($trace, 0, $offset);
-        }
-
-        foreach ($trace as &$t) {
-            $t = array(
-                'call' => (isset($t['class']) ? $t['class'].$t['type'] : '').$t['function'].'()',
-                'file' => isset($t['line']) ? "{$t['file']}:{$t['line']}" : '',
-                'args' => &$t['args'],
-            );
-
-            if (!isset($t['args']) || !$dumpArgs) {
-                unset($t['args']);
-            }
-        }
-    }
-
     private static function filterExceptionArray($xClass, array $a, $xPrefix, $filter)
     {
         if (isset($a[$xPrefix.'trace'])) {
@@ -233,32 +268,46 @@ class ExceptionCaster
             $trace = array();
         }
 
-        if (!($filter & Caster::EXCLUDE_VERBOSE)) {
+        if (!($filter & Caster::EXCLUDE_VERBOSE) && $trace) {
             if (isset($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line'])) {
-                array_unshift($trace, array(
-                    'function' => $xClass ? 'new '.$xClass : null,
-                    'file' => $a[Caster::PREFIX_PROTECTED.'file'],
-                    'line' => $a[Caster::PREFIX_PROTECTED.'line'],
-                ));
+                self::traceUnshift($trace, $xClass, $a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line']);
             }
-            $a[$xPrefix.'trace'] = new TraceStub($trace, self::$traceArgs);
+            $a[Caster::PREFIX_VIRTUAL.'trace'] = new TraceStub($trace, self::$traceArgs);
         }
         if (empty($a[$xPrefix.'previous'])) {
             unset($a[$xPrefix.'previous']);
         }
         unset($a[$xPrefix.'string'], $a[Caster::PREFIX_DYNAMIC.'xdebug_message'], $a[Caster::PREFIX_DYNAMIC.'__destructorException']);
 
+        if (isset($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line'])) {
+            $a[Caster::PREFIX_PROTECTED.'file'] = new LinkStub($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line']);
+        }
+
         return $a;
     }
 
-    private static function extractSource(array $srcArray, $line, $srcContext)
+    private static function traceUnshift(&$trace, $class, $file, $line)
+    {
+        if (isset($trace[0]['file'], $trace[0]['line']) && $trace[0]['file'] === $file && $trace[0]['line'] === $line) {
+            return;
+        }
+        array_unshift($trace, array(
+            'function' => $class ? 'new '.$class : null,
+            'file' => $file,
+            'line' => $line,
+        ));
+    }
+
+    private static function extractSource($srcLines, $line, $srcContext, $title, $lang, $file = null)
     {
+        $srcLines = explode("\n", $srcLines);
         $src = array();
 
         for ($i = $line - 1 - $srcContext; $i <= $line - 1 + $srcContext; ++$i) {
-            $src[] = (isset($srcArray[$i]) ? $srcArray[$i] : '')."\n";
+            $src[] = (isset($srcLines[$i]) ? $srcLines[$i] : '')."\n";
         }
 
+        $srcLines = array();
         $ltrim = 0;
         do {
             $pad = null;
@@ -275,12 +324,26 @@ class ExceptionCaster
             ++$ltrim;
         } while (0 > $i && null !== $pad);
 
-        if (--$ltrim) {
-            foreach ($src as $i => $line) {
-                $src[$i] = isset($line[$ltrim]) && "\r" !== $line[$ltrim] ? substr($line, $ltrim) : ltrim($line, " \t");
+        --$ltrim;
+
+        foreach ($src as $i => $c) {
+            if ($ltrim) {
+                $c = isset($c[$ltrim]) && "\r" !== $c[$ltrim] ? substr($c, $ltrim) : ltrim($c, " \t");
+            }
+            $c = substr($c, 0, -1);
+            if ($i !== $srcContext) {
+                $c = new ConstStub('default', $c);
+            } else {
+                $c = new ConstStub($c, $title);
+                if (null !== $file) {
+                    $c->attr['file'] = $file;
+                    $c->attr['line'] = $line;
+                }
             }
+            $c->attr['lang'] = $lang;
+            $srcLines[sprintf("\0~separator=› &%d\0", $i + $line - $srcContext)] = $c;
         }
 
-        return implode('', $src);
+        return new EnumStub($srcLines);
     }
 }