Skip to content

Commit d82f45c

Browse files
Coding Standards: Use array_last() to get the last array element.
This commit replaces `$array[ count( $array ) - 1 ]` constructs with the `array_last()` function (added in PHP 8.5 and polyfilled as of WordPress 6.9) for improved readability. Follow-up to [60672]. Props Soean, westonruter. See #64897. git-svn-id: https://develop.svn.wordpress.org/trunk@62956 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 0fa1497 commit d82f45c

6 files changed

Lines changed: 13 additions & 11 deletions

File tree

‎src/wp-admin/includes/ajax-actions.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ function wp_ajax_ajax_tag_search() {
128128

129129
if ( str_contains( $search, ',' ) ) {
130130
$search = explode( ',', $search );
131-
$search = $search[ count( $search ) - 1 ];
131+
$search = array_last( $search );
132132
}
133133

134134
$search = trim( $search );

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ public function add_freeform( $length = null ) {
342342
* @param int|null $last_offset Last byte offset into document if continuing form earlier output.
343343
*/
344344
public function add_inner_block( WP_Block_Parser_Block $block, $token_start, $token_length, $last_offset = null ) {
345-
$parent = $this->stack[ count( $this->stack ) - 1 ];
345+
$parent = $this->stack[ array_key_last( $this->stack ) ];
346346
$parent->block->innerBlocks[] = (array) $block;
347347
$html = substr( $this->document, $parent->prev_offset, $token_start - $parent->prev_offset );
348348

‎src/wp-includes/class-wp-query.php‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2430,11 +2430,13 @@ public function get_posts() {
24302430

24312431
if ( '' !== $query_vars['author_name'] ) {
24322432
if ( str_contains( $query_vars['author_name'], '/' ) ) {
2433-
$query_vars['author_name'] = explode( '/', $query_vars['author_name'] );
2434-
if ( $query_vars['author_name'][ count( $query_vars['author_name'] ) - 1 ] ) {
2435-
$query_vars['author_name'] = $query_vars['author_name'][ count( $query_vars['author_name'] ) - 1 ]; // No trailing slash.
2433+
$author_name_parts = explode( '/', $query_vars['author_name'] );
2434+
$last_part = array_last( $author_name_parts );
2435+
2436+
if ( $last_part ) {
2437+
$query_vars['author_name'] = $last_part; // No trailing slash.
24362438
} else {
2437-
$query_vars['author_name'] = $query_vars['author_name'][ count( $query_vars['author_name'] ) - 2 ]; // There was a trailing slash.
2439+
$query_vars['author_name'] = $author_name_parts[ count( $author_name_parts ) - 2 ]; // There was a trailing slash.
24382440
}
24392441
}
24402442
$query_vars['author_name'] = sanitize_title_for_query( $query_vars['author_name'] );

‎src/wp-includes/class-wp-theme-json.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3994,7 +3994,7 @@ public function get_styles_for_block( $block_metadata ) {
39943994
*/
39953995
$is_processing_element = in_array( 'elements', $block_metadata['path'], true );
39963996

3997-
$current_element = $is_processing_element ? $block_metadata['path'][ count( $block_metadata['path'] ) - 1 ] : null;
3997+
$current_element = $is_processing_element ? array_last( $block_metadata['path'] ) : null;
39983998

39993999
$element_pseudo_allowed = array();
40004000

@@ -4688,7 +4688,7 @@ public static function remove_insecure_properties( $theme_json, $origin = 'theme
46884688
* Get a reference to element name from path.
46894689
* $metadata['path'] = array( 'styles', 'elements', 'link' );
46904690
*/
4691-
$current_element = $metadata['path'][ count( $metadata['path'] ) - 1 ];
4691+
$current_element = array_last( $metadata['path'] );
46924692

46934693
/*
46944694
* $output is stripped of pseudo selectors. Re-add and process them

‎src/wp-includes/pomo/plural-forms.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ protected function parse( $str ) {
128128
case ')':
129129
$found = false;
130130
while ( ! empty( $stack ) ) {
131-
$o2 = $stack[ count( $stack ) - 1 ];
131+
$o2 = array_last( $stack );
132132
if ( '(' !== $o2 ) {
133133
$output[] = array( 'op', array_pop( $stack ) );
134134
continue;
@@ -163,7 +163,7 @@ protected function parse( $str ) {
163163
}
164164

165165
while ( ! empty( $stack ) ) {
166-
$o2 = $stack[ count( $stack ) - 1 ];
166+
$o2 = array_last( $stack );
167167

168168
// Ternary is right-associative in C.
169169
if ( '?:' === $operator || '?' === $operator ) {

‎src/wp-trackback.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function trackback_response( $error = 0, $error_message = '' ) {
5050

5151
if ( ! isset( $_GET['tb_id'] ) || ! $_GET['tb_id'] ) {
5252
$post_id = explode( '/', $_SERVER['REQUEST_URI'] );
53-
$post_id = (int) $post_id[ count( $post_id ) - 1 ];
53+
$post_id = (int) array_last( $post_id );
5454
}
5555

5656
$trackback_url = isset( $_POST['url'] ) ? sanitize_url( $_POST['url'] ) : '';

0 commit comments

Comments
 (0)