From 65b517b01858b81bbc619fcba5ef9c30444c3692 Mon Sep 17 00:00:00 2001 From: Anton Bakai Date: Tue, 24 May 2016 23:58:05 +0300 Subject: [PATCH 1/7] added test for #18843 --- src/Symfony/Component/Yaml/Tests/InlineTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Symfony/Component/Yaml/Tests/InlineTest.php b/src/Symfony/Component/Yaml/Tests/InlineTest.php index 255928d16d2cd..2b237d63d45ca 100644 --- a/src/Symfony/Component/Yaml/Tests/InlineTest.php +++ b/src/Symfony/Component/Yaml/Tests/InlineTest.php @@ -296,6 +296,8 @@ protected function getTestsForDump() '[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'))), '[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'), + + '{ foo: { bar: { 1: 2, baz: 3 } } }' => array('foo' => array('bar' => array(1 => 2, 'baz' => 3))), ); } } From 64b053138cfa1b16dee423d2311b51a5c8f59628 Mon Sep 17 00:00:00 2001 From: Anton Bakai Date: Wed, 25 May 2016 00:00:40 +0300 Subject: [PATCH 2/7] fixed #18843 (fixed detection of normal/associative arrays) --- src/Symfony/Component/Yaml/Inline.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index 5f15e3c5b4a9b..66c9930b8f971 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -156,12 +156,22 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp */ private static function dumpArray($value, $exceptionOnInvalidType, $objectSupport) { + if ($value) { + $expectedKey = 0; + $isMapping = false; + + foreach ($value as $key => $val) { + if ($key !== $expectedKey++) { + $isMapping = true; + break; + } + } + } else { + $isMapping = true; + } + // array - $keys = array_keys($value); - $keysCount = count($keys); - if ((1 === $keysCount && '0' == $keys[0]) - || ($keysCount > 1 && array_reduce($keys, function ($v, $w) { return (int) $v + $w; }, 0) === $keysCount * ($keysCount - 1) / 2) - ) { + if (!$isMapping) { $output = array(); foreach ($value as $val) { $output[] = self::dump($val, $exceptionOnInvalidType, $objectSupport); From 3150e456fb8d218bf142878a4e9e3b698ff1333f Mon Sep 17 00:00:00 2001 From: Anton Bakai Date: Sun, 29 May 2016 20:11:28 +0300 Subject: [PATCH 3/7] extracted checking if array is hash to dedicated method and used it in Inline and Dumper --- src/Symfony/Component/Yaml/Dumper.php | 2 +- src/Symfony/Component/Yaml/Inline.php | 38 ++++++++++++++++----------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/Symfony/Component/Yaml/Dumper.php b/src/Symfony/Component/Yaml/Dumper.php index 21351a5c34fc9..9bd9389c07483 100644 --- a/src/Symfony/Component/Yaml/Dumper.php +++ b/src/Symfony/Component/Yaml/Dumper.php @@ -58,7 +58,7 @@ public function dump($input, $inline = 0, $indent = 0, $exceptionOnInvalidType = if ($inline <= 0 || !is_array($input) || empty($input)) { $output .= $prefix.Inline::dump($input, $exceptionOnInvalidType, $objectSupport); } else { - $isAHash = array_keys($input) !== range(0, count($input) - 1); + $isAHash = Inline::isHash($input); foreach ($input as $key => $value) { $willBeInlined = $inline - 1 <= 0 || !is_array($value) || empty($value); diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index 66c9930b8f971..1e817945a5533 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -145,6 +145,26 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp } } + /** + * Check if given array is hash or just normal indexed array + * + * @param array $value The PHP array to check + * + * @return bool true if value is hash array, false otherwise + */ + public static function isHash(array $value) + { + $expectedKey = 0; + + foreach ($value as $key => $val) { + if ($key !== $expectedKey++) { + return true; + } + } + + return false; + } + /** * Dumps a PHP array to a YAML string. * @@ -156,22 +176,8 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp */ private static function dumpArray($value, $exceptionOnInvalidType, $objectSupport) { - if ($value) { - $expectedKey = 0; - $isMapping = false; - - foreach ($value as $key => $val) { - if ($key !== $expectedKey++) { - $isMapping = true; - break; - } - } - } else { - $isMapping = true; - } - // array - if (!$isMapping) { + if ($value && !self::isHash($value)) { $output = array(); foreach ($value as $val) { $output[] = self::dump($val, $exceptionOnInvalidType, $objectSupport); @@ -180,7 +186,7 @@ private static function dumpArray($value, $exceptionOnInvalidType, $objectSuppor return sprintf('[%s]', implode(', ', $output)); } - // mapping + // hash $output = array(); foreach ($value as $key => $val) { $output[] = sprintf('%s: %s', self::dump($key, $exceptionOnInvalidType, $objectSupport), self::dump($val, $exceptionOnInvalidType, $objectSupport)); From 1f5db46abb86ccff3f8568f6098f1543a7a6aa25 Mon Sep 17 00:00:00 2001 From: Anton Bakai Date: Sun, 29 May 2016 20:15:37 +0300 Subject: [PATCH 4/7] added test for Inline::isHash --- .../Component/Yaml/Tests/InlineTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Symfony/Component/Yaml/Tests/InlineTest.php b/src/Symfony/Component/Yaml/Tests/InlineTest.php index 2b237d63d45ca..23f02f00822f0 100644 --- a/src/Symfony/Component/Yaml/Tests/InlineTest.php +++ b/src/Symfony/Component/Yaml/Tests/InlineTest.php @@ -169,6 +169,24 @@ public function testParseUnquotedAsteriskFollowedByAComment() Inline::parse('{ foo: * #foo }'); } + /** + * @dataProvider getDataForIsHash + */ + public function testIsHash($array, $expected) + { + $this->assertSame($expected, Inline::isHash($array)); + } + + public function getDataForIsHash() + { + return array( + array([], false), + array([1, 2, 3], false), + array([2 => 1, 1 => 2, 0 => 3], true), + array(['foo' => 1, 'bar' => 2], true), + ); + } + protected function getTestsForParse() { return array( From b01b05782aa4e384410c8b2d2479386610ac749d Mon Sep 17 00:00:00 2001 From: Anton Bakai Date: Sun, 29 May 2016 20:18:17 +0300 Subject: [PATCH 5/7] cs fix --- src/Symfony/Component/Yaml/Inline.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index 1e817945a5533..20caffe630ef0 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -146,7 +146,7 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp } /** - * Check if given array is hash or just normal indexed array + * Check if given array is hash or just normal indexed array. * * @param array $value The PHP array to check * From ff26a5b22eff5a7a4470c5ed472faee334e13ad8 Mon Sep 17 00:00:00 2001 From: Anton Bakai Date: Sun, 29 May 2016 21:48:22 +0300 Subject: [PATCH 6/7] replaced accidentally used short array notation with traditional --- src/Symfony/Component/Yaml/Tests/InlineTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Yaml/Tests/InlineTest.php b/src/Symfony/Component/Yaml/Tests/InlineTest.php index 23f02f00822f0..b1263d8d290bb 100644 --- a/src/Symfony/Component/Yaml/Tests/InlineTest.php +++ b/src/Symfony/Component/Yaml/Tests/InlineTest.php @@ -180,10 +180,10 @@ public function testIsHash($array, $expected) public function getDataForIsHash() { return array( - array([], false), - array([1, 2, 3], false), - array([2 => 1, 1 => 2, 0 => 3], true), - array(['foo' => 1, 'bar' => 2], true), + array(array(), false), + array(array(1, 2, 3), false), + array(array(2 => 1, 1 => 2, 0 => 3), true), + array(array('foo' => 1, 'bar' => 2), true), ); } From 1bafe3a911b482c1fb7bd2e7025c039baa83f2ff Mon Sep 17 00:00:00 2001 From: Anton Bakai Date: Sun, 29 May 2016 21:51:06 +0300 Subject: [PATCH 7/7] marked method as internal --- src/Symfony/Component/Yaml/Inline.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Symfony/Component/Yaml/Inline.php b/src/Symfony/Component/Yaml/Inline.php index 20caffe630ef0..0ce1ced7d7ab8 100644 --- a/src/Symfony/Component/Yaml/Inline.php +++ b/src/Symfony/Component/Yaml/Inline.php @@ -148,6 +148,8 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp /** * Check if given array is hash or just normal indexed array. * + * @internal + * * @param array $value The PHP array to check * * @return bool true if value is hash array, false otherwise