Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 293850

Null values when recursively merging arrays should be ignored

$
0
0

Problem/Motivation

NULL is PHP's unit type. In PHP, and in other languages, it is not always clear how programs should treat NULL values. Do they indicate the absence of value? Or do they have a value that does not hold any information?

In some ways, it is the former. For example, from the manual page:

Undefined, and unset() variables will resolve to the value null.

Or, the following both return true i.e. the absence of a value equals a NULL value:

echo is_null([]['a']);
echo is_null(['a' => NULL]['a']);

void is a separate, return-only, type that indicates absence of value, but still functions that declare to not return a value still return NULL i.e. NULL is treated as absence of value.

Many developers and programs perceive it this way.

When recursively merging arrays using the NestedArray utility, NULL values override other values. This may be unexpected behavior in many cases and it can lead to errors.

I encountered this when merging arrays containing plugin configuration and NULL values where overriding real values. In my use case this was unexpected behavior that caused errors in the final behavior of the program.

Steps to reproduce

$array1 = ['color' => 'white', 'size' => 'large'];
$array2 = ['color' => 'blue'];
dump($array1, $array2);

The result has a large size.

$array1 = ['color' => 'white', 'size' => 'large'];
$array2 = ['color' => 'blue', 'size' => NULL];
dump($array1, $array2);

The result has a NULL size.

Proposed resolution

We don't want to break current behavior of the mergeDeepArray function, and there may be cases where having a NULL value might indeed be interpreted as an instruction to unset the value provided by the previous array.

I would therefore propose to add an extra argument, with a default value that keeps the current behavior:

public static function mergeDeepArray(array $arrays, $preserve_integer_keys = FALSE, $preserve_null_values = TRUE) {

Remaining tasks

Review usage of mergeDeepArray in core and determine whether it should be called with the new argument set to FALSE.

User interface changes

API changes

Data model changes

Release notes snippet


Viewing all articles
Browse latest Browse all 293850

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>