Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b1e2ded

Browse files
feature #14079 [VarDumper] Add and use Caster::PREFIX_* consts (nicolas-grekas)
This PR was merged into the 2.7 branch. Discussion ---------- [VarDumper] Add and use Caster::PREFIX_* consts | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Will need a rebase on top of #14058 once it is merged Commits ------- 86cf8de [VarDumper] Make use of Caster::PREFIX_* consts
2 parents c5dce60 + 86cf8de commit b1e2ded

File tree

10 files changed

+94
-82
lines changed

10 files changed

+94
-82
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class AmqpCaster
4646

4747
public static function castConnection(\AMQPConnection $c, array $a, Stub $stub, $isNested)
4848
{
49-
$prefix = "\0~\0";
49+
$prefix = Caster::PREFIX_VIRTUAL;
5050

5151
// BC layer in the ampq lib
5252
if (method_exists($c, 'getReadTimeout')) {
@@ -70,7 +70,7 @@ public static function castConnection(\AMQPConnection $c, array $a, Stub $stub,
7070

7171
public static function castChannel(\AMQPChannel $c, array $a, Stub $stub, $isNested)
7272
{
73-
$prefix = "\0~\0";
73+
$prefix = Caster::PREFIX_VIRTUAL;
7474

7575
$a += array(
7676
$prefix.'isConnected' => $c->isConnected(),
@@ -85,7 +85,7 @@ public static function castChannel(\AMQPChannel $c, array $a, Stub $stub, $isNes
8585

8686
public static function castQueue(\AMQPQueue $c, array $a, Stub $stub, $isNested)
8787
{
88-
$prefix = "\0~\0";
88+
$prefix = Caster::PREFIX_VIRTUAL;
8989

9090
$a += array(
9191
$prefix.'name' => $c->getName(),
@@ -100,7 +100,7 @@ public static function castQueue(\AMQPQueue $c, array $a, Stub $stub, $isNested)
100100

101101
public static function castExchange(\AMQPExchange $c, array $a, Stub $stub, $isNested)
102102
{
103-
$prefix = "\0~\0";
103+
$prefix = Caster::PREFIX_VIRTUAL;
104104

105105
$a += array(
106106
$prefix.'name' => $c->getName(),
@@ -114,12 +114,15 @@ public static function castExchange(\AMQPExchange $c, array $a, Stub $stub, $isN
114114
return $a;
115115
}
116116

117-
public static function castEnvelope(\AMQPEnvelope $c, array $a, Stub $stub, $isNested)
117+
public static function castEnvelope(\AMQPEnvelope $c, array $a, Stub $stub, $isNested, $filter = 0)
118118
{
119-
$prefix = "\0~\0";
119+
$prefix = Caster::PREFIX_VIRTUAL;
120+
121+
if (!($filter & Caster::EXCLUDE_VERBOSE)) {
122+
$a += array($prefix.'body' => $c->getBody());
123+
}
120124

121125
$a += array(
122-
$prefix.'body' => $c->getBody(),
123126
$prefix.'routingKey' => $c->getRoutingKey(),
124127
$prefix.'deliveryTag' => $c->getDeliveryTag(),
125128
$prefix.'deliveryMode' => new ConstStub($c->getDeliveryMode().(2 === $c->getDeliveryMode() ? ' (persistent)' : ' (non-persistent)'), $c->getDeliveryMode()),

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,39 @@ class Caster
2929
const EXCLUDE_NOT_IMPORTANT = 256;
3030
const EXCLUDE_STRICT = 512;
3131

32+
const PREFIX_VIRTUAL = "\0~\0";
33+
const PREFIX_DYNAMIC = "\0+\0";
34+
const PREFIX_PROTECTED = "\0*\0";
35+
36+
/**
37+
* Casts objects to arrays and adds the dynamic property prefix.
38+
*
39+
* @param object $obj The object to cast.
40+
* @param ReflectionClass $reflector The class reflector to use for inspecting the object definition.
41+
*
42+
* @return array The array-cast of the object, with prefixed dynamic properties.
43+
*/
44+
public static function castObject($obj, \ReflectionClass $reflector)
45+
{
46+
if ($reflector->hasMethod('__debugInfo')) {
47+
$a = $obj->__debugInfo();
48+
} else {
49+
$a = (array) $obj;
50+
}
51+
52+
if ($a) {
53+
$p = array_keys($a);
54+
foreach ($p as $i => $k) {
55+
if (!isset($k[0]) || ("\0" !== $k[0] && !$reflector->hasProperty($k))) {
56+
$p[$i] = self::PREFIX_DYNAMIC.$k;
57+
}
58+
}
59+
$a = array_combine($p, $a);
60+
}
61+
62+
return $a;
63+
}
64+
3265
/**
3366
* Filters out the specified properties.
3467
*

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ class DOMCaster
6363

6464
public static function castException(\DOMException $e, array $a, Stub $stub, $isNested)
6565
{
66-
if (isset($a["\0*\0code"], self::$errorCodes[$a["\0*\0code"]])) {
67-
$a["\0*\0code"] = new ConstStub(self::$errorCodes[$a["\0*\0code"]], $a["\0*\0code"]);
66+
$k = Caster::PREFIX_PROTECTED.'code';
67+
if (isset($a[$k], self::$errorCodes[$a[$k]])) {
68+
$a[$k] = new ConstStub(self::$errorCodes[$a[$k]], $a[$k]);
6869
}
6970

7071
return $a;
@@ -82,8 +83,8 @@ public static function castLength($dom, array $a, Stub $stub, $isNested)
8283
public static function castImplementation($dom, array $a, Stub $stub, $isNested)
8384
{
8485
$a += array(
85-
"\0~\0Core" => '1.0',
86-
"\0~\0XML" => '2.0',
86+
Caster::PREFIX_VIRTUAL.'Core' => '1.0',
87+
Caster::PREFIX_VIRTUAL.'XML' => '2.0',
8788
);
8889

8990
return $a;
@@ -129,11 +130,8 @@ public static function castNameSpaceNode(\DOMNameSpaceNode $dom, array $a, Stub
129130
return $a;
130131
}
131132

132-
public static function castDocument(\DOMDocument $dom, array $a, Stub $stub, $isNested)
133+
public static function castDocument(\DOMDocument $dom, array $a, Stub $stub, $isNested, $filter = 0)
133134
{
134-
$formatOutput = $dom->formatOutput;
135-
$dom->formatOutput = true;
136-
137135
$a += array(
138136
'doctype' => $dom->doctype,
139137
'implementation' => $dom->implementation,
@@ -148,16 +146,20 @@ public static function castDocument(\DOMDocument $dom, array $a, Stub $stub, $is
148146
'strictErrorChecking' => $dom->strictErrorChecking,
149147
'documentURI' => $dom->documentURI,
150148
'config' => $dom->config,
151-
'formatOutput' => $formatOutput,
149+
'formatOutput' => $dom->formatOutput,
152150
'validateOnParse' => $dom->validateOnParse,
153151
'resolveExternals' => $dom->resolveExternals,
154152
'preserveWhiteSpace' => $dom->preserveWhiteSpace,
155153
'recover' => $dom->recover,
156154
'substituteEntities' => $dom->substituteEntities,
157-
"\0~\0xml" => $dom->saveXML(),
158155
);
159156

160-
$dom->formatOutput = $formatOutput;
157+
if (!($filter & Caster::EXCLUDE_VERBOSE)) {
158+
$formatOutput = $dom->formatOutput;
159+
$dom->formatOutput = true;
160+
$a += array(Caster::PREFIX_VIRTUAL.'xml' => $dom->saveXML());
161+
$dom->formatOutput = $formatOutput;
162+
}
161163

162164
return $a;
163165
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@ class MongoCaster
2222
{
2323
public static function castCursor(\MongoCursorInterface $cursor, array $a, Stub $stub, $isNested)
2424
{
25-
$prefix = "\0~\0";
26-
2725
if ($info = $cursor->info()) {
2826
foreach ($info as $k => $v) {
29-
$a[$prefix.$k] = $v;
27+
$a[Caster::PREFIX_VIRTUAL.$k] = $v;
3028
}
3129
}
32-
$a[$prefix.'dead'] = $cursor->dead();
30+
$a[Caster::PREFIX_VIRTUAL.'dead'] = $cursor->dead();
3331

3432
return $a;
3533
}

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,21 @@ public static function castPdo(\PDO $c, array $a, Stub $stub, $isNested)
7878
}
7979
}
8080

81-
$m = "\0~\0";
81+
$prefix = Caster::PREFIX_VIRTUAL;
8282
$a += array(
83-
$m.'inTransaction' => method_exists($c, 'inTransaction'),
84-
$m.'errorInfo' => $c->errorInfo(),
85-
$m.'attributes' => $attr,
83+
$prefix.'inTransaction' => method_exists($c, 'inTransaction'),
84+
$prefix.'errorInfo' => $c->errorInfo(),
85+
$prefix.'attributes' => $attr,
8686
);
8787

88-
if ($a[$m.'inTransaction']) {
89-
$a[$m.'inTransaction'] = $c->inTransaction();
88+
if ($a[$prefix.'inTransaction']) {
89+
$a[$prefix.'inTransaction'] = $c->inTransaction();
9090
} else {
91-
unset($a[$m.'inTransaction']);
91+
unset($a[$prefix.'inTransaction']);
9292
}
9393

94-
if (!isset($a[$m.'errorInfo'][1], $a[$m.'errorInfo'][2])) {
95-
unset($a[$m.'errorInfo']);
94+
if (!isset($a[$prefix.'errorInfo'][1], $a[$prefix.'errorInfo'][2])) {
95+
unset($a[$prefix.'errorInfo']);
9696
}
9797

9898
$c->setAttribute(\PDO::ATTR_ERRMODE, $errmode);
@@ -102,11 +102,11 @@ public static function castPdo(\PDO $c, array $a, Stub $stub, $isNested)
102102

103103
public static function castPdoStatement(\PDOStatement $c, array $a, Stub $stub, $isNested)
104104
{
105-
$m = "\0~\0";
106-
$a[$m.'errorInfo'] = $c->errorInfo();
105+
$prefix = Caster::PREFIX_VIRTUAL;
106+
$a[$prefix.'errorInfo'] = $c->errorInfo();
107107

108-
if (!isset($a[$m.'errorInfo'][1], $a[$m.'errorInfo'][2])) {
109-
unset($a[$m.'errorInfo']);
108+
if (!isset($a[$prefix.'errorInfo'][1], $a[$prefix.'errorInfo'][2])) {
109+
unset($a[$prefix.'errorInfo']);
110110
}
111111

112112
return $a;

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

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,23 @@ class SplCaster
2222
{
2323
public static function castArrayObject(\ArrayObject $c, array $a, Stub $stub, $isNested)
2424
{
25+
$prefix = Caster::PREFIX_VIRTUAL;
2526
$class = $stub->class;
2627
$flags = $c->getFlags();
2728

2829
$b = array(
29-
"\0~\0flag::STD_PROP_LIST" => (bool) ($flags & \ArrayObject::STD_PROP_LIST),
30-
"\0~\0flag::ARRAY_AS_PROPS" => (bool) ($flags & \ArrayObject::ARRAY_AS_PROPS),
31-
"\0~\0iteratorClass" => $c->getIteratorClass(),
32-
"\0~\0storage" => $c->getArrayCopy(),
30+
$prefix.'flag::STD_PROP_LIST' => (bool) ($flags & \ArrayObject::STD_PROP_LIST),
31+
$prefix.'flag::ARRAY_AS_PROPS' => (bool) ($flags & \ArrayObject::ARRAY_AS_PROPS),
32+
$prefix.'iteratorClass' => $c->getIteratorClass(),
33+
$prefix.'storage' => $c->getArrayCopy(),
3334
);
3435

3536
if ($class === 'ArrayObject') {
3637
$a = $b;
3738
} else {
3839
if (!($flags & \ArrayObject::STD_PROP_LIST)) {
3940
$c->setFlags(\ArrayObject::STD_PROP_LIST);
40-
41-
if ($a = (array) $c) {
42-
$class = new \ReflectionClass($class);
43-
foreach ($a as $k => $p) {
44-
if (!isset($k[0]) || ("\0" !== $k[0] && !$class->hasProperty($k))) {
45-
unset($a[$k]);
46-
$a["\0+\0".$k] = $p;
47-
}
48-
}
49-
}
50-
41+
$a = Caster::castObject($c, new \ReflectionClass($class));
5142
$c->setFlags($flags);
5243
}
5344

@@ -60,20 +51,21 @@ public static function castArrayObject(\ArrayObject $c, array $a, Stub $stub, $i
6051
public static function castHeap(\Iterator $c, array $a, Stub $stub, $isNested)
6152
{
6253
$a += array(
63-
"\0~\0heap" => iterator_to_array(clone $c),
54+
Caster::PREFIX_VIRTUAL.'heap' => iterator_to_array(clone $c),
6455
);
6556

6657
return $a;
6758
}
6859

6960
public static function castDoublyLinkedList(\SplDoublyLinkedList $c, array $a, Stub $stub, $isNested)
7061
{
62+
$prefix = Caster::PREFIX_VIRTUAL;
7163
$mode = $c->getIteratorMode();
7264
$c->setIteratorMode(\SplDoublyLinkedList::IT_MODE_KEEP | $mode & ~\SplDoublyLinkedList::IT_MODE_DELETE);
7365

7466
$a += array(
75-
"\0~\0mode" => new ConstStub((($mode & \SplDoublyLinkedList::IT_MODE_LIFO) ? 'IT_MODE_LIFO' : 'IT_MODE_FIFO').' | '.(($mode & \SplDoublyLinkedList::IT_MODE_KEEP) ? 'IT_MODE_KEEP' : 'IT_MODE_DELETE'), $mode),
76-
"\0~\0dllist" => iterator_to_array($c),
67+
$prefix.'mode' => new ConstStub((($mode & \SplDoublyLinkedList::IT_MODE_LIFO) ? 'IT_MODE_LIFO' : 'IT_MODE_FIFO').' | '.(($mode & \SplDoublyLinkedList::IT_MODE_KEEP) ? 'IT_MODE_KEEP' : 'IT_MODE_DELETE'), $mode),
68+
$prefix.'dllist' => iterator_to_array($c),
7769
);
7870
$c->setIteratorMode($mode);
7971

@@ -83,7 +75,7 @@ public static function castDoublyLinkedList(\SplDoublyLinkedList $c, array $a, S
8375
public static function castFixedArray(\SplFixedArray $c, array $a, Stub $stub, $isNested)
8476
{
8577
$a += array(
86-
"\0~\0storage" => $c->toArray(),
78+
Caster::PREFIX_VIRTUAL.'storage' => $c->toArray(),
8779
);
8880

8981
return $a;
@@ -92,7 +84,7 @@ public static function castFixedArray(\SplFixedArray $c, array $a, Stub $stub, $
9284
public static function castObjectStorage(\SplObjectStorage $c, array $a, Stub $stub, $isNested)
9385
{
9486
$storage = array();
95-
unset($a["\0+\0\0gcdata"]); // Don't hit https://bugs.php.net/65967
87+
unset($a[Caster::PREFIX_DYNAMIC."\0gcdata"]); // Don't hit https://bugs.php.net/65967
9688

9789
foreach ($c as $obj) {
9890
$storage[spl_object_hash($obj)] = array(
@@ -102,7 +94,7 @@ public static function castObjectStorage(\SplObjectStorage $c, array $a, Stub $s
10294
}
10395

10496
$a += array(
105-
"\0~\0storage" => $storage,
97+
Caster::PREFIX_VIRTUAL.'storage' => $storage,
10698
);
10799

108100
return $a;

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

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\VarDumper\Cloner;
1313

14+
use Symfony\Component\VarDumper\Caster\Caster;
1415
use Symfony\Component\VarDumper\Exception\ThrowingCasterException;
1516

1617
/**
@@ -206,31 +207,16 @@ protected function castObject(Stub $stub, $isNested)
206207
} else {
207208
$classInfo = array(
208209
$class,
209-
method_exists($class, '__debugInfo'),
210210
new \ReflectionClass($class),
211211
array_reverse(array('*' => '*', $class => $class) + class_parents($class) + class_implements($class)),
212212
);
213213

214214
$this->classInfo[$class] = $classInfo;
215215
}
216216

217-
if ($classInfo[1]) {
218-
$a = $this->callCaster(function ($obj) {return $obj->__debugInfo();}, $obj, array(), null, $isNested);
219-
} else {
220-
$a = (array) $obj;
221-
}
222-
223-
if ($a) {
224-
$p = array_keys($a);
225-
foreach ($p as $i => $k) {
226-
if (!isset($k[0]) || ("\0" !== $k[0] && !$classInfo[2]->hasProperty($k))) {
227-
$p[$i] = "\0+\0".$k;
228-
}
229-
}
230-
$a = array_combine($p, $a);
231-
}
217+
$a = $this->callCaster('Symfony\Component\VarDumper\Caster\Caster::castObject', $obj, $classInfo[1], null, $isNested);
232218

233-
foreach ($classInfo[3] as $p) {
219+
foreach ($classInfo[2] as $p) {
234220
if (!empty($this->casters[$p = strtolower($p)])) {
235221
foreach ($this->casters[$p] as $p) {
236222
$a = $this->callCaster($p, $obj, $a, $stub, $isNested);
@@ -284,7 +270,7 @@ private function callCaster($callback, $obj, $a, $stub, $isNested)
284270
$a = $cast;
285271
}
286272
} catch (\Exception $e) {
287-
$a[(Stub::TYPE_OBJECT === $stub->type ? "\0~\0" : '').''] = new ThrowingCasterException($callback, $e);
273+
$a[(Stub::TYPE_OBJECT === $stub->type ? Caster::PREFIX_VIRTUAL : '').''] = new ThrowingCasterException($callback, $e);
288274
}
289275

290276
return $a;

src/Symfony/Component/VarDumper/Test/VarDumperTestCase.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ abstract class VarDumperTestCase extends \PHPUnit_Framework_TestCase
2121
{
2222
public function assertDumpEquals($dump, $data, $message = '')
2323
{
24-
$this->assertSame($dump, $this->getVarDumperDump($data), $message);
24+
$this->assertSame(rtrim($dump), $this->getVarDumperDump($data), $message);
2525
}
2626

2727
public function assertDumpMatchesFormat($dump, $data, $message = '')
2828
{
29-
$this->assertStringMatchesFormat($dump, $this->getVarDumperDump($data), $message);
29+
$this->assertStringMatchesFormat(rtrim($dump), $this->getVarDumperDump($data), $message);
3030
}
3131

3232
private function getVarDumperDump($data)
@@ -39,6 +39,6 @@ private function getVarDumperDump($data)
3939
$data = stream_get_contents($h, -1, 0);
4040
fclose($h);
4141

42-
return $data;
42+
return rtrim($data);
4343
}
4444
}

0 commit comments

Comments
 (0)