Skip to content

Commit f57a837

Browse files
committed
feature #48478 [VarDumper] Add caster for WeakMap (nicolas-grekas)
This PR was merged into the 6.3 branch. Discussion ---------- [VarDumper] Add caster for WeakMap | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Commits ------- bde17b7 [VarDumper] Add caster for WeakMap
2 parents 6c8f6b3 + bde17b7 commit f57a837

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

src/Symfony/Component/VarDumper/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.3
5+
---
6+
7+
* Add caster for `WeakMap`
8+
49
6.2
510
---
611

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ public static function castObjectStorage(\SplObjectStorage $c, array $a, Stub $s
184184

185185
$clone = clone $c;
186186
foreach ($clone as $obj) {
187-
$storage[] = [
187+
$storage[] = new EnumStub([
188188
'object' => $obj,
189189
'info' => $clone->getInfo(),
190-
];
190+
]);
191191
}
192192

193193
$a += [
@@ -211,6 +211,24 @@ public static function castWeakReference(\WeakReference $c, array $a, Stub $stub
211211
return $a;
212212
}
213213

214+
public static function castWeakMap(\WeakMap $c, array $a, Stub $stub, bool $isNested)
215+
{
216+
$map = [];
217+
218+
foreach (clone $c as $obj => $data) {
219+
$map[] = new EnumStub([
220+
'object' => $obj,
221+
'data' => $data,
222+
]);
223+
}
224+
225+
$a += [
226+
Caster::PREFIX_VIRTUAL.'map' => $map,
227+
];
228+
229+
return $a;
230+
}
231+
214232
private static function castSplArray(\ArrayObject|\ArrayIterator $c, array $a, Stub $stub, bool $isNested): array
215233
{
216234
$prefix = Caster::PREFIX_VIRTUAL;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ abstract class AbstractCloner implements ClonerInterface
125125
'SplObjectStorage' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castObjectStorage'],
126126
'SplPriorityQueue' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castHeap'],
127127
'OuterIterator' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castOuterIterator'],
128+
'WeakMap' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castWeakMap'],
128129
'WeakReference' => ['Symfony\Component\VarDumper\Caster\SplCaster', 'castWeakReference'],
129130

130131
'Redis' => ['Symfony\Component\VarDumper\Caster\RedisCaster', 'castRedis'],

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,26 @@ public function testBadSplFileInfo()
213213
EOTXT;
214214
$this->assertDumpEquals($expected, $var);
215215
}
216+
217+
public function testWeakMap()
218+
{
219+
$var = new \WeakMap();
220+
$obj = new \stdClass();
221+
$var[$obj] = 123;
222+
223+
$expected = <<<EOTXT
224+
WeakMap {
225+
map: array:1 [
226+
0 => {
227+
object: {}
228+
data: 123
229+
}
230+
]
231+
}
232+
EOTXT;
233+
234+
$this->assertDumpEquals($expected, $var);
235+
}
216236
}
217237

218238
class MyArrayIterator extends \ArrayIterator

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy