wppaste
WordPress

WP_View_Config_Data::merge_properties( mixed $current, mixed $incoming, bool $replace_lists ): mixed

Since
7.1.0
Source
wp-includes/class-wp-view-config-data.php:515
Merges an incoming value into the current one, recursing by value shape.

Description

This is the core of the merge algorithm and is applied at every nesting level: a scalar (or null) in $incoming replaces $current outright, an associative array merges key by key (recursing here for each key, with a null value deleting that key), and a list either replaces $current wholesale ($replace_lists) or merges into it by member identity. The $replace_lists flag is carried down through associative nesting so that, under replace(), every list reached along the way is swapped wholesale.

An array in $incoming only merges into a current value of the same shape.
A non-empty mismatch — an associative array where a list lives, or a non-empty list where an associative value lives — is reported with _doing_it_wrong() and leaves the current value unchanged, so a malformed patch cannot silently destroy configuration. An empty array is shape-ambiguous and merges nothing, so it is a no-op: clearing a list is spelled replace() with an empty list, and resetting a key is spelled null.

Compatibility

WordPress
since 7.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 1 of the 5 tracked releases, added in 7.1.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$currentmixed
The current value.
$incomingmixed
The incoming value.
$replace_listsbool
Whether a list in $incoming replaces the current list wholesale instead of merging into it by member identity.

Return value

mixed
The merged value.

Performance profile

How much work a call to WP_View_Config_Data::merge_properties() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
6–35

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 100.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
3

3 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksdo_action()one call below WP_View_Config_Data::merge_properties()

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 10 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_View_Config_Data::merge_properties() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!is_array($incoming)6none
is_array($incoming)15–19array_is_list()
is_array($incoming) && array_is_list()16–18array_is_list(), ->strip_nulls()
is_array($incoming) && is_array($current)21–25array_is_list(), array_is_list()
is_array($incoming) && array_is_list() && $incoming !== false && !is_array($current)22–24array_is_list(), ->merge_list_by_identity()
is_array($incoming) && array_is_list() && $incoming !== false && is_array($current)22–24array_is_list(), array_is_list(), ->strip_nulls()
is_array($incoming) && !array_is_list() && is_array($current)26–30array_is_list(), array_is_list(), array_is_list()
is_array($incoming) && array_is_list() && $incoming !== false && is_array($current)26–30array_is_list(), array_is_list(), ->merge_list_by_identity()
is_array($incoming) && is_array($current) && $current !== false26–28array_is_list(), array_is_list(), esc_html__(), _doing_it_wrong()
is_array($incoming) && array_is_list() && $incoming !== false && is_array($current)32–35array_is_list(), array_is_list(), array_is_list(), ->merge_list_by_identity()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1006–3519
8.51006–3519
8.41006–3519
8.31006–3519
8.21006–3519
8.11006–35191 fewer instruction than PHP 7.4
7.41016–3519

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 6

Used by · 3

Source code

	private function merge_properties( $current, $incoming, $replace_lists ) {		// Scalar properties are merged as-is.		if ( ! is_array( $incoming ) ) {			return $incoming;		} 		// Numerical indexed arrays are expected to be lists (sequential integer keys starting at 0).		if ( array_is_list( $incoming ) ) {			// A non-empty list only lands where a list (or nothing) lives, under			// merge() and replace() alike. An empty array is shape-ambiguous and			// exempt, so replace() with an empty list can still clear a list.			if ( array() !== $incoming && is_array( $current ) && ! array_is_list( $current ) && array() !== $current ) {				_doing_it_wrong(					__METHOD__,					esc_html__( 'A view configuration patch value must match the shape of the value it patches: a list merges into a list, and an associative array into an associative array.' ),					'7.1.0'				);				return $current;			} 			// replace() takes an incoming list as-is; merge() merges it by member identity.			if ( $replace_lists ) {				// As-is except for nulls: a list swapped in wholesale has no				// existing leaf for a null to delete (the same rationale as				// set()), so a null member is dropped rather than stored.				return $this->strip_nulls( $incoming );			} 			// An empty list has no members to merge, and an empty array is			// shape-ambiguous, so merging one is a no-op rather than a reset.			if ( array() === $incoming ) {				return $current;			} 			return $this->merge_list_by_identity(				is_array( $current ) && array_is_list( $current ) ? $current : array(),				$incoming			);		} 		// Consider any other array as associative (keys are strings).		if ( is_array( $current ) && array_is_list( $current ) && array() !== $current ) {			_doing_it_wrong(				__METHOD__,				esc_html__( 'A view configuration patch value must match the shape of the value it patches: a list merges into a list, and an associative array into an associative array.' ),				'7.1.0'			);			return $current;		} 		$result = is_array( $current ) && ! array_is_list( $current ) ? $current : array();		foreach ( $incoming as $key => $value ) {			// A null patch value deletes the property.			if ( null === $value ) {				unset( $result[ $key ] );				continue;			} 			$result[ $key ] = $this->merge_properties(				array_key_exists( $key, $result ) ? $result[ $key ] : array(),				$value,				$replace_lists			);		} 		return $result;	}

Changelog

Introduced in 7.1.0.

Signature, return type and hooks compared across 1 parsed release.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/class-wp-view-config-data.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.