{"id":17986049,"date":"2024-08-30T18:19:35","date_gmt":"2024-08-30T18:19:35","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/undefined-array-key-49\/"},"modified":"2024-08-30T18:19:35","modified_gmt":"2024-08-30T18:19:35","slug":"undefined-array-key-49","status":"publish","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/undefined-array-key-49\/","title":{"rendered":"Undefined array key"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">When working on WPCLI on WP Engine we have been seeing thousands of these notices since we upgraded to PHP8.2. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>PHP Warning: Undefined array key \"get\" in \/nas\/content\/live\/haymarketwp\/wp-content\/object-cache.php on line 348<br \/>Warning: Undefined array key \"get\" in \/nas\/content\/live\/haymarketwp\/wp-content\/object-cache.php on line 348<br \/>PHP Warning: Undefined array key \"miss\" in \/nas\/content\/live\/haymarketwp\/wp-content\/object-cache.php on line 351<br \/>Warning: Undefined array key \"miss\" in \/nas\/content\/live\/haymarketwp\/wp-content\/object-cache.php on line 351<br \/>PHP Warning: Undefined array key \"add\" in \/nas\/content\/live\/haymarketwp\/wp-content\/object-cache.php on line 237<br \/>Warning: Undefined array key \"add\" in \/nas\/content\/live\/haymarketwp\/wp-content\/object-cache.php on line 237<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The remediation solution is simple and I have patched the file. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">At line 236:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\tif ( false !== $result ) {<br \/>            $this-&gt;stats&#091;'add'] = $this-&gt;stats&#091;'add'] ?? 0;<br \/>\t\t\t++$this-&gt;stats&#091;'add'];<br \/>\t\t\t$this-&gt;group_ops&#091;$group]&#091;] = \"add $id\";<br \/>\t\t\t$this-&gt;cache&#091;$key] = $data;<br \/>\t\t}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">At line 348:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\tif ( $found ) {<br \/>            $this-&gt;stats&#091;'get'] = $this-&gt;stats&#091;'get'] ?? 0;<br \/>\t\t\t++$this-&gt;stats&#091;'get'];<br \/>\t\t\t$this-&gt;group_ops&#091;$group]&#091;] = \"get $id\";<br \/>\t\t} else {<br \/>            $this-&gt;stats&#091;'miss'] = $this-&gt;stats&#091;'miss'] ?? 0;<br \/>\t\t\t++$this-&gt;stats&#091;'miss'];<br \/>\t\t}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">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. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If older backward compatibility is required then replacing each null coalescence with the following condition check will also work.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>            if ( ! array_key_exists( 'miss', $this-&gt;stats ) ) {<br \/>                $this-&gt;stats&#091;'miss'] = 0;<br \/>            }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to ping me in wp slack.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Cheers,<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Mikel<\/p>\n","protected":false},"template":"","class_list":["post-17986049","topic","type-topic","status-publish","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/17986049","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/17986049\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=17986049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}