-
Notifications
You must be signed in to change notification settings - Fork 177
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
fix(php): JSON serialize empty arrays as empty object #5986
fix(php): JSON serialize empty arrays as empty object #5986
Conversation
…p/json--serialize-empty-arrays-as-object
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only nits, but lgtm!
* @param array $arr | ||
* @return bool | ||
*/ | ||
private static function isSequential(array $arr): bool { // @phpstan-ignore-line |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are we ignoring here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
phpstan wants array
to have more information, but we don't have more info
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, phpstan doesn't mind mixed[]
$overrideProperties = $options['bodyProperties'] ?? []; | ||
if(is_array($body) && self::isSequential($body)){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Let's tidy up the spacing on these:
if(is_array($body) && self::isSequential($body)){ | |
if (is_array($body) && self::isSequential($body)) { |
if(is_array($result)){ | ||
$result = array_merge($result, $overrideProperties); | ||
if(empty($result)){ | ||
// force to be serialized as {} instead of [] | ||
return (object)($result); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if(is_array($result)){ | |
$result = array_merge($result, $overrideProperties); | |
if(empty($result)){ | |
// force to be serialized as {} instead of [] | |
return (object)($result); | |
} | |
} | |
if (is_array($result)){ | |
$result = array_merge($result, $overrideProperties); | |
if (empty($result)){ | |
// force to be serialized as {} instead of [] | |
return (object)($result); | |
} | |
} |
Description
JSON serialize empty arrays as empty object
Changes Made
Testing