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
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
- Scaling
- Scales with input
- Instructions
- 6–35
- Plugin surface
- None
- Called by
- 3
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 100.
Nothing here hands control to plugin code.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_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.
| When | Instructions | Calls it makes |
|---|---|---|
!is_array($incoming) | 6 | none |
is_array($incoming) | 15–19 | array_is_list() |
is_array($incoming) && array_is_list() | 16–18 | array_is_list(), ->strip_nulls() |
is_array($incoming) && is_array($current) | 21–25 | array_is_list(), array_is_list() |
is_array($incoming) && array_is_list() && $incoming !== false && !is_array($current) | 22–24 | array_is_list(), ->merge_list_by_identity() |
is_array($incoming) && array_is_list() && $incoming !== false && is_array($current) | 22–24 | array_is_list(), array_is_list(), ->strip_nulls() |
is_array($incoming) && !array_is_list() && is_array($current) | 26–30 | array_is_list(), array_is_list(), array_is_list() |
is_array($incoming) && array_is_list() && $incoming !== false && is_array($current) | 26–30 | array_is_list(), array_is_list(), ->merge_list_by_identity() |
is_array($incoming) && is_array($current) && $current !== false | 26–28 | array_is_list(), array_is_list(), esc_html__(), _doing_it_wrong() |
is_array($incoming) && array_is_list() && $incoming !== false && is_array($current) | 32–35 | array_is_list(), array_is_list(), array_is_list(), ->merge_list_by_identity() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 100 | 6–35 | 19 | |
| 8.5 | 100 | 6–35 | 19 | |
| 8.4 | 100 | 6–35 | 19 | |
| 8.3 | 100 | 6–35 | 19 | |
| 8.2 | 100 | 6–35 | 19 | |
| 8.1 | 100 | 6–35 | 19 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 101 | 6–35 | 19 |
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
- array_is_list()Polyfill for `array_is_list()` function added in PHP 8.1.
- _doing_it_wrong()Marks something as being incorrectly called.
- esc_html__()Retrieves the translation of $text and escapes it for safe use in HTML output.
- WP_View_Config_Data::strip_nulls()Recursively drops every property whose value is `null` from a value.
- WP_View_Config_Data::merge_list_by_identity()Merges an incoming list into the current one by member identity.
- WP_View_Config_Data::merge_properties()Merges an incoming value into the current one, recursing by value shape.
Used by · 3
- WP_View_Config_Data::apply()Applies a patch to the configuration, top-level key by top-level key.
- WP_View_Config_Data::merge_list_by_identity()Merges an incoming list into the current one by member identity.
- WP_View_Config_Data::merge_properties()Merges an incoming value into the current one, recursing by value shape.
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.