Skip to content

Commit 50f4cdd

Browse files
committed
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 WordPress/gutenberg#82695. Developed in #13487. Follow-up to r62956, r63624. Props mukesh27, westonruter, dmsnell, soean. See #65818, #65817. git-svn-id: https://develop.svn.wordpress.org/trunk@63937 602fd350-edb4-49c9-b593-d223f7449a82
1 parent e2f5452 commit 50f4cdd

1 file changed

Lines changed: 2 additions & 8 deletions

File tree

‎src/wp-includes/class-wp-block-parser.php‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class WP_Block_Parser {
4545
*
4646
* @since 5.0.0
4747
* @var WP_Block_Parser_Frame[]
48+
* @phpstan-var list<WP_Block_Parser_Frame>
4849
*/
4950
public $stack;
5051

@@ -346,14 +347,7 @@ public function add_freeform( $length = null ) {
346347
* @param int|null $last_offset Last byte offset into document if continuing form earlier output.
347348
*/
348349
public function add_inner_block( WP_Block_Parser_Block $block, $token_start, $token_length, $last_offset = null ) {
349-
/*
350-
* Note: `array_last()` is intentionally not used here. It returns `null` for an
351-
* empty array, which makes static analysis flag the property accesses below.
352-
* Guarding against that is not possible without changing behavior: both callers
353-
* already ensure the stack is not empty, so the guard would be unreachable, and
354-
* this is a public method on a class that can be replaced via `block_parser_class`.
355-
*/
356-
$parent = $this->stack[ array_key_last( $this->stack ) ];
350+
$parent = $this->stack[ count( $this->stack ) - 1 ];
357351
$parent->block->innerBlocks[] = (array) $block;
358352
$html = substr( $this->document, $parent->prev_offset, $token_start - $parent->prev_offset );
359353

0 commit comments

Comments
 (0)