8000 Fix for #18843 by inso · Pull Request #18861 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Fix for #18843 #18861

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 7 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixed #18843 (fixed detection of normal/associative arrays)
  • Loading branch information
Anton Bakai committed May 24, 2016
commit 64b053138cfa1b16dee423d2311b51a5c8f59628
20 changes: 15 additions & 5 deletions src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,22 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp
*/
private static function dumpArray($value, $exceptionOnInvalidType, $objectSupport)
{
if ($value) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was intrigued by this problem: "how to detect if an array is associative or not" and I found two one-liners on StackOverflow. Maybe we can simplify this code?

$isMapping = array_keys($value) !== range(0, count($value) - 1);

$isMapping = count(array_filter(array_keys($value), 'is_string')) > 0;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My solution uses less memory and is faster

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the Dumper class we use array_keys($input) !== range(0, count($input) - 1) to detect the type of array. I am fine with changing the way we detect it, but then we should move the detection into a dedicated method an reuse it in both places.

$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);
Expand Down
0