8000 [VarDumper] Add EnumStub for dumping virtual collections with casters · symfony/symfony@e1af8cc · GitHub
[go: up one dir, main page]

Skip to content

Commit e1af8cc

Browse files
[VarDumper] Add EnumStub for dumping virtual collections with casters
1 parent bcb2ff6 commit e1af8cc

11 files changed

+114
-33
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Caster;
13+
14+
use Symfony\Component\VarDumper\Cloner\Stub;
15+
16+
/**
17+
* Represents an enumeration of values.
18+
*
19+
* @author Nicolas Grekas <p@tchwork.com>
20+
*/
21+
class EnumStub extends Stub
22+
{
23+
public function __construct(array $values)
24+
{
25+
$this->value = $values;
26+
}
27+
}

src/Symfony/Component/VarDumper/Caster/PdoCaster.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static function castPdo(\PDO $c, array $a, Stub $stub, $isNested)
8282
$a += array(
8383
$prefix.'inTransaction' => method_exists($c, 'inTransaction'),
8484
$prefix.'errorInfo' => $c->errorInfo(),
85-
$prefix.'attributes' => $attr,
85+
$prefix.'attributes' => new EnumStub($attr),
8686
);
8787

8888
if ($a[$prefix.'inTransaction']) {

src/Symfony/Component/VarDumper/Caster/PgSqlCaster.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public static function castLink($link, array $a, Stub $stub, $isNested)
101101
}
102102

103103
$a['param']['client_encoding'] = pg_client_encoding($link);
104+
$a['param'] = new EnumStub($a['param']);
104105

105106
return $a;
106107
}
@@ -145,7 +146,7 @@ public static function castResult($result, array $a, Stub $stub, $isNested)
145146
if ('1 chars' === $field['display']) {
146147
$field['display'] = '1 char';
147148
}
148-
$a['fields'][] = $field;
149+
$a['fields'][] = new EnumStub($field);
149150
}
150151

151152
return $a;

src/Symfony/Component/VarDumper/Caster/ReflectionCaster.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ public static function castClosure(\Closure $c, array $a, Stub $stub, $isNested)
5151
$a = static::castFunctionAbstract($c, $a, $stub, $isNested);
5252

5353
if (isset($a[$prefix.'parameters'])) {
54-
foreach ($a[$prefix.'parameters'] as &$v) {
54+
foreach ($a[$prefix.'parameters']->value as &$v) {
5555
$param = $v;
56-
$v = array();
56+
$v = new EnumStub(array());
5757
foreach (static::castParameter($param, array(), $stub, true) as $k => $param) {
5858
if ("\0" === $k[0]) {
59-
$v[substr($k, 3)] = $param;
59+
$v->value[substr($k, 3)] = $param;
6060
}
6161
}
62-
unset($v['position'], $v['isVariadic'], $v['byReference'], $v);
62+
unset($v->value['position'], $v->value['isVariadic'], $v->value['byReference'], $v);
6363
}
6464
}
6565

@@ -128,18 +128,25 @@ public static function castFunctionAbstract(\ReflectionFunctionAbstract $c, arra
128128
}
129129
$a[$prefix.'parameters'][$k] = $v;
130130
}
131+
if (isset($a[$prefix.'parameters'])) {
132+
$a[$prefix.'parameters'] = new EnumStub($a[$prefix.'parameters']);
133+
}
131134

132135
if ($v = $c->getStaticVariables()) {
133136
foreach ($v as $k => &$v) {
134137
$a[$prefix.'use']['$'.$k] = &$v;
135138
}
136139
unset($v);
140+
$a[$prefix.'use'] = new EnumStub($a[$prefix.'use']);
137141
}
138142

139143
if (!($filter & Caster::EXCLUDE_VERBOSE) && !$isNested) {
140144
self::addExtra($a, $c);
141145
}
142146

147+
// Added by HHVM
148+
unset($a[Caster::PREFIX_DYNAMIC.'static']);
149+
143150
return $a;
144151
}
145152

