[VarDumper] Towards PHP7 support

This commit is contained in:
Nicolas Grekas 2015-04-03 19:35:00 +02:00
parent 7adba99975
commit df484dadfc
5 changed files with 22 additions and 27 deletions

View File

@ -42,18 +42,19 @@ class ExceptionCaster
public static function castException(\Exception $e, array $a, Stub $stub, $isNested) public static function castException(\Exception $e, array $a, Stub $stub, $isNested)
{ {
$trace = $a["\0Exception\0trace"]; $xPrefix = PHP_VERSION_ID >= 70000 ? "\0BaseException\0" : "\0Exception\0";
unset($a["\0Exception\0trace"]); // Ensures the trace is always last $trace = $a[$xPrefix.'trace'];
unset($a[$xPrefix.'trace']); // Ensures the trace is always last
static::filterTrace($trace, static::$traceArgs); static::filterTrace($trace, static::$traceArgs);
if (null !== $trace) { if (null !== $trace) {
$a["\0Exception\0trace"] = $trace; $a[$xPrefix.'trace'] = $trace;
} }
if (empty($a["\0Exception\0previous"])) { if (empty($a[$xPrefix.'previous'])) {
unset($a["\0Exception\0previous"]); unset($a[$xPrefix.'previous']);
} }
unset($a["\0Exception\0string"], $a["\0+\0xdebug_message"], $a["\0+\0__destructorException"]); unset($a[$xPrefix.'string'], $a["\0+\0xdebug_message"], $a["\0+\0__destructorException"]);
return $a; return $a;
} }
@ -69,23 +70,20 @@ class ExceptionCaster
public static function castThrowingCasterException(ThrowingCasterException $e, array $a, Stub $stub, $isNested) public static function castThrowingCasterException(ThrowingCasterException $e, array $a, Stub $stub, $isNested)
{ {
$b = (array) $a["\0Exception\0previous"]; $xPrefix = PHP_VERSION_ID >= 70000 ? "\0BaseException\0" : "\0Exception\0";
$b = (array) $a[$xPrefix.'previous'];
if (isset($b["\0*\0message"])) { if (isset($a[$xPrefix.'trace'][0])) {
$a["\0~\0message"] = $b["\0*\0message"]; $b[$xPrefix.'trace'][0] += array(
}
if (isset($a["\0Exception\0trace"])) {
$b["\0Exception\0trace"][0] += array(
'file' => $b["\0*\0file"], 'file' => $b["\0*\0file"],
'line' => $b["\0*\0line"], 'line' => $b["\0*\0line"],
); );
array_splice($b["\0Exception\0trace"], -1 - count($a["\0Exception\0trace"])); array_splice($b[$xPrefix.'trace'], -1 - count($a[$xPrefix.'trace']));
static::filterTrace($b["\0Exception\0trace"], false); static::filterTrace($b[$xPrefix.'trace'], false);
$a["\0~\0trace"] = $b["\0Exception\0trace"]; $a["\0~\0trace"] = $b[$xPrefix.'trace'];
} }
unset($a["\0Exception\0trace"], $a["\0Exception\0previous"], $a["\0*\0code"], $a["\0*\0file"], $a["\0*\0line"]); unset($a[$xPrefix.'trace'], $a[$xPrefix.'previous'], $a["\0*\0code"], $a["\0*\0file"], $a["\0*\0line"]);
return $a; return $a;
} }

View File

@ -119,12 +119,10 @@ class VarCloner extends AbstractCloner
$stub = $arrayRefs[$len] = new Stub(); $stub = $arrayRefs[$len] = new Stub();
$stub->type = Stub::TYPE_ARRAY; $stub->type = Stub::TYPE_ARRAY;
$stub->class = Stub::ARRAY_ASSOC; $stub->class = Stub::ARRAY_ASSOC;
$stub->value = $zval['array_count'] ?: count($v);
$a = $v;
// Copies of $GLOBALS have very strange behavior, // Copies of $GLOBALS have very strange behavior,
// let's detect them with some black magic // let's detect them with some black magic
$a = $v;
$a[$gid] = true; $a[$gid] = true;
// Happens with copies of $GLOBALS // Happens with copies of $GLOBALS
@ -137,6 +135,8 @@ class VarCloner extends AbstractCloner
} else { } else {
$a = $v; $a = $v;
} }
$stub->value = $zval['array_count'] ?: count($a);
} }
break; break;

