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
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
- Scaling
- Scales with input
- Instructions
- 5–40
- Plugin surface
- None
- Called by
- 2
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.
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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 5–40 | none |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 100 | 5–40 | 27 | |
| 8.5 | 100 | 5–40 | 27 | |
| 8.4 | 100 | 5–40 | 27 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 109 | 5–46 | 27 | |
| 8.2 | 109 | 5–46 | 27 | |
| 8.1 | 109 | 5–46 | 27 | 4 fewer instructions than PHP 7.4 |
| 7.4 | 113 | 5–46 | 27 |
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
- WP_REST_View_Config_Controller::cast_empty_objects()Recursively casts empty arrays to objects where the schema types them as objects.
Used by · 2
- WP_REST_View_Config_Controller::cast_empty_objects()Recursively casts empty arrays to objects where the schema types them as objects.
- WP_REST_View_Config_Controller::get_items()Returns the default view configuration for the given entity type.
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.