Commit 9449e6fe4e for wordpress.org
commit 9449e6fe4eb15a277505020e89a58edba720e4d4
Author: westonruter <westonruter@git.wordpress.org>
Date: Fri Sep 25 19:24:50 2026 +0000
Code Quality: Restore `count()` indexing in block parser.
In `WP_Block_Parser::add_inner_block()` the last stack frame is once again read as `$this->stack[ count( $this->stack ) - 1 ]`, undoing the switch to `array_key_last()` made in r62956 and removing the inline comment added in r63624 that explained why `array_last()` is not used. Indexing by `array_key_last()` gives the misleading impression that `$this->stack` could be an associative array, when it is only ever a sequentially indexed list. The original code was already correct, and both callers ensure the stack is not empty.
The `$stack` property is now also documented with `@phpstan-var list<WP_Block_Parser_Frame>`, which tells static analysis that the stack is always sequentially indexed. With that in place, there is nothing prompting a switch to `array_key_last()` or `array_last()`, so the explanatory comment is no longer needed. This mirrors the change made to the parser in Gutenberg via https://github.com/WordPress/gutenberg/pull/82695.
Developed in https://github.com/WordPress/wordpress-develop/pull/13487.
Follow-up to r62956, r63624.
Props mukesh27, westonruter, dmsnell, soean.
See #65818, #65817.
Built from https://develop.svn.wordpress.org/trunk@63937
git-svn-id: http://core.svn.wordpress.org/trunk@63103 1a063a9b-81f0-0310-95a4-ce76da25c4cd
diff --git a/wp-includes/class-wp-block-parser.php b/wp-includes/class-wp-block-parser.php
index 81f9a42c88..3090c238eb 100644
--- a/wp-includes/class-wp-block-parser.php
+++ b/wp-includes/class-wp-block-parser.php
@@ -45,6 +45,7 @@ class WP_Block_Parser {
*
* @since 5.0.0
* @var WP_Block_Parser_Frame[]
+ * @phpstan-var list<WP_Block_Parser_Frame>
*/
public $stack;
@@ -346,14 +347,7 @@ class WP_Block_Parser {
* @param int|null $last_offset Last byte offset into document if continuing form earlier output.
*/
public function add_inner_block( WP_Block_Parser_Block $block, $token_start, $token_length, $last_offset = null ) {
- /*
- * Note: `array_last()` is intentionally not used here. It returns `null` for an
- * empty array, which makes static analysis flag the property accesses below.
- * Guarding against that is not possible without changing behavior: both callers
- * already ensure the stack is not empty, so the guard would be unreachable, and
- * this is a public method on a class that can be replaced via `block_parser_class`.
- */
- $parent = $this->stack[ array_key_last( $this->stack ) ];
+ $parent = $this->stack[ count( $this->stack ) - 1 ];
$parent->block->innerBlocks[] = (array) $block;
$html = substr( $this->document, $parent->prev_offset, $token_start - $parent->prev_offset );
diff --git a/wp-includes/version.php b/wp-includes/version.php
index 1b995c67e7..ae8ae5c6c1 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
-$wp_version = '7.2-alpha-63936';
+$wp_version = '7.2-alpha-63937';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.