wppaste
WordPress

WP_REST_View_Config_Controller::cast_empty_objects( mixed $value, array $schema ): mixed

Since
7.1.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-view-config-controller.php:190
Recursively casts empty arrays to objects where the schema types them as objects.

Description

PHP cannot distinguish an empty associative array from an empty list, so json_encode() always serializes array() as a JSON array ([]). The REST schema, however, types several values as objects, which must encode as {}. This walks the value against its schema and casts any empty, object-typed array to an object. Non-empty associative arrays already encode as objects, so they are left as arrays and only recursed into to fix any nested empty objects.

Union schemas (oneOf/anyOf) are handled only for the empty-array case: an empty value is cast to an object when any branch allows an object. Such values are not recursed into, which is sufficient for the form schema where they never contain empty nested objects.

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

$valuemixed
The value to normalize.
$schemaarray
The schema node describing the value.

Return value

mixed
The normalized value, with empty object-typed arrays cast to objects.

Performance profile

How much work a call to WP_REST_View_Config_Controller::cast_empty_objects() 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
5–40

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
2

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

What one call costs · 1 distinct outcome

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

WhenInstructionsCalls it makes
always5–40none

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1005–4027
8.51005–4027
8.41005–40279 fewer instructions than PHP 8.3
8.31095–4627
8.21095–4627
8.11095–46274 fewer instructions than PHP 7.4
7.41135–4627

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 · 1

Used by · 2

Source code

	protected function cast_empty_objects( $value, $schema ) {		if ( ! is_array( $value ) || ! is_array( $schema ) ) {			return $value;		} 		if ( isset( $schema['oneOf'] ) || isset( $schema['anyOf'] ) ) {			$branches = $schema['oneOf'] ?? $schema['anyOf'];			if ( array() === $value ) {				foreach ( $branches as $branch ) {					if ( is_array( $branch ) && in_array( 'object', (array) ( $branch['type'] ?? array() ), true ) ) {						return (object) array();					}				}			}			return $value;		} 		$types = (array) ( $schema['type'] ?? array() ); 		if ( in_array( 'array', $types, true ) && isset( $schema['items'] ) ) {			foreach ( $value as $index => $item ) {				$value[ $index ] = $this->cast_empty_objects( $item, $schema['items'] );			}			return $value;		} 		if ( in_array( 'object', $types, true ) ) {			if ( isset( $schema['properties'] ) ) {				foreach ( $schema['properties'] as $property => $property_schema ) {					if ( array_key_exists( $property, $value ) ) {						$value[ $property ] = $this->cast_empty_objects( $value[ $property ], $property_schema );					}				}			}			if ( isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] ) ) {				foreach ( $value as $key => $item ) {					if ( isset( $schema['properties'][ $key ] ) ) {						continue;					}					$value[ $key ] = $this->cast_empty_objects( $item, $schema['additionalProperties'] );				}			} 			// Empty object-typed arrays must serialize as {} to match the schema.			if ( array() === $value ) {				return (object) array();			}		} 		return $value;	}

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/rest-api/endpoints/class-wp-rest-view-config-controller.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.