wppaste
WordPress

Cookie::parse( string $cookie_header, string $name = '', int|null $reference_time = null ): WpOrg\Requests\Cookie

Source
wp-includes/Requests/src/Cookie.php:416
Parse a cookie string into a cookie object

Description

Based on Mozilla's parsing code in Firefox and related projects, which is an intentional deviation from RFC 2109 and RFC 2616. RFC 6265 specifies some of this handling, but not in a thorough manner.

Compatibility

WordPress
core
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

$namestringoptional
Default: ''
$reference_timeint|nulloptional
Default: null

Return value

WpOrg\Requests\Cookie
Parsed cookie object

Performance profile

How much work a call to Cookie::parse() 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
14–52

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

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 one call costs · 3 distinct outcomes

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

WhenInstructionsCalls it makes
always14–17::InvalidArgument()
always39–46explode(), array_shift()
empty($name)49–52explode(), array_shift(), explode()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev9614–528
8.59614–528
8.49614–52814 fewer instructions than PHP 8.3
8.311014–598
8.211014–598
8.111014–598
7.411014–598

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

  • \WpOrg\Requests\Exception\InvalidArgument::create()
  • \WpOrg\Requests\Utility\CaseInsensitiveDictionary::__construct()
  • static::__construct()

Source code

	public static function parse($cookie_header, $name = '', $reference_time = null) {		if (is_string($cookie_header) === false) {			throw InvalidArgument::create(1, '$cookie_header', 'string', gettype($cookie_header));		} 		if (is_string($name) === false) {			throw InvalidArgument::create(2, '$name', 'string', gettype($name));		} 		$parts   = explode(';', $cookie_header);		$kvparts = array_shift($parts); 		if (!empty($name)) {			$value = $cookie_header;		} elseif (strpos($kvparts, '=') === false) {			// Some sites might only have a value without the equals separator.			// Deviate from RFC 6265 and pretend it was actually a blank name			// (`=foo`)			//			// https://bugzilla.mozilla.org/show_bug.cgi?id=169091			$name  = '';			$value = $kvparts;		} else {			list($name, $value) = explode('=', $kvparts, 2);		} 		$name  = trim($name);		$value = trim($value); 		// Attribute keys are handled case-insensitively		$attributes = new CaseInsensitiveDictionary(); 		if (!empty($parts)) {			foreach ($parts as $part) {				if (strpos($part, '=') === false) {					$part_key   = $part;					$part_value = true;				} else {					list($part_key, $part_value) = explode('=', $part, 2);					$part_value                  = trim($part_value);				} 				$part_key              = trim($part_key);				$attributes[$part_key] = $part_value;			}		} 		return new static($name, $value, $attributes, [], $reference_time);	}

Changelog

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.0.4 tag, from src/wp-includes/Requests/src/Cookie.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.