Skip to content

Commit 208ca94

Browse files
[VarDumper] Allow preserving a subset of cut arrays
1 parent 449b18c commit 208ca94

File tree

5 files changed

+39
-14
lines changed

5 files changed

+39
-14
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
/**
15+
* Represents a cut array.
16+
*
17+
* @author Nicolas Grekas <p@tchwork.com>
18+
*/
19+
class CutArrayStub extends CutStub
20+
{
21+
public $preservedSubset;
22+
23+
public function __construct(array $value, array $preservedKeys)
24+
{
25+
parent::__construct($value);
26+
27+
$this->preservedSubset = array_intersect_key($value, array_flip($preservedKeys));
28+
$this->cut -= count($this->preservedSubset);
29+
}
30+
}

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,7 @@ public static function castFileObject(\SplFileObject $c, array $a, Stub $stub, $
160160
}
161161

162162
if (isset($a[$prefix.'fstat'])) {
163-
$fstat = $a[$prefix.'fstat'];
164-
$fstat = array(
165-
'dev' => $fstat['dev'],
166-
'ino' => $fstat['ino'],
167-
'nlink' => $fstat['nlink'],
168-
'rdev' => $fstat['rdev'],
169-
'blksize' => $fstat['blksize'],
170-
'blocks' => $fstat['blocks'],
171-
'' => ''.(count($fstat) - 6),
172-
);
173-
174-
$a[$prefix.'fstat'] = $fstat;
163+
$a[$prefix.'fstat'] = new CutArrayStub($a[$prefix.'fstat'], array('dev', 'ino', 'nlink', 'rdev', 'blksize', 'blocks'));
175164
}
176165

177166
return $a;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public static function castStub(Stub $c, array $a, Stub $stub, $isNested)
3333
}
3434
}
3535

36+
public static function castCutArray(CutArrayStub $c, array $a, Stub $stub, $isNested)
37+
{
38+
return $isNested ? $c->preservedSubset : $a;
39+
}
40+
3641
public static function cutInternals($obj, array $a, Stub $stub, $isNested)
3742
{
3843
if ($isNested) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ abstract class AbstractCloner implements ClonerInterface
2323
{
2424
public static $defaultCasters = array(
2525
'Symfony\Component\VarDumper\Caster\CutStub' => 'Symfony\Component\VarDumper\Caster\StubCaster::castStub',
26+
'Symfony\Component\VarDumper\Caster\CutArrayStub' => 'Symfony\Component\VarDumper\Caster\StubCaster::castCutArray',
2627
'Symfony\Component\VarDumper\Caster\ConstStub' => 'Symfony\Component\VarDumper\Caster\StubCaster::castStub',
2728

2829
'Closure' => 'Symfony\Component\VarDumper\Caster\ReflectionCaster::castClosure',

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ public function testCastFileObject()
106106
]
107107
flags: DROP_NEW_LINE|SKIP_EMPTY
108108
maxLineLen: 0
109-
fstat: array:7 [
109+
fstat: array:26 [
110110
"dev" => %d
111111
"ino" => %d
112112
"nlink" => %d
113113
"rdev" => 0
114114
"blksize" => %d
115115
"blocks" => %d
116-
"…" => "…20"
116+
…20
117117
]
118118
eof: false
119119
key: 0

0 commit comments

Comments
 (0)