Skip to content

Commit

Permalink
Use false rather than null when a parser subtree doesn't match
Browse files Browse the repository at this point in the history
  • Loading branch information
JanJakes committed Nov 12, 2024
1 parent 96dd467 commit 205e919
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions wp-includes/parser/class-wp-parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public function __construct( WP_Parser_Grammar $grammar, array $tokens ) {
public function parse() {
// @TODO: Make the starting rule lookup non-grammar-specific.
$query_rule_id = $this->grammar->get_rule_id( 'query' );
return $this->parse_recursive( $query_rule_id );
return $this->parse_recursive( $query_rule_id ) ?: null;
}

private function parse_recursive( $rule_id ) {
$is_terminal = $rule_id <= $this->grammar->highest_terminal_id;
if ( $is_terminal ) {
if ( $this->position >= count( $this->tokens ) ) {
return null;
return false;
}

if ( WP_Parser_Grammar::EMPTY_RULE_ID === $rule_id ) {
Expand All @@ -40,12 +40,12 @@ private function parse_recursive( $rule_id ) {
++$this->position;
return $this->tokens[ $this->position - 1 ];
}
return null;
return false;
}

$branches = $this->grammar->rules[ $rule_id ];
if ( ! count( $branches ) ) {
return null;
return false;
}

// Bale out from processing the current branch if none of its rules can
Expand All @@ -56,7 +56,7 @@ private function parse_recursive( $rule_id ) {
! isset( $this->grammar->lookahead_is_match_possible[ $rule_id ][ $token_id ] ) &&
! isset( $this->grammar->lookahead_is_match_possible[ $rule_id ][ WP_Parser_Grammar::EMPTY_RULE_ID ] )
) {
return null;
return false;
}
}

Expand All @@ -68,7 +68,7 @@ private function parse_recursive( $rule_id ) {
$branch_matches = true;
foreach ( $branch as $subrule_id ) {
$subnode = $this->parse_recursive( $subrule_id );
if ( null === $subnode ) {
if ( false === $subnode ) {
$branch_matches = false;
break;
} elseif ( true === $subnode ) {
Expand Down Expand Up @@ -111,7 +111,7 @@ private function parse_recursive( $rule_id ) {

if ( ! $branch_matches ) {
$this->position = $starting_position;
return null;
return false;
}

if ( 0 === count( $node->children ) ) {
Expand Down

0 comments on commit 205e919

Please sign in to comment.