Skip to content

Commit 924d16e

Browse files
Merge branch '3.2'
* 3.2: Fix errors not rethrown even if not handled by console.error listeners [VarDumper] Fix dumping of non-nested stubs [Security] Avoid unnecessary route lookup for empty logout path respect inline level when dumping objects as maps Test case for not in-lined map-objects
2 parents dcbd318 + a94764d commit 924d16e

File tree

3 files changed

+110
-8
lines changed

3 files changed

+110
-8
lines changed

Dumper.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,20 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)
8181

8282
$output = '';
8383
$prefix = $indent ? str_repeat(' ', $indent) : '';
84+
$dumpObjectAsInlineMap = true;
8485

85-
if ($inline <= 0 || !is_array($input) || empty($input)) {
86+
if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($input instanceof \ArrayObject || $input instanceof \stdClass)) {
87+
$dumpObjectAsInlineMap = empty((array) $input);
88+
}
89+
90+
if ($inline <= 0 || (!is_array($input) && $dumpObjectAsInlineMap) || empty($input)) {
8691
$output .= $prefix.Inline::dump($input, $flags);
8792
} else {
88-
$isAHash = Inline::isHash($input);
93+
$dumpAsMap = Inline::isHash($input);
8994

9095
foreach ($input as $key => $value) {
9196
if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && is_string($value) && false !== strpos($value, "\n")) {
92-
$output .= sprintf("%s%s%s |\n", $prefix, $isAHash ? Inline::dump($key, $flags).':' : '-', '');
97+
$output .= sprintf("%s%s%s |\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '');
9398

9499
foreach (preg_split('/\n|\r\n/', $value) as $row) {
95100
$output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
@@ -98,11 +103,17 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)
98103
continue;
99104
}
100105

101-
$willBeInlined = $inline - 1 <= 0 || !is_array($value) || empty($value);
106+
$dumpObjectAsInlineMap = true;
107+
108+
if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \ArrayObject || $value instanceof \stdClass)) {
109+
$dumpObjectAsInlineMap = empty((array) $value);
110+
}
111+
112+
$willBeInlined = $inline - 1 <= 0 || !is_array($value) && $dumpObjectAsInlineMap || empty($value);
102113

103114
$output .= sprintf('%s%s%s%s',
104115
$prefix,
105-
$isAHash ? Inline::dump($key, $flags).':' : '-',
116+
$dumpAsMap ? Inline::dump($key, $flags).':' : '-',
106117
$willBeInlined ? ' ' : "\n",
107118
$this->dump($value, $inline - 1, $willBeInlined ? 0 : $indent + $this->indentation, $flags)
108119
).($willBeInlined ? "\n" : '');

Inline.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public static function dump($value, $flags = 0)
174174
}
175175

176176
if (Yaml::DUMP_OBJECT_AS_MAP & $flags && ($value instanceof \stdClass || $value instanceof \ArrayObject)) {
177-
return self::dumpArray((array) $value, $flags & ~Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
177+
return self::dumpArray($value, $flags & ~Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
178178
}
179179

180180
if (Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE & $flags) {
@@ -234,12 +234,16 @@ public static function dump($value, $flags = 0)
234234
*
235235
* @internal
236236
*
237-
* @param array $value The PHP array to check
237+
* @param array|\ArrayObject|\stdClass $value The PHP array or array-like object to check
238238
*
239239
* @return bool true if value is hash array, false otherwise
240240
*/
241-
public static function isHash(array $value)
241+
public static function isHash($value)
242242
{
243+
if ($value instanceof \stdClass || $value instanceof \ArrayObject) {
244+
return true;
245+
}
246+
243247
$expectedKey = 0;
244248

245249
foreach ($value as $key => $val) {

Tests/DumperTest.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,93 @@ public function objectAsMapProvider()
351351
return $tests;
352352
}
353353

354+
public function testDumpingArrayObjectInstancesRespectsInlineLevel()
355+
{
356+
$deep = new \ArrayObject(array('deep1' => 'd', 'deep2' => 'e'));
357+
$inner = new \ArrayObject(array('inner1' => 'b', 'inner2' => 'c', 'inner3' => $deep));
358+
$outer = new \ArrayObject(array('outer1' => 'a', 'outer2' => $inner));
359+
360+
$yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
361+
362+
$expected = <<<YAML
363+
outer1: a
364+
outer2:
365+
inner1: b
366+
inner2: c
367+
inner3: { deep1: d, deep2: e }
368+
369+
YAML;
370+
$this->assertSame($expected, $yaml);
371+
}
372+
373+
public function testDumpingArrayObjectInstancesWithNumericKeysInlined()
374+
{
375+
$deep = new \ArrayObject(array('d', 'e'));
376+
$inner = new \ArrayObject(array('b', 'c', $deep));
377+
$outer = new \ArrayObject(array('a', $inner));
378+
379+
$yaml = $this->dumper->dump($outer, 0, 0, Yaml::DUMP_OBJECT_AS_MAP);
380+
$expected = <<<YAML
381+
{ 0: a, 1: { 0: b, 1: c, 2: { 0: d, 1: e } } }
382+
YAML;
383+
$this->assertSame($expected, $yaml);
384+
}
385+
386+
public function testDumpingArrayObjectInstancesWithNumericKeysRespectsInlineLevel()
387+
{
388+
$deep = new \ArrayObject(array('d', 'e'));
389+
$inner = new \ArrayObject(array('b', 'c', $deep));
390+
$outer = new \ArrayObject(array('a', $inner));
391+
$yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
392+
$expected = <<<YAML
393+
0: a
394+
1:
395+
0: b
396+
1: c
397+
2: { 0: d, 1: e }
398+
399+
YAML;
400+
$this->assertEquals($expected, $yaml);
401+
}
402+
403+
public function testDumpEmptyArrayObjectInstanceAsMap()
404+
{
405+
$this->assertSame('{ }', $this->dumper->dump(new \ArrayObject(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP));
406+
}
407+
408+
public function testDumpEmptyStdClassInstanceAsMap()
409+
{
410+
$this->assertSame('{ }', $this->dumper->dump(new \stdClass(), 2, 0, Yaml::DUMP_OBJECT_AS_MAP));
411+
}
412+
413+
public function testDumpingStdClassInstancesRespectsInlineLevel()
414+
{
415+
$deep = new \stdClass();
416+
$deep->deep1 = 'd';
417+
$deep->deep2 = 'e';
418+
419+
$inner = new \stdClass();
420+
$inner->inner1 = 'b';
421+
$inner->inner2 = 'c';
422+
$inner->inner3 = $deep;
423+
424+
$outer = new \stdClass();
425+
$outer->outer1 = 'a';
426+
$outer->outer2 = $inner;
427+
428+
$yaml = $this->dumper->dump($outer, 2, 0, Yaml::DUMP_OBJECT_AS_MAP);
429+
430+
$expected = <<<YAML
431+
outer1: a
432+
outer2:
433+
inner1: b
434+
inner2: c
435+
inner3: { deep1: d, deep2: e }
436+
437+
YAML;
438+
$this->assertSame($expected, $yaml);
439+
}
440+
354441
public function testDumpMultiLineStringAsScalarBlock()
355442
{
356443
$data = array(

0 commit comments

Comments
 (0)