Skip to content
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

[8.x] Arrayable/collection support for Collection::splice() replacement param #39592

Conversation

MarkKremer
Copy link
Contributor

Hi there,

Behavior

The Collection::splice()'s $replacement parameter has different behavior depending on whether a collection or array is passed in. I expected this to be the same. Example:

$data = collect(['foo', 'baz']);
$data->splice(1, 0, ['bar']); // array
dump($data->all());

// Output:
// array:3 [
//   0 => "foo"
//   1 => "bar"
//   2 => "baz"
// ]

$data = collect(['foo', 'baz']);
$data->splice(1, 0, collect(['bar'])); // collection
dump($data->all());

// Output:
// array:3 [
//  0 => "foo"
//  1 => array:1 [
//    0 => "bar"
//  ]
//  2 => "baz"
// ]

This is due to the way PHP's native array_splice method handles objects. Although, I think this is unexpected for the user.

Backwards compatibility

Backwards compatibility is broken for all objects which implements Enumerable, Arrayable, Jsonable, JsonSerializable or Traversable. Other types are cast to an array by the getArrayableItems() method, which is similar to what array_splice does. The latter does not break backward compatibility.

@MarkKremer MarkKremer changed the title [bug?] Add arrayable support for Collection::splice() replacement param [bug?] Arrayable/collection support for Collection::splice() replacement param Nov 12, 2021
@GrahamCampbell GrahamCampbell changed the title [bug?] Arrayable/collection support for Collection::splice() replacement param [8.x] Arrayable/collection support for Collection::splice() replacement param Nov 12, 2021
@taylorotwell taylorotwell merged commit 1acfd7c into laravel:8.x Nov 15, 2021
@MarkKremer MarkKremer deleted the collection-splice-support-arrayable-replacement branch November 15, 2021 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants