Wrong usage of array_splice

Created on 6 December 2024, 4 months ago

Problem/Motivation

I have added a new message to the thread, but the new message did not appear on the page.

Steps to reproduce

Hard to define steps for reproducing this problem

Proposed resolution

Array_splice can cause unexpected behaviour, the second argument is the offset parameter which denotes the position in the array, not the key.

In https://git.drupalcode.org/project/private_message/-/blob/4.x/src/Servic... we are using the key instead of the position.

Everything is fine as long as the key is increased by 1

$test = [
	0 => '0', 
	1 => '1', 
	2 => '2',
];
var_dump( array_splice($test, 2));

It will return this
array(1) {
[0]=>
string(1) "2"
}

In case one number is skipped, it generates errors

$test = [
	0 => '0', 
	2 => '2',
];

var_dump( array_splice($test, 2));

It will return an empty array. I had a very similar case, but sadly, I do not know how to replicate that

🐛 Bug report
Status

Active

Version

4.0

Component

Code

Created by

🇵🇱Poland alorenc Wolsztyn, 🇵🇱

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024