wppaste
WordPress

WP_Token_Map::from_array( array $mappings, int $key_length = 2 ): WP_Token_Map|null

Since
6.6.0
Source
wp-includes/class-wp-token-map.php:283
Create a token map using an associative array of key/value pairs as the input.

Description

Example:

$smilies = WP_Token_Map::from_array( array(
 '8O' => '😯',
 ':(' => '🙁',
 ':)' => '🙂',
 ':?' => '😕',
 ) );

Compatibility

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

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$mappingsarray
The keys transform into the values, both are strings.
$key_lengthintoptional
Determines the group key length. Leave at the default value of 2 unless there's an empirical reason to change it.Default: 2

Return value

WP_Token_Map|null
Token map, unless unable to create it.

Performance profile

How much work a call to WP_Token_Map::from_array() 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
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
12

Executed per call on PHP 8.5. The body compiles to 148.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksdo_action()one call below WP_Token_Map::from_array()

Further down the call graph this can also reach query, option, cache, serialize 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 · 1 distinct outcome

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

WhenInstructionsCalls it makes
always12::longest_first_then_alphabetical()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1481214
8.51481214
8.414812146 fewer instructions than PHP 8.3
8.31541214
8.21541214
8.1154121412 more instructions than PHP 7.4
7.414230–3514

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

Source code

	public static function from_array( array $mappings, int $key_length = 2 ): ?WP_Token_Map {		$map             = new WP_Token_Map();		$map->key_length = $key_length; 		// Start by grouping words. 		$groups = array();		$shorts = array();		foreach ( $mappings as $word => $mapping ) {			if (				self::MAX_LENGTH <= strlen( $word ) ||				self::MAX_LENGTH <= strlen( $mapping )			) {				_doing_it_wrong(					__METHOD__,					sprintf(						/* translators: 1: maximum byte length (a count) */						__( 'Token Map tokens and substitutions must all be shorter than %1$d bytes.' ),						self::MAX_LENGTH					),					'6.6.0'				);				return null;			} 			$length = strlen( $word ); 			if ( $key_length >= $length ) {				$shorts[] = $word;			} else {				$group = substr( $word, 0, $key_length ); 				if ( ! isset( $groups[ $group ] ) ) {					$groups[ $group ] = array();				} 				$groups[ $group ][] = array( substr( $word, $key_length ), $mapping );			}		} 		/*		 * Sort the words to ensure that no smaller substring of a match masks the full match.		 * For example, `Cap` should not match before `CapitalDifferentialD`.		 */		usort( $shorts, 'WP_Token_Map::longest_first_then_alphabetical' );		foreach ( $groups as $group_key => $group ) {			usort(				$groups[ $group_key ],				static function ( array $a, array $b ): int {					return self::longest_first_then_alphabetical( $a[0], $b[0] );				}			);		} 		// Finally construct the optimized lookups. 		foreach ( $shorts as $word ) {			$map->small_words     .= str_pad( $word, $key_length + 1, "\x00", STR_PAD_RIGHT );			$map->small_mappings[] = $mappings[ $word ];		} 		$group_keys = array_keys( $groups );		sort( $group_keys ); 		foreach ( $group_keys as $group ) {			$map->groups .= "{$group}\x00"; 			$group_string = ''; 			foreach ( $groups[ $group ] as $group_word ) {				list( $word, $mapping ) = $group_word; 				$word_length    = pack( 'C', strlen( $word ) );				$mapping_length = pack( 'C', strlen( $mapping ) );				$group_string  .= "{$word_length}{$word}{$mapping_length}{$mapping}";			} 			$map->large_words[] = $group_string;		}

Changelog

Introduced in 6.6.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/class-wp-token-map.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.