From 9944589068a31b05988c22ce9e1660788e0de400 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 26 Mar 2015 18:01:05 +0100 Subject: [PATCH] [VarDumper] Fix dumping ThrowingCasterException --- .../VarDumper/Caster/ExceptionCaster.php | 26 +++------- .../Component/VarDumper/Caster/PdoCaster.php | 22 ++++---- .../VarDumper/Cloner/AbstractCloner.php | 2 +- .../Exception/ThrowingCasterException.php | 11 +--- .../VarDumper/Tests/CliDumperTest.php | 52 +++++++++++++++++++ 5 files changed, 73 insertions(+), 40 deletions(-) diff --git a/src/Symfony/Component/VarDumper/Caster/ExceptionCaster.php b/src/Symfony/Component/VarDumper/Caster/ExceptionCaster.php index be7d417f41..7938da63e5 100644 --- a/src/Symfony/Component/VarDumper/Caster/ExceptionCaster.php +++ b/src/Symfony/Component/VarDumper/Caster/ExceptionCaster.php @@ -71,27 +71,17 @@ class ExceptionCaster { $b = (array) $a["\0Exception\0previous"]; - array_splice($b["\0Exception\0trace"], count($a["\0Exception\0trace"])); - - $t = static::$traceArgs; - static::$traceArgs = false; - $b = static::castException($a["\0Exception\0previous"], $b, $stub, $isNested); - static::$traceArgs = $t; - - if (empty($a["\0*\0message"])) { - $a["\0*\0message"] = "Unexpected exception thrown from a caster: ".get_class($a["\0Exception\0previous"]); - } - if (isset($b["\0*\0message"])) { $a["\0~\0message"] = $b["\0*\0message"]; } - if (isset($b["\0*\0file"])) { - $a["\0~\0file"] = $b["\0*\0file"]; - } - if (isset($b["\0*\0line"])) { - $a["\0~\0line"] = $b["\0*\0line"]; - } - if (isset($b["\0Exception\0trace"])) { + + if (isset($a["\0Exception\0trace"])) { + $b["\0Exception\0trace"][0] += array( + 'file' => $b["\0*\0file"], + 'line' => $b["\0*\0line"], + ); + array_splice($b["\0Exception\0trace"], -1 - count($a["\0Exception\0trace"])); + static::filterTrace($b["\0Exception\0trace"], false); $a["\0~\0trace"] = $b["\0Exception\0trace"]; } diff --git a/src/Symfony/Component/VarDumper/Caster/PdoCaster.php b/src/Symfony/Component/VarDumper/Caster/PdoCaster.php index 0f9c919794..5f99fcd3c7 100644 --- a/src/Symfony/Component/VarDumper/Caster/PdoCaster.php +++ b/src/Symfony/Component/VarDumper/Caster/PdoCaster.php @@ -59,30 +59,30 @@ class PdoCaster public static function castPdo(\PDO $c, array $a, Stub $stub, $isNested) { - $a = array(); + $attr = array(); $errmode = $c->getAttribute(\PDO::ATTR_ERRMODE); $c->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); - foreach (self::$pdoAttributes as $attr => $values) { - if (!isset($attr[0])) { - $attr = $values; - $values = array(); + foreach (self::$pdoAttributes as $k => $v) { + if (!isset($k[0])) { + $k = $v; + $v = array(); } try { - $a[$attr] = 'ERRMODE' === $attr ? $errmode : $c->getAttribute(constant("PDO::ATTR_{$attr}")); - if ($values && isset($values[$a[$attr]])) { - $a[$attr] = new ConstStub($values[$a[$attr]], $a[$attr]); + $attr[$k] = 'ERRMODE' === $k ? $errmode : $c->getAttribute(constant('PDO::ATTR_'.$k)); + if ($v && isset($v[$attr[$k]])) { + $attr[$k] = new ConstStub($v[$attr[$k]], $attr[$k]); } - } catch (\Exception $m) { + } catch (\Exception $e) { } } $m = "\0~\0"; - $a = (array) $c + array( + $a += array( $m.'inTransaction' => method_exists($c, 'inTransaction'), $m.'errorInfo' => $c->errorInfo(), - $m.'attributes' => $a, + $m.'attributes' => $attr, ); if ($a[$m.'inTransaction']) { diff --git a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php index 63e683cb15..5c615bb7c7 100644 --- a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php +++ b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php @@ -267,7 +267,7 @@ abstract class AbstractCloner implements ClonerInterface $a = $cast; } } catch (\Exception $e) { - $a["\0~\0⚠"] = new ThrowingCasterException($callback, $e); + $a[(Stub::TYPE_OBJECT === $stub->type ? "\0~\0" : '').'⚠'] = new ThrowingCasterException($callback, $e); } return $a; diff --git a/src/Symfony/Component/VarDumper/Exception/ThrowingCasterException.php b/src/Symfony/Component/VarDumper/Exception/ThrowingCasterException.php index 1959ac8985..1ab2f26ed7 100644 --- a/src/Symfony/Component/VarDumper/Exception/ThrowingCasterException.php +++ b/src/Symfony/Component/VarDumper/Exception/ThrowingCasterException.php @@ -16,21 +16,12 @@ namespace Symfony\Component\VarDumper\Exception; */ class ThrowingCasterException extends \Exception { - private $caster; - /** * @param callable $caster The failing caster * @param \Exception $prev The exception thrown from the caster */ public function __construct($caster, \Exception $prev) { - if (is_array($caster)) { - if (isset($caster[0]) && is_object($caster[0])) { - $caster[0] = get_class($caster[0]); - } - $caster = implode('::', $caster); - } - $this->caster = $caster; - parent::__construct(null, 0, $prev); + parent::__construct('Unexpected exception thrown from a caster: '.get_class($prev), 0, $prev); } } diff --git a/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php b/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php index bba30256b4..67e51db456 100644 --- a/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php +++ b/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php @@ -102,6 +102,58 @@ array:25 [ b"bin-key-é" => "" ] +EOTXT + , + $out + ); + } + + public function testThrowingCaster() + { + $out = fopen('php://memory', 'r+b'); + + $dumper = new CliDumper(); + $dumper->setColors(false); + $cloner = new VarCloner(); + $cloner->addCasters(array( + ':stream' => function () { + throw new \Exception('Foobar'); + }, + )); + $line = __LINE__ - 3; + $file = __FILE__; + $ref = (int) $out; + + $data = $cloner->cloneVar($out); + $dumper->dump($data, $out); + rewind($out); + $out = stream_get_contents($out); + + $this->assertStringMatchesFormat( + << array:2 [ + "call" => "%s{closure}()" + "file" => "{$file}:{$line}" + ] + ] + } +} + EOTXT , $out