@@ -220,14 +227,18 @@ public static function castZendExtension(\ReflectionZendExtension $c, array $a,
220227

221228
private static function addExtra(&$a, \Reflector $c)
222229
{
223-
$a = &$a[Caster::PREFIX_VIRTUAL.'extra'];
230+
$x = isset($a[Caster::PREFIX_VIRTUAL.'extra']) ? $a[Caster::PREFIX_VIRTUAL.'extra']->value : array();
224231

225232
if (method_exists($c, 'getFileName') && $m = $c->getFileName()) {
226-
$a['file'] = $m;
227-
$a['line'] = $c->getStartLine().' to '.$c->getEndLine();
233+
$x['file'] = $m;
234+
$x['line'] = $c->getStartLine().' to '.$c->getEndLine();
228235
}
229236

230-
self::addMap($a, $c, self::$extraMap, '');
237+
self::addMap($x, $c, self::$extraMap, '');
238+
239+
if ($x) {
240+
$a[Caster::PREFIX_VIRTUAL.'extra'] = new EnumStub($x);
241+
}
231242
}
232243

233244
private static function addMap(&$a, \Reflector $c, $map, $prefix = Caster::PREFIX_VIRTUAL)

src/Symfony/Component/VarDumper/Caster/StubCaster.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,24 @@ public static function cutInternals($obj, array $a, Stub $stub, $isNested)
4848

4949
return $a;
5050
}
51+
52+
public static function castEnum(EnumStub $c, array $a, Stub $stub, $isNested)
53+
{
54+
if ($isNested) {
55+
$stub->class = '';
56+
$stub->handle = 0;
57+
58+
$a = array();
59+
60+
if ($c->value) {
61+
foreach (array_keys($c->value) as $k) {
62+
$keys[] = Caster::PREFIX_VIRTUAL.$k;
63+
}
64+
// Preserve references with array_combine()
65+
$a = array_combine($keys, $c->value);
66+
}
67+
}
68+
69+
return $a;
70+
}
5171
}

src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ abstract class AbstractCloner implements ClonerInterface
2525
'Symfony\Component\VarDumper\Caster\CutStub' => 'Symfony\Component\VarDumper\Caster\StubCaster::castStub',
2626
'Symfony\Component\VarDumper\Caster\CutArrayStub' => 'Symfony\Component\VarDumper\Caster\StubCaster::castCutArray',
2727
'Symfony\Component\VarDumper\Caster\ConstStub' => 'Symfony\Component\VarDumper\Caster\StubCaster::castStub',
28+
'Symfony\Component\VarDumper\Caster\EnumStub' => 'Symfony\Component\VarDumper\Caster\StubCaster::castEnum',
2829

2930
'Closure' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castClosure',
3031
'ReflectionClass' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castClass',

