-
-
Notifications
You must be signed in to change notification settings - Fork 738
Closed
rectorphp/rector-src
#4323Description
I'm using a trick to serialize empty associative arrays in JSON.
For that purpose I cast the array into an object before serializing it.
Here is an oversimplified example:
<?php
final class Foo
{
/**
* @return array<string, stdClass>
*/
public static function cleanup(?string $value): array
{
return [
// Ensure empty metadata is serialized as `{}` and not `[]`
'metadata' => (object) array_filter([
'foo' => $value,
]),
];
}
}
assert(json_encode(Foo::cleanup('c')) === '{"metadata":{"foo":"c"}}');
assert(json_encode(Foo::cleanup(null)) === '{"metadata":{}}');It breaks the ArrayShapeFromConstantArrayReturnRector rule.
Here is the demo:
https://getrector.com/demo/ab809a74-239e-4b46-af98-b33a60643aa3
PS: Thanks for your amazing tool!
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels