Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 23da9db

Browse filesBrowse files
bug #50960 [VarDumper] Fix dumping ArrayObject with DumpDataCollector (lyrixx, HypeMC)
This PR was merged into the 5.4 branch. Discussion ---------- [VarDumper] Fix dumping `ArrayObject` with `DumpDataCollector` | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #50940 | License | MIT | Doc PR | - The main problem here is that the `ArrayObjectstorage` key in [the array returned by `__debugInfo()`](https://github.com/symfony/symfony/blob/de7ab4d85dcc94032b77f1a15d7a030714a1f734/src/Symfony/Component/VarDumper/Caster/SplCaster.php#L229-L229) seems to be a reference: https://3v4l.org/8rSFn (a bug in PHP perhaps?) Since the `DumpDataCollector` does the actual dumping in the `__destructor()` the `ArrayObjectstorage` is modified by then which messes with the object created by the `VarCloner`. Commits ------- b25f377 [VarDumper] Fix dumping `ArrayObject` with `DumpDataCollector` 1f2c6f7 [VarDumper] Add tests to demonstrate a bug when dumping ArrayObject with full stack fmk
2 parents a056e7b + b25f377 commit 23da9db
Copy full SHA for 23da9db

File tree

6 files changed

+78
-12
lines changed
Filter options

6 files changed

+78
-12
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Caster/SplCaster.php
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,11 @@ private static function castSplArray($c, array $a, Stub $stub, bool $isNested):
229229
$a = Caster::castObject($c, \get_class($c), method_exists($c, '__debugInfo'), $stub->class);
230230
$c->setFlags($flags);
231231
}
232-
if (\PHP_VERSION_ID < 70400) {
233-
$a[$prefix.'storage'] = $c->getArrayCopy();
234-
}
232+
233+
unset($a["\0ArrayObject\0storage"], $a["\0ArrayIterator\0storage"]);
234+
235235
$a += [
236+
$prefix.'storage' => $c->getArrayCopy(),
236237
$prefix.'flag::STD_PROP_LIST' => (bool) ($flags & \ArrayObject::STD_PROP_LIST),
237238
$prefix.'flag::ARRAY_AS_PROPS' => (bool) ($flags & \ArrayObject::ARRAY_AS_PROPS),
238239
];

‎src/Symfony/Component/VarDumper/Dumper/ContextProvider/SourceContextProvider.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Dumper/ContextProvider/SourceContextProvider.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function getContext(): ?array
4444

4545
$file = $trace[1]['file'];
4646
$line = $trace[1]['line'];
47-
$name = false;
47+
$name = '-' === $file || 'Standard input code' === $file ? 'Standard input code' : false;
4848
$fileExcerpt = false;
4949

