10000 Fixed dumpArray when passing an empty array by wouterj · Pull Request #16267 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Fixed dumpArray when passing an empty array #16267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
< 8000 span data-view-component="true"> Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fixed dumpArray when passing an empty array
  • Loading branch information
wouterj committed Oct 17, 2015
commit 9920a581a0d7f4f9062002f3c2eeba6e0d3b1e36
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ services:
properties: { foo: bar, moo: '@foo.baz', qux: { '%foo%': 'foo is %foo%', foobar: '%foo%' } }
calls:
- [setBar, ['@bar']]
- [initialize, { }]
- [initialize, []]

configurator: sc_configure
bar:
Expand Down
7 changes: 6 additions & 1 deletion src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,14 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp
*/
private static function dumpArray($value, $exceptionOnInvalidType, $objectSupport)
{
// array
$keys = array_keys($value);
$keysCount = count($keys);

if (0 === $keysCount) {
return '[]';
}

// sequence
if ((1 === $keysCount && '0' == $keys[0])
|| ($keysCount > 1 && array_reduce($keys, function ($v, $w) { return (int) $v + $w; }, 0) === $keysCount * ($keysCount - 1) / 2)
) {
Expand Down
62 changes: 35 additions & 27 deletions src/Symfony/Component/Yaml/Tests/DumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function testSetIndentation()
$expected = <<<EOF
'': bar
foo: '#bar'
'foo''bar': { }
'foo''bar': []
bar:
- 1
- foo
Expand Down Expand Up @@ -102,28 +102,38 @@ public function testSpecifications()
}
}

public function testInlineLevel()
/**
* @dataProvider getInlineLevelTests
*/
public function testInlineLevel($expected, $levels)
{
$expected = <<<EOF
{ '': bar, foo: '#bar', 'foo''bar': { }, bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } }
EOF;
$this->assertEquals($expected, $this->dumper->dump($this->array, -10), '->dump() takes an inline level argument');
$this->assertEquals($expected, $this->dumper->dump($this->array, 0), '->dump() takes an inline level argument');
$levels = (array) $levels;

$expected = <<<EOF
foreach ($levels as $level) {
$this->assertEquals($expected, $this->dumper->dump($this->array, $level), '->dump() takes an inline level argument');
}
}

public function getInlineLevelTests()
{
return array(
array(<<<EOF
{ '': bar, foo: '#bar', 'foo''bar': [], bar: [1, foo], foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } } }
EOF
, array(-10, 0)),
array(<<<EOF
'': bar
foo: '#bar'
'foo''bar': { }
'foo''bar': []
bar: [1, foo]
foobar: { foo: bar, bar: [1, foo], foobar: { foo: bar, bar: [1, foo] } }

EOF;
$this->assertEquals($expected, $this->dumper->dump($this->array, 1), '->dump() takes an inline level argument');

$expected = <<<EOF
EOF
, 1),
array(<<<EOF
'': bar
foo: '#bar'
'foo''bar': { }
'foo''bar': []
bar:
- 1
- foo
Expand All @@ -132,13 +142,12 @@ public function testInlineLevel()
bar: [1, foo]
foobar: { foo: bar, bar: [1, foo] }

EOF;
$this->assertEquals($expected, $this->dumper->dump($this->array, 2), '->dump() takes an inline level argument');

$expected = <<<EOF
EOF
, 2),
array(<<<EOF
'': bar
foo: '#bar'
'foo''bar': { }
'foo''bar': []
bar:
- 1
- foo
Expand All @@ -151,13 +160,12 @@ public function testInlineLevel()
foo: bar
bar: [1, foo]

EOF;
$this->assertEquals($expected, $this->dumper->dump($this->array, 3), '->dump() takes an inline level argument');

$expected = <<<EOF
EOF
, 3),
array(<<<EOF
'': bar
foo: '#bar'
'foo''bar': { }
'foo''bar': []
bar:
- 1
- foo
Expand All @@ -172,9 +180,9 @@ public function testInlineLevel()
- 1
- foo

EOF;
$this->assertEquals($expected, $this->dumper->dump($this->array, 4), '->dump() takes an inline level argument');
$this->assertEquals($expected, $this->dumper->dump($this->array, 10), '->dump() takes an inline level argument');
EOF
, array(4, 10)),
);
}

public function testObjectSupportEnabled()
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Yaml/Tests/InlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ protected function getTestsForDump()
"'off'" => 'off',

// sequences
'[]' => array(),
'[foo, bar, false, null, 12]' => array('foo', 'bar', false, null, 12),
'[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'),

Expand Down
0