src/Symfony/Component/VarDumper/Dumper/CliDumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public function enterHash(Cursor $cursor, $type, $class, $hasChild)
247247
$class = $this->utf8Encode($class);
248248
}
249249
if (Cursor::HASH_OBJECT === $type) {
250-
$prefix = 'stdClass' !== $class ? $this->style('note', $class).' {' : '{';
250+
$prefix = $class && 'stdClass' !== $class ? $this->style('note', $class).' {' : '{';
251251
} elseif (Cursor::HASH_RESOURCE === $type) {
252252
$prefix = $this->style('note', $class.' resource').($hasChild ? ' {' : ' ');
253253
} else {

src/Symfony/Component/VarDumper/Tests/Caster/PdoCasterTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ public function testCastPdo()
2929
$pdo->setAttribute(\PDO::ATTR_STATEMENT_CLASS, array('PDOStatement', array($pdo)));
3030

3131
$cast = PdoCaster::castPdo($pdo, array(), new Stub(), false);
32-
$attr = $cast["\0~\0attributes"];
3332

33+
$this->assertInstanceOf('Symfony\Component\VarDumper\Caster\EnumStub', $cast["\0~\0attributes"]);
34+
35+
$attr = $cast["\0~\0attributes"] = $cast["\0~\0attributes"]->value;
3436
$this->assertInstanceOf('Symfony\Component\VarDumper\Caster\ConstStub', $attr['CASE']);
3537
$this->assertSame('NATURAL', $attr['CASE']->class);
3638
$this->assertSame('BOTH', $attr['DEFAULT_FETCH_MODE']->class);

src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,42 @@ public function testReflectionCaster()
3939
%A +name: "name"
4040
+class: "ReflectionClass"
4141
%A modifiers: "public"
42-
extra: null
4342
}
4443
%A]
4544
methods: array:%d [
4645
%A
4746
"export" => ReflectionMethod {
4847
+name: "export"
4948
+class: "ReflectionClass"
50-
parameters: array:2 [
51-
"$%s" => ReflectionParameter {
49+
parameters: {
50+
$%s: ReflectionParameter {
5251
%A position: 0
53-
%A }
54-
]
55-
modifiers: "public static"
56-
}
5752
%A
5853
}
54+
EOTXT
55+
, $var
56+
);
57+
}
58+
59+
public function testClosureCaster()
60+
{
61+
$a = $b = 123;
62+
$var = function ($x) use ($a, &$b) {};
63+
64+
$this->assertDumpMatchesFormat(
65+
<<<EOTXT
66+
Closure {
67+
class: "Symfony\Component\VarDumper\Tests\Caster\ReflectionCasterTest"
68+
%Aparameters: {
69+
\$x: {}
70+
}
71+
use: {
72+
\$a: 123
73+
\$b: & 123
74+
}
75+
file: "%sReflectionCasterTest.php"
76+
line: "62 to 62"
77+
}
5978
EOTXT
6079
, $var
6180
);

src/Symfony/Component/VarDumper/Tests/CliDumperTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ class: "Symfony\Component\VarDumper\Tests\CliDumperTest"
8484
+"bar": "bar"
8585
}
8686
"closure" => Closure {{$r}{$closure54}
87-
parameters: array:2 [
88-
"\$a" => []
89-
"&\$b" => array:2 [
90-
"typeHint" => "PDO"
91-
"default" => null
92-
]
93-
]
87+
parameters: {
88+
\$a: {}
89+
&\$b: {
90+
typeHint: "PDO"
91+
default: null
92+
}
93+
}
9494
file: "{$var['file']}"
9595
line: "{$var['line']} to {$var['line']}"
9696
}

src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ public function testGet()
8787
+"<span class=sf-dump-public title="Runtime added dynamic property">bar</span>": "<span class=sf-dump-str title="3 characters">bar</span>"
8888
</samp>}
8989
"<span class=sf-dump-key>closure</span>" => <span class=sf-dump-note>Closure</span> {{$r}<samp>{$closure54}
90-
<span class=sf-dump-meta>parameters</span>: <span class=sf-dump-note>array:2</span> [<samp>
91-
"<span class=sf-dump-key>\$a</span>" => []
92-
"<span class=sf-dump-key>&amp;\$b</span>" => <span class=sf-dump-note>array:2</span> [<samp>
93-
"<span class=sf-dump-key>typeHint</span>" => "<span class=sf-dump-str title="3 characters">PDO</span>"
94-
"<span class=sf-dump-key>default</span>" => <span class=sf-dump-const>null</span>
95-
</samp>]
96-
</samp>]
90+
<span class=sf-dump-meta>parameters</span>: {<samp>
91+
<span class=sf-dump-meta>\$a</span>: {}
92+
<span class=sf-dump-meta>&amp;\$b</span>: {<samp>
93+
<span class=sf-dump-meta>typeHint</span>: "<span class=sf-dump-str title="3 characters">PDO</span>"
94+
<span class=sf-dump-meta>default</span>: <span class=sf-dump-const>null</span>
95+
</samp>}
96+
</samp>}
9797
<span class=sf-dump-meta>file</span>: "<span class=sf-dump-str title="%d characters">{$var['file']}</span>"
9898
<span class=sf-dump-meta>line</span>: "<span class=sf-dump-str title="%d characters">{$var['line']} to {$var['line']}</span>"
9999
</samp>}

0 commit comments

Comments
 (0)
0