5050
for ($i = 2; $i < $this->limit; ++$i) {

‎src/Symfony/Component/VarDumper/Tests/Caster/SplCasterTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Tests/Caster/SplCasterTest.php
+2-8Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,14 @@ class([123]) extends \ArrayObject {};
174174
$expected = <<<EOTXT
175175
ArrayObject@anonymous {
176176
+"foo": 234
177-
-storage: array:1 [
177+
storage: array:1 [
178178
0 => 123
179179
]
180180
flag::STD_PROP_LIST: false
181181
flag::ARRAY_AS_PROPS: false
182182
iteratorClass: "ArrayIterator"
183183
}
184184
EOTXT;
185-
if (\PHP_VERSION_ID < 70400) {
186-
$expected = str_replace('-storage:', 'storage:', $expected);
187-
}
188185
$this->assertDumpEquals($expected, $var);
189186
}
190187

@@ -195,16 +192,13 @@ public function testArrayIterator()
195192
$expected = <<<EOTXT
196193
Symfony\Component\VarDumper\Tests\Caster\MyArrayIterator {
197194
-foo: 123
198-
-storage: array:1 [
195+
storage: array:1 [
199196
0 => 234
200197
]
201198
flag::STD_PROP_LIST: false
202199
flag::ARRAY_AS_PROPS: false
203200
}
204201
EOTXT;
205-
if (\PHP_VERSION_ID < 70400) {
206-
$expected = str_replace('-storage:', 'storage:', $expected);
207-
}
208202
$this->assertDumpEquals($expected, $var);
209203
}
210204

+69Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
--TEST--
2+
Test integration with Symfony's DumpDataCollector
3+
--FILE--
4+
<?php
5+
putenv('NO_COLOR=1');
6+
7+
$vendor = __DIR__;
8+
while (!file_exists($vendor.'/vendor')) {
9+
$vendor = dirname($vendor);
10+
}
11+
require $vendor.'/vendor/autoload.php';
12+
13+
use Symfony\Component\HttpKernel\DataCollector\DumpDataCollector;
14+
use Symfony\Component\VarDumper\Cloner\VarCloner;
15+
use Symfony\Component\VarDumper\VarDumper;
16+
17+
VarDumper::setHandler(function ($var, string $label = null) {
18+
$dumper = new DumpDataCollector();
19+
$cloner = new VarCloner();
20+
$handler = function ($var, string $label = null) use ($dumper, $cloner) {
21+
$var = $cloner->cloneVar($var);
22+
if (null !== $label) {
23+
$var = $var->withContext(['label' => $label]);
24+
}
25+
26+
$dumper->dump($var);
27+
};
28+
VarDumper::setHandler($handler);
29+
$handler($var, $label);
30+
});
31+
32+
$arrayObject = new \ArrayObject();
33+
dump($arrayObject);
34+
$arrayObject['X'] = 'A';
35+
$arrayObject['Y'] = new \ArrayObject(['type' => 'object']);
36+
$arrayObject['Y']['Z'] = 'B';
37+
38+
$arrayIterator = new \ArrayIterator();
39+
dump($arrayIterator);
40+
$arrayIterator['X'] = 'A';
41+
$arrayIterator['Y'] = new \ArrayIterator(['type' => 'object']);
42+
$arrayIterator['Y']['Z'] = 'B';
43+
44+
$recursiveArrayIterator = new \RecursiveArrayIterator();
45+
dump($recursiveArrayIterator);
46+
$recursiveArrayIterator['X'] = 'A';
47+
$recursiveArrayIterator['Y'] = new \RecursiveArrayIterator(['type' => 'object']);
48+
$recursiveArrayIterator['Y']['Z'] = 'B';
49+
50+
--EXPECTF--
51+
%s on line %d:
52+
ArrayObject {#%d
53+
storage: []
54+
flag::STD_PROP_LIST: false
55+
flag::ARRAY_AS_PROPS: false
56+
iteratorClass: "ArrayIterator"
57+
}
58+
%s on line %d:
59+
ArrayIterator {#%d
60+
storage: []
61+
flag::STD_PROP_LIST: false
62+
flag::ARRAY_AS_PROPS: false
63+
}
64+
%s on line %d:
65+
RecursiveArrayIterator {#%d
66+
storage: []
67+
flag::STD_PROP_LIST: false
68+
flag::ARRAY_AS_PROPS: false
69+
}

‎src/Symfony/Component/VarDumper/composer.json

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/composer.json
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"require-dev": {
2424
"ext-iconv": "*",
2525
"symfony/console": "^4.4|^5.0|^6.0",
26+
"symfony/http-kernel": "^4.4|^5.0|^6.0",
2627
"symfony/process": "^4.4|^5.0|^6.0",
2728
"symfony/uid": "^5.1|^6.0",
2829
"twig/twig": "^2.13|^3.0.4"

‎src/Symfony/Component/VarDumper/phpunit.xml.dist

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/phpunit.xml.dist
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<testsuites>
1818
<testsuite name="Symfony VarDumper Component Test Suite">
1919
<directory>./Tests/</directory>
20+
<directory suffix=".phpt">./Tests/Dumper/functions/</directory>
2021
</testsuite>
2122
</testsuites>
2223

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.