View File

@ -22,6 +22,6 @@ class ThrowingCasterException extends \Exception
*/ */
public function __construct($caster, \Exception $prev) public function __construct($caster, \Exception $prev)
{ {
parent::__construct('Unexpected exception thrown from a caster: '.get_class($prev), 0, $prev); parent::__construct('Unexpected '.get_class($prev).' thrown from a caster: '.$prev->getMessage(), 0, $prev);
} }
} }

View File

@ -38,7 +38,6 @@ class CliDumperTest extends \PHPUnit_Framework_TestCase
ob_start(); ob_start();
$dumper->dump($data); $dumper->dump($data);
$out = ob_get_clean(); $out = ob_get_clean();
$closureLabel = PHP_VERSION_ID >= 50400 ? 'public method' : 'function';
$out = preg_replace('/[ \t]+$/m', '', $out); $out = preg_replace('/[ \t]+$/m', '', $out);
$intMax = PHP_INT_MAX; $intMax = PHP_INT_MAX;
$res1 = (int) $var['res']; $res1 = (int) $var['res'];
@ -77,7 +76,7 @@ array:25 [
} }
"closure" => Closure {#%d "closure" => Closure {#%d
reflection: """ reflection: """
Closure [ <user> {$closureLabel} Symfony\Component\VarDumper\Tests\Fixture\{closure} ] { Closure [ <user%S> %s Symfony\Component\VarDumper\Tests\Fixture\{closure} ] {
@@ {$var['file']} {$var['line']} - {$var['line']} @@ {$var['file']} {$var['line']} - {$var['line']}
- Parameters [2] { - Parameters [2] {
@ -143,8 +142,7 @@ EOTXT
eof: false eof: false
options: [] options: []
: Symfony\Component\VarDumper\Exception\ThrowingCasterException {#%d : Symfony\Component\VarDumper\Exception\ThrowingCasterException {#%d
#message: "Unexpected exception thrown from a caster: Exception" #message: "Unexpected Exception thrown from a caster: Foobar"
message: "Foobar"
trace: array:1 [ trace: array:1 [
0 => array:2 [ 0 => array:2 [
"call" => "%s{closure}()" "call" => "%s{closure}()"

View File

@ -39,7 +39,6 @@ class HtmlDumperTest extends \PHPUnit_Framework_TestCase
ob_start(); ob_start();
$dumper->dump($data); $dumper->dump($data);
$out = ob_get_clean(); $out = ob_get_clean();
$closureLabel = PHP_VERSION_ID >= 50400 ? 'public method' : 'function';
$out = preg_replace('/[ \t]+$/m', '', $out); $out = preg_replace('/[ \t]+$/m', '', $out);
$var['file'] = htmlspecialchars($var['file'], ENT_QUOTES, 'UTF-8'); $var['file'] = htmlspecialchars($var['file'], ENT_QUOTES, 'UTF-8');
$intMax = PHP_INT_MAX; $intMax = PHP_INT_MAX;
@ -81,7 +80,7 @@ class HtmlDumperTest extends \PHPUnit_Framework_TestCase
</samp>} </samp>}
"<span class=sf-dump-key>closure</span>" => <span class=sf-dump-note>Closure</span> {<a class=sf-dump-ref>#%d</a><samp> "<span class=sf-dump-key>closure</span>" => <span class=sf-dump-note>Closure</span> {<a class=sf-dump-ref>#%d</a><samp>
<span class=sf-dump-meta>reflection</span>: """ <span class=sf-dump-meta>reflection</span>: """
<span class=sf-dump-str title="%d characters">Closure [ &lt;user&gt; {$closureLabel} Symfony\Component\VarDumper\Tests\Fixture\{closure} ] {</span> <span class=sf-dump-str title="%d characters">Closure [ &lt;user%S&gt; %s Symfony\Component\VarDumper\Tests\Fixture\{closure} ] {</span>
<span class=sf-dump-str title="%d characters"> @@ {$var['file']} {$var['line']} - {$var['line']}</span> <span class=sf-dump-str title="%d characters"> @@ {$var['file']} {$var['line']} - {$var['line']}</span>
<span class=sf-dump-str title="%d characters"> - Parameters [2] {</span> <span class=sf-dump-str title="%d characters"> - Parameters [2] {</span>