Skip to content

[VarDumper] fix very special vars handling #13351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testDump()
$this->assertSame($xDump, $dump);

$this->assertStringStartsWith(
'a:1:{i:0;a:5:{s:4:"data";O:39:"Symfony\Component\VarDumper\Cloner\Data":4:{s:45:"Symfony\Component\VarDumper\Cloner\Datadata";a:1:{i:0;a:1:{i:0;i:123;}}s:49:"Symfony\Component\VarDumper\Cloner\DatamaxDepth";i:-1;s:57:"Symfony\Component\VarDumper\Cloner\DatamaxItemsPerDepth";i:-1;s:54:"Symfony\Component\VarDumper\Cloner\DatauseRefHandles";i:-1;}s:4:"name";s:25:"DumpDataCollectorTest.php";s:4:"file";s:',
'a:1:{i:0;a:5:{s:4:"data";O:39:"Symfony\Component\VarDumper\Cloner\Data":4:{s:45:"Symfony\Component\VarDumper\Cloner\Datadata";a:1:{i:0;a:1:{i:0;i:123;}}s:49:"Symfony\Component\VarDumper\Cloner\DatamaxDepth";i:20;s:57:"Symfony\Component\VarDumper\Cloner\DatamaxItemsPerDepth";i:-1;s:54:"Symfony\Component\VarDumper\Cloner\DatauseRefHandles";i:-1;}s:4:"name";s:25:"DumpDataCollectorTest.php";s:4:"file";s:',
str_replace("\0", '', $collector->serialize())
);

Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ abstract class AbstractCloner implements ClonerInterface

protected $maxItems = 2500;
protected $maxString = -1;
protected $useExt;

private $casters = array();
private $prevErrorHandler;
Expand All @@ -98,6 +99,7 @@ public function __construct(array $casters = null)
$casters = static::$defaultCasters;
}
$this->addCasters($casters);
$this->useExt = extension_loaded('symfony_debug');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/VarDumper/Cloner/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class Data
{
private $data;
private $maxDepth = -1;
private $maxDepth = 20;
private $maxItemsPerDepth = -1;
private $useRefHandles = -1;

Expand Down
14 changes: 13 additions & 1 deletion src/Symfony/Component/VarDumper/Cloner/VarCloner.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class VarCloner extends AbstractCloner
*/
protected function doClone($var)
{
$useExt = extension_loaded('symfony_debug');
$useExt = $this->useExt;
$i = 0; // Current iteration position in $queue
$len = 1; // Length of $queue
$pos = 0; // Number of cloned items past the first level
Expand Down Expand Up @@ -120,7 +120,19 @@ protected function doClone($var)
$stub->type = Stub::TYPE_ARRAY;
$stub->class = Stub::ARRAY_ASSOC;
$stub->value = $zval['array_count'] ?: count($v);

$a = $v;
$a[] = null;
$h = count($v);
array_pop($a);

// Happens with copies of $GLOBALS
if ($h !== $stub->value) {
$a = array();
foreach ($v as $gk => &$gv) {
$a[$gk] =& $gv;
}
}
}
break;

Expand Down
145 changes: 145 additions & 0 deletions src/Symfony/Component/VarDumper/Tests/CliDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,153 @@ public function testGet()

EOTXT
,
$out
);
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testSpecialVars56()
{
if (PHP_VERSION_ID < 50600) {
$this->markTestSkipped('PHP 5.6 is required');
}

$var = $this->getSpecialVars();

$dumper = new CliDumper();
$dumper->setColors(false);
$cloner = new VarCloner();

$data = $cloner->cloneVar($var);
$out = fopen('php://memory', 'r+b');
$dumper->dump($data, $out);
rewind($out);
$out = stream_get_contents($out);

$this->assertSame(
<<<EOTXT
array:2 [
0 => array:1 [
0 => &1 array:1 [
0 => &1 array:1 [&1]
]
]
1 => array:1 [
"GLOBALS" => &2 array:1 [
"GLOBALS" => &2 array:1 [&2]
]
]
]

EOTXT
,
$out
);
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testGlobalsNoExt()
{
$var = $this->getSpecialVars();
unset($var[0]);
$out = '';

$dumper = new CliDumper(function ($line, $depth) use (&$out) {
if ($depth >= 0) {
$out .= str_repeat(' ', $depth).$line."\n";
}
});
$dumper->setColors(false);
$cloner = new VarCloner();

$refl = new \ReflectionProperty($cloner, 'useExt');
$refl->setAccessible(true);
$refl->setValue($cloner, false);

$data = $cloner->cloneVar($var);
$dumper->dump($data);

$this->assertSame(
<<<EOTXT
array:2 [
1 => array:1 [
"GLOBALS" => &1 array:1 [
"GLOBALS" => &1 array:1 [&1]
]
]
2 => &1 array:1 [&1]
]

EOTXT
,
$out
);
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testBuggyRefs()
{
if (PHP_VERSION_ID >= 50600) {
$this->markTestSkipped('PHP 5.6 fixed refs counting');
}

$var = $this->getSpecialVars();
$var = $var[0];

$dumper = new CliDumper();
$dumper->setColors(false);
$cloner = new VarCloner();

$data = $cloner->cloneVar($var)->getLimitedClone(3, -1);
$out = '';
$dumper->dump($data, function ($line, $depth) use (&$out) {
if ($depth >= 0) {
$out .= str_repeat(' ', $depth).$line."\n";
}
});

$this->assertSame(
<<<EOTXT
array:1 [
0 => array:1 [
0 => array:1 [
0 => array:1 [
…1
]
]
]
]

EOTXT
,
$out
);
}

private function getSpecialVars()
{
foreach (array_keys($GLOBALS) as $var) {
if ('GLOBALS' !== $var) {
unset($GLOBALS[$var]);
}
}

$var = function &() {
$var = array();
$var[] =& $var;

return $var;
};

return array($var(), $GLOBALS, &$GLOBALS);
}
}