yakpro-po icon indicating copy to clipboard operation
yakpro-po copied to clipboard

PHP Warning: array_merge(): Argument #1 is not an array in include/classes/scrambler.php on line 255

Open sedimentation-fault opened this issue 4 years ago • 0 comments

I get

PHP Warning:  array_merge(): Argument #1 is not an array in include/classes/scrambler.php on line 
255

The line in question contains

$this->t_ignore_prefix = array_merge($this->t_ignore_prefix,$t);

so $this->t_ignore_prefix is not an array at the time array_merge() is called, not even an empty one.

I know this is just a warning and not an error, but reading the docs on array_merge, I see, among others, this comment:

$a = array(1,2,3); // an array $b = 5; // anything but not an array

$c = array_merge($a, $b); // shows a PHP warning: Argument #2 is not an array

var_dump($c); // output as NULL

// now merge in reverse order $d = array_merge($b, $a); // shows a PHP warning: Argument #1 is not an array

var_dump($d); // output as NULL

NOTE: For any operation that relies on the previous array merge operation it is highly necessary to check the arguments as well as the result of the merge are arrays before continuing as the warning would not stop the operation and this might result in data loss and what not... and this should also be stated in the .documentation. Example #3 somewhat started to demonstrate this but used type casting which made this irrelevant to this matter.

You may say, "in our case the result of merging the non-array $this->t_ignore_prefix with $t will still be OK", but relying on such effects is very obscure behavior to me.

Solution

Initialize all arrays to the empty array:

In the constructor of scrambler, at the top, where other $this properties are initialized, add:

        $this->t_ignore_prefix      = array();
        $this->t_ignore             = array();

sedimentation-fault avatar Jul 06 '20 15:07 sedimentation-fault