Skip to content

Commit

Permalink
[VarDumper] Towards PHP7 support
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Apr 4, 2015
1 parent 7adba99 commit df484da
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 27 deletions.
32 changes: 15 additions & 17 deletions src/Symfony/Component/VarDumper/Caster/ExceptionCaster.php
Expand Up @@ -42,18 +42,19 @@ class ExceptionCaster

public static function castException(\Exception $e, array $a, Stub $stub, $isNested)
{
$trace = $a["\0Exception\0trace"];
unset($a["\0Exception\0trace"]); // Ensures the trace is always last
$xPrefix = PHP_VERSION_ID >= 70000 ? "\0BaseException\0" : "\0Exception\0";
$trace = $a[$xPrefix.'trace'];
unset($a[$xPrefix.'trace']); // Ensures the trace is always last

static::filterTrace($trace, static::$traceArgs);

if (null !== $trace) {
$a["\0Exception\0trace"] = $trace;
$a[$xPrefix.'trace'] = $trace;
}
if (empty($a["\0Exception\0previous"])) {
unset($a["\0Exception\0previous"]);
if (empty($a[$xPrefix.'previous'])) {
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;
}
Expand All @@ -69,23 +70,20 @@ public static function castErrorException(\ErrorException $e, array $a, Stub $st

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"])) {
$a["\0~\0message"] = $b["\0*\0message"];
}

if (isset($a["\0Exception\0trace"])) {
$b["\0Exception\0trace"][0] += array(
if (isset($a[$xPrefix.'trace'][0])) {
$b[$xPrefix.'trace'][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"];
array_splice($b[$xPrefix.'trace'], -1 - count($a[$xPrefix.'trace']));
static::filterTrace($b[$xPrefix.'trace'], false);
$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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/VarDumper/Cloner/VarCloner.php
Expand Up @@ -119,12 +119,10 @@ protected function doClone($var)
$stub = $arrayRefs[$len] = new Stub();
$stub->type = Stub::TYPE_ARRAY;
$stub->class = Stub::ARRAY_ASSOC;
$stub->value = $zval['array_count'] ?: count($v);

$a = $v;

// Copies of $GLOBALS have very strange behavior,
// let's detect them with some black magic
$a = $v;
$a[$gid] = true;

// Happens with copies of $GLOBALS
Expand All @@ -137,6 +135,8 @@ protected function doClone($var)
} else {
$a = $v;
}

$stub->value = $zval['array_count'] ?: count($a);
}
break;

Expand Down
Expand Up @@ -22,6 +22,6 @@ class ThrowingCasterException extends \Exception
*/
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);
}
}
6 changes: 2 additions & 4 deletions src/Symfony/Component/VarDumper/Tests/CliDumperTest.php
Expand Up @@ -38,7 +38,6 @@ public function testGet()
ob_start();
$dumper->dump($data);
$out = ob_get_clean();
$closureLabel = PHP_VERSION_ID >= 50400 ? 'public method' : 'function';
$out = preg_replace('/[ \t]+$/m', '', $out);
$intMax = PHP_INT_MAX;
$res1 = (int) $var['res'];
Expand Down Expand Up @@ -77,7 +76,7 @@ public function testGet()
}
"closure" => Closure {#%d
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']}
- Parameters [2] {
Expand Down Expand Up @@ -143,8 +142,7 @@ public function testThrowingCaster()
eof: false
options: []
⚠: Symfony\Component\VarDumper\Exception\ThrowingCasterException {#%d
#message: "Unexpected exception thrown from a caster: Exception"
message: "Foobar"
#message: "Unexpected Exception thrown from a caster: Foobar"
trace: array:1 [
0 => array:2 [
"call" => "%s{closure}()"
Expand Down
3 changes: 1 addition & 2 deletions src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php
Expand Up @@ -39,7 +39,6 @@ public function testGet()
ob_start();
$dumper->dump($data);
$out = ob_get_clean();
$closureLabel = PHP_VERSION_ID >= 50400 ? 'public method' : 'function';
$out = preg_replace('/[ \t]+$/m', '', $out);
$var['file'] = htmlspecialchars($var['file'], ENT_QUOTES, 'UTF-8');
$intMax = PHP_INT_MAX;
Expand Down Expand Up @@ -81,7 +80,7 @@ public function testGet()
</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-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"> - Parameters [2] {</span>
Expand Down

0 comments on commit df484da

Please sign in to comment.