Skip to content

Commit 815fabf

Browse files
Merge branch '2.7' into 2.8
* 2.7: `@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/DependencyInjection/Tests/Dumper/PhpDumperTest.php src/Symfony/Component/Finder/Finder.php src/Symfony/Component/Security/Acl/Dbal/MutableAclProvider.php src/Symfony/Component/Security/Acl/Domain/ObjectIdentity.php src/Symfony/Component/Security/Acl/Model/AclInterface.php src/Symfony/Component/Security/Acl/Model/MutableAclProviderInterface.php src/Symfony/Component/Security/Acl/Permission/MaskBuilder.php src/Symfony/Component/Translation/Loader/XliffFileLoader.php src/Symfony/Component/Yaml/Tests/InlineTest.php
2 parents 5190891 + 4c2dc51 commit 815fabf

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
@@ -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);

Inline.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,28 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp
157157
}
158158
}
159159

160+
/**
161+
* Check if given array is hash or just normal indexed array.
162+
*
163+
* @internal
164+
*
165+
* @param array $value The PHP array to check
166+
*
167+
* @return bool true if value is hash array, false otherwise
168+
*/
169+
public static function isHash(array $value)
170+
{
171+
$expectedKey = 0;
172+
173+
foreach ($value as $key => $val) {
174+
if ($key !== $expectedKey++) {
175+
return true;
176+
}
177+
}
178+
179+
return false;
180+
}
181+
160182
/**
161183
* Dumps a PHP array to a YAML string.
162184
*
@@ -169,11 +191,7 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp
169191
private static function dumpArray($value, $exceptionOnInvalidType, $objectSupport)
170192
{
171193
// array
172-
$keys = array_keys($value);
173-
$keysCount = count($keys);
174-
if ((1 === $keysCount && '0' == $keys[0])
175-
|| ($keysCount > 1 && array_reduce($keys, function ($v, $w) { return (int) $v + $w; }, 0) === $keysCount * ($keysCount - 1) / 2)
176-
) {
194+
if ($value && !self::isHash($value)) {
177195
$output = array();
178196
foreach ($value as $val) {
179197
$output[] = self::dump($val, $exceptionOnInvalidType, $objectSupport);
@@ -182,7 +200,7 @@ private static function dumpArray($value, $exceptionOnInvalidType, $objectSuppor
182200
return sprintf('[%s]', implode(', ', $output));
183201
}
184202

185-
// mapping
203+
// hash
186204
$output = array();
187205
foreach ($value as $key => $val) {
188206
$output[] = sprintf('%s: %s', self::dump($key, $exceptionOnInvalidType, $objectSupport), self::dump($val, $exceptionOnInvalidType, $objectSupport));

Tests/InlineTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,24 @@ public function getScalarIndicators()
220220
return array(array('|'), array('>'));
221221
}
222222

223+
/**
224+
* @dataProvider getDataForIsHash
225+
*/
226+
public function testIsHash($array, $expected)
227+
{
228+
$this->assertSame($expected, Inline::isHash($array));
229+
}
230+
231+
public function getDataForIsHash()
232+
{
233+
return array(
234+
array(array(), false),
235+
array(array(1, 2, 3), false),
236+
array(array(2 => 1, 1 => 2, 0 => 3), true),
237+
array(array('foo' => 1, 'bar' => 2), true),
238+
);
239+
}
240+
223241
public function getTestsForParse()
224242
{
225243
return array(
@@ -426,6 +444,8 @@ public function getTestsForDump()
426444
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')))),
427445

428446
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')),
447+
448+
array('{ foo: { bar: { 1: 2, baz: 3 } } }', array('foo' => array('bar' => array(1 => 2, 'baz' => 3)))),
429449
);
430450
}
431451
}

0 commit comments

Comments
 (0)