From 10943d9c6da1e8daa4d62abe2d57d542b6ef5dc4 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Fri, 26 Sep 2014 09:07:47 +0200 Subject: [PATCH] [VarDumper] edge case fixes --- .../DependencyInjection/Configuration.php | 3 +- .../Resources/views/Profiler/dump.html.twig | 4 +-- .../Debug/Resources/ext/{ext => }/config.m4 | 0 .../Debug/Resources/ext/{ext => }/config.w32 | 0 .../ext/{ext => }/php_symfony_debug.h | 0 .../Resources/ext/{ext => }/symfony_debug.c | 0 .../Resources/ext/{ext => }/tests/001.phpt | 0 .../DataCollector/DumpDataCollector.php | 3 -- .../Component/VarDumper/Caster/CasterStub.php | 12 +++---- .../VarDumper/Cloner/AbstractCloner.php | 8 ++--- .../Component/VarDumper/Cloner/Data.php | 4 +-- .../Component/VarDumper/Cloner/ExtCloner.php | 34 +++++++++++-------- .../Component/VarDumper/Cloner/PhpCloner.php | 34 +++++++++++-------- .../VarDumper/Tests/CliDumperTest.php | 3 +- .../VarDumper/Tests/HtmlDumperTest.php | 3 +- 15 files changed, 58 insertions(+), 50 deletions(-) rename src/Symfony/Component/Debug/Resources/ext/{ext => }/config.m4 (100%) rename src/Symfony/Component/Debug/Resources/ext/{ext => }/config.w32 (100%) rename src/Symfony/Component/Debug/Resources/ext/{ext => }/php_symfony_debug.h (100%) rename src/Symfony/Component/Debug/Resources/ext/{ext => }/symfony_debug.c (100%) rename src/Symfony/Component/Debug/Resources/ext/{ext => }/tests/001.phpt (100%) diff --git a/src/Symfony/Bundle/DebugBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/DebugBundle/DependencyInjection/Configuration.php index 176211ad70..223bc5b299 100644 --- a/src/Symfony/Bundle/DebugBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/DebugBundle/DependencyInjection/Configuration.php @@ -41,7 +41,8 @@ class Configuration implements ConfigurationInterface ->min(-1) ->defaultValue(-1) ->end() - ->end(); + ->end() + ; return $treeBuilder; } diff --git a/src/Symfony/Bundle/DebugBundle/Resources/views/Profiler/dump.html.twig b/src/Symfony/Bundle/DebugBundle/Resources/views/Profiler/dump.html.twig index 4c650b13d1..51796195b7 100644 --- a/src/Symfony/Bundle/DebugBundle/Resources/views/Profiler/dump.html.twig +++ b/src/Symfony/Bundle/DebugBundle/Resources/views/Profiler/dump.html.twig @@ -18,7 +18,7 @@
in {% if dump.file %} - {% set link = dump.file|file_link(dump.line) }} %} + {% set link = dump.file|file_link(dump.line) %} {% if link %} {{ dump.name }} {% else %} @@ -76,7 +76,7 @@
  • in {% if dump.line %} - {% set link = dump.file|file_link(dump.line) }} %} + {% set link = dump.file|file_link(dump.line) %} {% if link %} {{ dump.name }} {% else %} diff --git a/src/Symfony/Component/Debug/Resources/ext/ext/config.m4 b/src/Symfony/Component/Debug/Resources/ext/config.m4 similarity index 100% rename from src/Symfony/Component/Debug/Resources/ext/ext/config.m4 rename to src/Symfony/Component/Debug/Resources/ext/config.m4 diff --git a/src/Symfony/Component/Debug/Resources/ext/ext/config.w32 b/src/Symfony/Component/Debug/Resources/ext/config.w32 similarity index 100% rename from src/Symfony/Component/Debug/Resources/ext/ext/config.w32 rename to src/Symfony/Component/Debug/Resources/ext/config.w32 diff --git a/src/Symfony/Component/Debug/Resources/ext/ext/php_symfony_debug.h b/src/Symfony/Component/Debug/Resources/ext/php_symfony_debug.h similarity index 100% rename from src/Symfony/Component/Debug/Resources/ext/ext/php_symfony_debug.h rename to src/Symfony/Component/Debug/Resources/ext/php_symfony_debug.h diff --git a/src/Symfony/Component/Debug/Resources/ext/ext/symfony_debug.c b/src/Symfony/Component/Debug/Resources/ext/symfony_debug.c similarity index 100% rename from src/Symfony/Component/Debug/Resources/ext/ext/symfony_debug.c rename to src/Symfony/Component/Debug/Resources/ext/symfony_debug.c diff --git a/src/Symfony/Component/Debug/Resources/ext/ext/tests/001.phpt b/src/Symfony/Component/Debug/Resources/ext/tests/001.phpt similarity index 100% rename from src/Symfony/Component/Debug/Resources/ext/ext/tests/001.phpt rename to src/Symfony/Component/Debug/Resources/ext/tests/001.phpt diff --git a/src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php b/src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php index b54a90237a..ba19540585 100644 --- a/src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php +++ b/src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php @@ -15,7 +15,6 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Stopwatch\Stopwatch; use Symfony\Component\VarDumper\Cloner\Data; -use Symfony\Component\VarDumper\Dumper\JsonDumper; use Symfony\Component\VarDumper\Dumper\CliDumper; use Symfony\Component\VarDumper\Dumper\HtmlDumper; use Symfony\Component\VarDumper\Dumper\DataDumperInterface; @@ -158,8 +157,6 @@ class DumpDataCollector extends DataCollector implements DataDumperInterface { if ('html' === $format) { $dumper = new HtmlDumper(); - } elseif ('json' === $format) { - $dumper = new JsonDumper(); } else { throw new \InvalidArgumentException(sprintf('Invalid dump format: %s', $format)); } diff --git a/src/Symfony/Component/VarDumper/Caster/CasterStub.php b/src/Symfony/Component/VarDumper/Caster/CasterStub.php index aaf8ed1035..56fc2b112e 100644 --- a/src/Symfony/Component/VarDumper/Caster/CasterStub.php +++ b/src/Symfony/Component/VarDumper/Caster/CasterStub.php @@ -22,11 +22,13 @@ class CasterStub extends Stub { public function __construct($value, $class = '') { + $this->class = $class; + $this->value = $value; + switch (gettype($value)) { case 'object': $this->type = self::TYPE_OBJECT; $this->class = get_class($value); - $this->value = spl_object_hash($value); $this->cut = -1; break; @@ -40,7 +42,6 @@ class CasterStub extends Stub case 'unknown type': $this->type = self::TYPE_RESOURCE; $this->class = @get_resource_type($value); - $this->value = (int) $value; $this->cut = -1; break; @@ -49,13 +50,8 @@ class CasterStub extends Stub $this->type = self::TYPE_STRING; $this->class = preg_match('//u', $value) ? self::STRING_UTF8 : self::STRING_BINARY; $this->cut = self::STRING_BINARY === $this->class ? strlen($value) : (function_exists('iconv_strlen') ? iconv_strlen($value, 'UTF-8') : -1); - break; + $this->value = ''; } - // No break; - - default: - $this->class = $class; - $this->value = $value; break; } } diff --git a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php index ef94af979e..f8983ddbb4 100644 --- a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php +++ b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php @@ -174,14 +174,14 @@ abstract class AbstractCloner implements ClonerInterface /** * Casts an object to an array representation. * - * @param object $obj The object itself. * @param Stub $stub The Stub for the casted object. * @param bool $isNested True if the object is nested in the dumped structure. * * @return array The object casted as array. */ - protected function castObject($obj, Stub $stub, $isNested) + protected function castObject(Stub $stub, $isNested) { + $obj = $stub->value; $class = $stub->class; if (isset($this->classInfo[$class])) { @@ -225,15 +225,15 @@ abstract class AbstractCloner implements ClonerInterface /** * Casts a resource to an array representation. * - * @param resource $res The resource. * @param Stub $stub The Stub for the casted resource. * @param bool $isNested True if the object is nested in the dumped structure. * * @return array The resource casted as array. */ - protected function castResource($res, Stub $stub, $isNested) + protected function castResource(Stub $stub, $isNested) { $a = array(); + $res = $stub->value; $type = $stub->class; if (!empty($this->casters[':'.$type])) { diff --git a/src/Symfony/Component/VarDumper/Cloner/Data.php b/src/Symfony/Component/VarDumper/Cloner/Data.php index 3394cf796f..077e19b487 100644 --- a/src/Symfony/Component/VarDumper/Cloner/Data.php +++ b/src/Symfony/Component/VarDumper/Cloner/Data.php @@ -200,8 +200,8 @@ class Data */ public static function utf8Encode($s) { - if (function_exists('iconv')) { - return iconv('CP1252', 'UTF-8', $s); + if (function_exists('mb_convert_encoding')) { + return mb_convert_encoding($s, 'UTF-8', 'CP1252'); } $s .= $s; diff --git a/src/Symfony/Component/VarDumper/Cloner/ExtCloner.php b/src/Symfony/Component/VarDumper/Cloner/ExtCloner.php index 9aefcc9f4a..cf1a0d19ea 100644 --- a/src/Symfony/Component/VarDumper/Cloner/ExtCloner.php +++ b/src/Symfony/Component/VarDumper/Cloner/ExtCloner.php @@ -95,16 +95,18 @@ class ExtCloner extends AbstractCloner $stub = new Stub(); $stub->type = Stub::TYPE_OBJECT; $stub->class = $zval['object_class']; - $stub->value = $h; - $a = $this->castObject($v, $stub, 0 < $i); - if (Stub::TYPE_OBJECT !== $stub->type) { - break; + $stub->value = $v; + $a = $this->castObject($stub, 0 < $i); + if ($v !== $stub->value) { + if (Stub::TYPE_OBJECT !== $stub->type) { + break; + } + $h = spl_object_hash($stub->value); } - $h = $stub->value; - $stub->value = ''; + $stub->value = null; if (0 <= $maxItems && $maxItems <= $pos) { $stub->cut = count($a); - $a = array(); + $a = null; } } if (empty($softRefs[$h])) { @@ -112,6 +114,7 @@ class ExtCloner extends AbstractCloner } else { $stub = $softRefs[$h]; $stub->ref = ++$refs; + $a = null; } break; @@ -120,16 +123,18 @@ class ExtCloner extends AbstractCloner $stub = new Stub(); $stub->type = Stub::TYPE_RESOURCE; $stub->class = $zval['resource_type']; - $stub->value = $h; - $a = $this->castResource($v, $stub, 0 < $i); - if (Stub::TYPE_RESOURCE !== $stub->type) { - break; + $stub->value = $v; + $a = $this->castResource($stub, 0 < $i); + if ($v !== $stub->value) { + if (Stub::TYPE_RESOURCE !== $stub->type) { + break; + } + $h = (int) $stub->value; } - $h = $stub->value; - $stub->value = ''; + $stub->value = null; if (0 <= $maxItems && $maxItems <= $pos) { $stub->cut = count($a); - $a = array(); + $a = null; } } if (empty($softRefs[$h])) { @@ -137,6 +142,7 @@ class ExtCloner extends AbstractCloner } else { $stub = $softRefs[$h]; $stub->ref = ++$refs; + $a = null; } break; } diff --git a/src/Symfony/Component/VarDumper/Cloner/PhpCloner.php b/src/Symfony/Component/VarDumper/Cloner/PhpCloner.php index 46fa6b0f0c..3fdafff667 100644 --- a/src/Symfony/Component/VarDumper/Cloner/PhpCloner.php +++ b/src/Symfony/Component/VarDumper/Cloner/PhpCloner.php @@ -101,16 +101,18 @@ class PhpCloner extends AbstractCloner $stub = new Stub(); $stub->type = Stub::TYPE_OBJECT; $stub->class = get_class($v); - $stub->value = $h; - $a = $this->castObject($v, $stub, 0 < $i); - if (Stub::TYPE_OBJECT !== $stub->type) { - break; + $stub->value = $v; + $a = $this->castObject($stub, 0 < $i); + if ($v !== $stub->value) { + if (Stub::TYPE_OBJECT !== $stub->type) { + break; + } + $h = spl_object_hash($stub->value); } - $h = $stub->value; - $stub->value = ''; + $stub->value = null; if (0 <= $maxItems && $maxItems <= $pos) { $stub->cut = count($a); - $a = array(); + $a = null; } } if (empty($softRefs[$h])) { @@ -118,6 +120,7 @@ class PhpCloner extends AbstractCloner } else { $stub = $softRefs[$h]; $stub->ref = ++$refs; + $a = null; } break; @@ -127,16 +130,18 @@ class PhpCloner extends AbstractCloner $stub = new Stub(); $stub->type = Stub::TYPE_RESOURCE; $stub->class = get_resource_type($v); - $stub->value = $h; - $a = $this->castResource($v, $stub, 0 < $i); - if (Stub::TYPE_RESOURCE !== $stub->type) { - break; + $stub->value = $v; + $a = $this->castResource($stub, 0 < $i); + if ($v !== $stub->value) { + if (Stub::TYPE_RESOURCE !== $stub->type) { + break; + } + $h = (int) $stub->value; } - $h = $stub->value; - $stub->value = ''; + $stub->value = null; if (0 <= $maxItems && $maxItems <= $pos) { $stub->cut = count($a); - $a = array(); + $a = null; } } if (empty($softRefs[$h])) { @@ -144,6 +149,7 @@ class PhpCloner extends AbstractCloner } else { $stub = $softRefs[$h]; $stub->ref = ++$refs; + $a = null; } break; } diff --git a/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php b/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php index 6a4e9c3f88..17d4ef84eb 100644 --- a/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php +++ b/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php @@ -40,6 +40,7 @@ class CliDumperTest extends \PHPUnit_Framework_TestCase $out = ob_get_clean(); $closureLabel = PHP_VERSION_ID >= 50400 ? 'public method' : 'function'; $out = preg_replace('/[ \t]+$/m', '', $out); + $intMax = PHP_INT_MAX; $this->assertSame( << NAN 4 => INF 5 => -INF - 6 => 9223372036854775807 + 6 => {$intMax} "str" => "déjà" 7 => b"é" "[]" => [] diff --git a/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php b/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php index 25b027d62e..e93ff236c2 100644 --- a/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php +++ b/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php @@ -43,6 +43,7 @@ class HtmlDumperTest extends \PHPUnit_Framework_TestCase $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; $this->assertSame( <<3 => NAN 4 => INF 5 => -INF - 6 => 9223372036854775807 + 6 => {$intMax} "str" => "déjà" 7 => b"é" "[]" => []