Skip to content

Commit 56832b4

Browse files
Merge branch '3.0' into 3.1
* 3.0: `@throws` annotations should go after `@return` Fix merge updated VERSION for 2.3.42 update CONTRIBUTORS for 2.3.42 updated CHANGELOG for 2.3.42 Revert "bug #18908 [DependencyInjection] force enabling the external XML entity loaders (xabbuh)" Partial revert of previous PR [DependencyInjection] Skip deep reference check for 'service_container' Catch \Throwable [Serializer] Add missing @throws annotations Fix for #18843 force enabling the external XML entity loaders Removed UTC specification with timestamp Conflicts: src/Symfony/Component/Yaml/Tests/InlineTest.php
2 parents eca51b7 + fd2f945 commit 56832b4

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

Dumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function dump($input, $inline = 0, $indent = 0, $flags = 0)
8585
if ($inline <= 0 || !is_array($input) || empty($input)) {
8686
$output .= $prefix.Inline::dump($input, $flags);
8787
} else {
88-
$isAHash = array_keys($input) !== range(0, count($input) - 1);
88+
$isAHash = Inline::isHash($input);
8989

9090
foreach ($input as $key => $value) {
9191
if ($inline > 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && is_string($value) && false !== strpos($value, "\n")) {

Inline.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,28 @@ public static function dump($value, $flags = 0)
214214
}
215215
}
216216

217+
/**
218+
* Check if given array is hash or just normal indexed array.
219+
*
220+
* @internal
221+
*
222+
* @param array $value The PHP array to check
223+
*
224+
* @return bool true if value is hash array, false otherwise
225+
*/
226+
public static function isHash(array $value)
227+
{
228+
$expectedKey = 0;
229+
230+
foreach ($value as $key => $val) {
231+
if ($key !== $expectedKey++) {
232+
return true;
233+
}
234+
}
235+
236+
return false;
237+
}
238+
217239
/**
218240
* Dumps a PHP array to a YAML string.
219241
*
@@ -225,11 +247,7 @@ public static function dump($value, $flags = 0)
225247
private static function dumpArray($value, $flags)
226248
{
227249
// array
228-
$keys = array_keys($value);
229-
$keysCount = count($keys);
230-
if ((1 === $keysCount && '0' == $keys[0])
231-
|| ($keysCount > 1 && array_reduce($keys, function ($v, $w) { return (int) $v + $w; }, 0) === $keysCount * ($keysCount - 1) / 2)
232-
) {
250+
if ($value && !self::isHash($value)) {
233251
$output = array();
234252
foreach ($value as $val) {
235253
$output[] = self::dump($val, $flags);
@@ -238,7 +256,7 @@ private static function dumpArray($value, $flags)
238256
return sprintf('[%s]', implode(', ', $output));
239257
}
240258

241-
// mapping
259+
// hash
242260
$output = array();
243261
foreach ($value as $key => $val) {
244262
$output[] = sprintf('%s: %s', self::dump($key, $flags), self::dump($val, $flags));

Tests/InlineTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,24 @@ public function testParseUnquotedScalarStartingWithPercentCharacter()
277277
$this->assertContains('Not quoting a scalar starting with the "%" indicator character is deprecated since Symfony 3.1 and will throw a ParseException in 4.0.', $deprecations[0]);
278278
}
279279

280+
/**
281+
* @dataProvider getDataForIsHash
282+
*/
283+
public function testIsHash($array, $expected)
284+
{
285+
$this->assertSame($expected, Inline::isHash($array));
286+
}
287+
288+
public function getDataForIsHash()
289+
{
290+
return array(
291+
array(array(), false),
292+
array(array(1, 2, 3), false),
293+
array(array(2 => 1, 1 => 2, 0 => 3), true),
294+
array(array('foo' => 1, 'bar' => 2), true),
295+
);
296+
}
297+
280298
public function getTestsForParse()
281299
{
282300
return array(
@@ -483,6 +501,8 @@ public function getTestsForDump()
483501
array('[foo, { bar: foo, foo: [foo, { bar: foo }] }, [foo, { bar: foo }]]', array('foo', array('bar' => 'foo', 'foo' => array('foo', array('bar' => 'foo'))), array('foo', array('bar' => 'foo')))),
484502

485503
array('[foo, \'@foo.baz\', { \'%foo%\': \'foo is %foo%\', bar: \'%foo%\' }, true, \'@service_container\']', array('foo', '@foo.baz', array('%foo%' => 'foo is %foo%', 'bar' => '%foo%'), true, '@service_container')),
504+
505+
array('{ foo: { bar: { 1: 2, baz: 3 } } }', array('foo' => array('bar' => array(1 => 2, 'baz' => 3)))),
486506
);
487507
}
488508

0 commit comments

Comments
 (0)