10000 Fix for #18843 · src-run/symfony@7d78196 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7d78196

Browse files
Anton Bakainicolas-grekas
Anton Bakai
authored andcommitted
Fix for symfony#18843
1 parent 53b7236 commit 7d78196

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

src/Symfony/Component/Yaml/Dumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function dump($input, $inline = 0, $indent = 0, $exceptionOnInvalidType =
5858
if ($inline <= 0 || !is_array($input) || empty($input)) {
5959
$output .= $prefix.Inline::dump($input, $exceptionOnInvalidType, $objectSupport);
6060
} else {
61-
$isAHash = array_keys($input) !== range(0, count($input) - 1);
61+
$isAHash = Inline::isHash($input);
6262

6363
foreach ($input as $key => $value) {
6464
$willBeInlined = $inline - 1 <= 0 || !is_array($value) || empty($value);

src/Symfony/Component/Yaml/Inline.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,28 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp
145145
}
146146
}
147147

148+
/**
149+
* Check if given array is hash or just normal indexed array.
150+
*
151+
* @internal
152+
*
153+
* @param array $value The PHP array to check
154+
*
155+
* @return bool true if value is hash array, false otherwise
156+
*/
157+
public static function isHash(array $value)
158+
{
159+
$expectedKey = 0;
160+
161+
foreach ($value as $key => $val) {
162+
if ($key !== $expectedKey++) {
163+
return true;
164+
}
165+
}
166+
167+
return false;
168+
}
169+
148170
/**
149171
* Dumps a PHP array to a YAML string.
150172
*
@@ -157,11 +179,7 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp
157179
private static function dumpArray($value, $exceptionOnInvalidType, $objectSupport)
158180
{
159181
// array
160-
$keys = array_keys($value);
161-
$keysCount = count($keys);
162-
if ((1 === $keysCount && '0' == $keys[0])
163-
|| ($keysCount > 1 && array_reduce($keys, function ($v, $w) { return (int) $v + $w; }, 0) === $keysCount * ($keysCount - 1) / 2)
164-
) {
182+
if ($value && !self::isHash($value)) {
165183
$output = array();
166184 10000
foreach ($value as $val) {
167185
$output[] = self::dump($val, $exceptionOnInvalidType, $objectSupport);
@@ -170,7 +188,7 @@ private static function dumpArray($value, $exceptionOnInvalidType, $objectSuppor
170188
return sprintf('[%s]', implode(', ', $output));
171189
}
172190

173-
// mapping
191+
// hash
174192
$output = array();
175193
foreach ($value as $key => $val) {
176194
$output[] = sprintf('%s: %s', self::dump($key, $exceptionOnInvalidType, $objectSupport), self::dump($val, $exceptionOnInvalidType, $objectSupport));

src/Symfony/Component/Yaml/Tests/InlineTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,24 @@ public function testParseUnquotedAsteriskFollowedByAComment()
169169
Inline::parse('{ foo: * #foo }');
170170
}
171171

172+
/**
173+
* @dataProvider getDataForIsHash
174+
*/
175+
public function testIsHash($array, $expected)
176+
{
177+
$this->assertSame($expected, Inline::isHash($array));
178+
}
179+
180+
public function getDataForIsHash()
181+
{
182+
return array(
183+
array(array(), false),
184+
array(array(1, 2, 3), false),
185+
array(array(2 => 1, 1 => 2, 0 => 3), true),
186+
array(array('foo' => 1, 'bar' => 2), true),
187+
);
188+
}
189+
172190
protected function getTestsForParse()
173191
{
174192
return array(
@@ -296,6 +314,8 @@ protected function getTestsForDump()
296314
'[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'))),
297315

298316
'[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'),
317+
318+
'{ foo: { bar: { 1: 2, baz: 3 } } }' => array('foo' => array('bar' => array(1 => 2, 'baz' => 3))),
299319
);
300320
}
301321
}

0 commit comments

Comments
 (0)
0