Undefined array key
-
When working on WPCLI on WP Engine we have been seeing thousands of these notices since we upgraded to PHP8.2.
PHP Warning: Undefined array key "get" in /nas/content/live/haymarketwp/wp-content/object-cache.php on line 348
Warning: Undefined array key "get" in /nas/content/live/haymarketwp/wp-content/object-cache.php on line 348
PHP Warning: Undefined array key "miss" in /nas/content/live/haymarketwp/wp-content/object-cache.php on line 351
Warning: Undefined array key "miss" in /nas/content/live/haymarketwp/wp-content/object-cache.php on line 351
PHP Warning: Undefined array key "add" in /nas/content/live/haymarketwp/wp-content/object-cache.php on line 237
Warning: Undefined array key "add" in /nas/content/live/haymarketwp/wp-content/object-cache.php on line 237The remediation solution is simple and I have patched the file.
At line 236:
if ( false !== $result ) {
$this->stats['add'] = $this->stats['add'] ?? 0;
++$this->stats['add'];
$this->group_ops[$group][] = "add $id";
$this->cache[$key] = $data;
}At line 348:
if ( $found ) {
$this->stats['get'] = $this->stats['get'] ?? 0;
++$this->stats['get'];
$this->group_ops[$group][] = "get $id";
} else {
$this->stats['miss'] = $this->stats['miss'] ?? 0;
++$this->stats['miss'];
}I have confirmed that this plugin is working on all versions up to WordPress 6.5.5 with PHP8.2. The null coalescence in the patch has been available since PHP7.0.
If older backward compatibility is required then replacing each null coalescence with the following condition check will also work.
if ( ! array_key_exists( 'miss', $this->stats ) ) {
$this->stats['miss'] = 0;
}Obviously it would be good to completely unpack the stats array and understand where it should be properly initialized in the first place to eliminate all of this extra safety checking, but I needed an immediate resolution to the bloat of error notices.
Feel free to ping me in wp slack.
Cheers,
Mikel
The page I need help with: [log in to see the link]
- You must be logged in to reply to this topic.