Skip to content

Commit

Permalink
Coding Standards: Use static closures when not using $this.
Browse files Browse the repository at this point in the history
When a closure does not use `$this`, it can be made `static` for improved performance.

Static closures are supported in PHP since PHP 5.4. ​

Props jrf, hellofromTonya, swissspidy, SergeyBiryukov.
See #53359.

git-svn-id: https://develop.svn.wordpress.org/trunk@51657 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
hellofromtonya committed Aug 26, 2021
1 parent d87fa56 commit e83a341
Show file tree
Hide file tree
Showing 54 changed files with 95 additions and 95 deletions.
2 changes: 1 addition & 1 deletion src/wp-admin/edit-form-blocks.php
Expand Up @@ -31,7 +31,7 @@
// Default to is-fullscreen-mode to avoid jumps in the UI.
add_filter(
'admin_body_class',
function( $classes ) {
static function( $classes ) {
return "$classes is-fullscreen-mode";
}
);
Expand Down
2 changes: 1 addition & 1 deletion src/wp-admin/includes/class-wp-community-events.php
Expand Up @@ -474,7 +474,7 @@ protected function trim_events( array $events ) {

$future_wordcamps = array_filter(
$future_events,
function( $wordcamp ) {
static function( $wordcamp ) {
return 'wordcamp' === $wordcamp['type'];
}
);
Expand Down
Expand Up @@ -41,7 +41,7 @@ public function run_tests() {

$tests = array_filter( $tests );
$tests = array_map(
function( $test ) {
static function( $test ) {
$test = (object) $test;

if ( empty( $test->severity ) ) {
Expand Down
2 changes: 1 addition & 1 deletion src/wp-admin/includes/update-core.php
Expand Up @@ -1658,7 +1658,7 @@ function _upgrade_422_find_genericons_files_in_folder( $directory ) {
$dirs = glob( $directory . '*', GLOB_ONLYDIR );
$dirs = array_filter(
$dirs,
function( $dir ) {
static function( $dir ) {
// Skip any node_modules directories.
return false === strpos( $dir, 'node_modules' );
}
Expand Down
2 changes: 1 addition & 1 deletion src/wp-admin/options-privacy.php
Expand Up @@ -20,7 +20,7 @@

add_filter(
'admin_body_class',
function( $body_class ) {
static function( $body_class ) {
$body_class .= ' privacy-settings ';

return $body_class;
Expand Down
2 changes: 1 addition & 1 deletion src/wp-admin/privacy-policy-guide.php
Expand Up @@ -19,7 +19,7 @@

add_filter(
'admin_body_class',
function( $body_class ) {
static function( $body_class ) {
$body_class .= ' privacy-settings ';

return $body_class;
Expand Down
Expand Up @@ -186,7 +186,7 @@ public static function register( $wp_customize ) {
'settings' => 'accent_hue',
'description' => __( 'Apply a custom color for links, buttons, featured images.', 'twentytwenty' ),
'mode' => 'hue',
'active_callback' => function() use ( $wp_customize ) {
'active_callback' => static function() use ( $wp_customize ) {
return ( 'custom' === $wp_customize->get_setting( 'accent_hue_active' )->value() );
},
)
Expand Down
Expand Up @@ -92,7 +92,7 @@ public function register( $wp_customize ) {
array(
'capability' => 'edit_theme_options',
'default' => 'excerpt',
'sanitize_callback' => function( $value ) {
'sanitize_callback' => static function( $value ) {
return 'excerpt' === $value || 'full' === $value ? $value : 'excerpt';
},
)
Expand Down
Expand Up @@ -153,7 +153,7 @@ public function customizer_controls( $wp_customize ) {
array(
'section' => 'colors',
'priority' => 100,
'active_callback' => function() {
'active_callback' => static function() {
return 127 >= Twenty_Twenty_One_Custom_Colors::get_relative_luminance_from_hex( get_theme_mod( 'background_color', 'D1E4DD' ) );
},
)
Expand All @@ -165,7 +165,7 @@ public function customizer_controls( $wp_customize ) {
array(
'capability' => 'edit_theme_options',
'default' => false,
'sanitize_callback' => function( $value ) {
'sanitize_callback' => static function( $value ) {
return (bool) $value;
},
)
Expand All @@ -188,7 +188,7 @@ public function customizer_controls( $wp_customize ) {
'label' => esc_html__( 'Dark Mode support', 'twentytwentyone' ),
'priority' => 110,
'description' => $description,
'active_callback' => function( $value ) {
'active_callback' => static function( $value ) {
return 127 < Twenty_Twenty_One_Custom_Colors::get_relative_luminance_from_hex( get_theme_mod( 'background_color', 'D1E4DD' ) );
},
)
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/block-supports/duotone.php
Expand Up @@ -361,7 +361,7 @@ function wp_render_duotone_support( $block_content, $block ) {

$selectors = explode( ',', $duotone_support );
$selectors_scoped = array_map(
function ( $selector ) use ( $duotone_id ) {
static function ( $selector ) use ( $duotone_id ) {
return '.' . $duotone_id . ' ' . trim( $selector );
},
$selectors
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/block-supports/layout.php
Expand Up @@ -131,7 +131,7 @@ function wp_restore_group_inner_container( $block_content, $block ) {
$replace_regex = '/(^\s*<div\b[^>]*wp-block-group[^>]*>)(.*)(<\/div>\s*$)/ms';
$updated_content = preg_replace_callback(
$replace_regex,
function( $matches ) {
static function( $matches ) {
return $matches[1] . '<div class="wp-block-group__inner-container">' . $matches[2] . '</div>' . $matches[3];
},
$block_content
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/block-template.php
Expand Up @@ -124,7 +124,7 @@ function resolve_block_template( $template_type, $template_hierarchy ) {

usort(
$templates,
function ( $template_a, $template_b ) use ( $slug_priorities ) {
static function ( $template_a, $template_b ) use ( $slug_priorities ) {
return $slug_priorities[ $template_a->slug ] - $slug_priorities[ $template_b->slug ];
}
);
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/class-wp-theme-json.php
Expand Up @@ -636,7 +636,7 @@ private static function to_ruleset( $selector, $declarations ) {

$declaration_block = array_reduce(
$declarations,
function ( $carry, $element ) {
static function ( $carry, $element ) {
return $carry .= $element['name'] . ': ' . $element['value'] . ';'; },
''
);
Expand Down
4 changes: 2 additions & 2 deletions src/wp-includes/formatting.php
Expand Up @@ -3113,7 +3113,7 @@ function wp_rel_nofollow( $text ) {
$text = stripslashes( $text );
$text = preg_replace_callback(
'|<a (.+?)>|i',
function( $matches ) {
static function( $matches ) {
return wp_rel_callback( $matches, 'nofollow' );
},
$text
Expand Down Expand Up @@ -3147,7 +3147,7 @@ function wp_rel_ugc( $text ) {
$text = stripslashes( $text );
$text = preg_replace_callback(
'|<a (.+?)>|i',
function( $matches ) {
static function( $matches ) {
return wp_rel_callback( $matches, 'nofollow ugc' );
},
$text
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/rest-api.php
Expand Up @@ -3227,7 +3227,7 @@ function rest_get_endpoint_args_for_schema( $schema, $method = WP_REST_Server::C
function rest_convert_error_to_response( $error ) {
$status = array_reduce(
$error->get_all_error_data(),
function ( $status, $error_data ) {
static function ( $status, $error_data ) {
return is_array( $error_data ) && isset( $error_data['status'] ) ? $error_data['status'] : $status;
},
500
Expand Down
Expand Up @@ -597,7 +597,7 @@ public function get_fields_for_response( $request ) {
// Return the list of all requested fields which appear in the schema.
return array_reduce(
$requested_fields,
function( $response_fields, $field ) use ( $fields ) {
static function( $response_fields, $field ) use ( $fields ) {
if ( in_array( $field, $fields, true ) ) {
$response_fields[] = $field;
return $response_fields;
Expand Down
Expand Up @@ -381,15 +381,15 @@ public function create_item( $request ) {
$installed_locales = apply_filters( 'plugins_update_check_locales', $installed_locales );

$language_packs = array_map(
function( $item ) {
static function( $item ) {
return (object) $item;
},
$api->language_packs
);

$language_packs = array_filter(
$language_packs,
function( $pack ) use ( $installed_locales ) {
static function( $pack ) use ( $installed_locales ) {
return in_array( $pack->language, $installed_locales, true );
}
);
Expand Down
Expand Up @@ -85,7 +85,7 @@ public function register_routes() {
'form_data' => array(
'description' => __( 'Serialized widget form data to encode into instance settings.' ),
'type' => 'string',
'sanitize_callback' => function( $string ) {
'sanitize_callback' => static function( $string ) {
$array = array();
wp_parse_str( $string, $array );
return $array;
Expand Down
Expand Up @@ -792,7 +792,7 @@ public function get_item_schema() {
'type' => 'string',
'context' => array(),
'arg_options' => array(
'sanitize_callback' => function( $string ) {
'sanitize_callback' => static function( $string ) {
$array = array();
wp_parse_str( $string, $array );
return $array;
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/script-loader.php
Expand Up @@ -2692,7 +2692,7 @@ function wp_maybe_inline_styles() {
// Reorder styles array based on size.
usort(
$styles,
function( $a, $b ) {
static function( $a, $b ) {
return ( $a['size'] <= $b['size'] ) ? -1 : 1;
}
);
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/update.php
Expand Up @@ -517,7 +517,7 @@ function wp_update_plugins( $extra_stats = array() ) {
}
}

$sanitize_plugin_update_payload = function( &$item ) {
$sanitize_plugin_update_payload = static function( &$item ) {
$item = (object) $item;

unset( $item->translations, $item->compatibility );
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/user.php
Expand Up @@ -3459,7 +3459,7 @@ function wp_user_personal_data_exporter( $email_address ) {
// Remove items that use reserved names.
$extra_data = array_filter(
$_extra_data,
function( $item ) use ( $reserved_names ) {
static function( $item ) use ( $reserved_names ) {
return ! in_array( $item['name'], $reserved_names, true );
}
);
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/includes/abstract-testcase.php
Expand Up @@ -719,14 +719,14 @@ public function assertDiscardWhitespace( $expected, $actual, $message = '' ) {
public function assertSameIgnoreEOL( $expected, $actual, $message = '' ) {
$expected = map_deep(
$expected,
function ( $value ) {
static function ( $value ) {
return str_replace( "\r\n", "\n", $value );
}
);

$actual = map_deep(
$actual,
function ( $value ) {
static function ( $value ) {
return str_replace( "\r\n", "\n", $value );
}
);
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/includes/plural-form-function.php
Expand Up @@ -7,7 +7,7 @@
* @param string $expression
*/
function tests_make_plural_form_function( $nplurals, $expression ) {
$closure = function ( $n ) use ( $nplurals, $expression ) {
$closure = static function ( $n ) use ( $nplurals, $expression ) {
$expression = str_replace( 'n', $n, $expression );

// phpcs:ignore Squiz.PHP.Eval -- This is test code, not production.
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/actions/closures.php
Expand Up @@ -12,7 +12,7 @@ class Tests_Actions_Closures extends WP_UnitTestCase {
*/
function test_action_closure() {
$tag = 'test_action_closure';
$closure = function( $a, $b ) {
$closure = static function( $a, $b ) {
$GLOBALS[ $a ] = $b;
};
add_action( $tag, $closure, 10, 2 );
Expand All @@ -25,7 +25,7 @@ function test_action_closure() {
$this->assertSame( $GLOBALS[ $context[0] ], $context[1] );

$tag2 = 'test_action_closure_2';
$closure2 = function() {
$closure2 = static function() {
$GLOBALS['closure_no_args'] = true;
};
add_action( $tag2, $closure2 );
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/admin/includesListTable.php
Expand Up @@ -360,7 +360,7 @@ public function test_bulk_action_menu_supports_options_and_optgroups() {

add_filter(
'bulk_actions-edit-comments',
function() {
static function() {
return array(
'delete' => 'Delete',
'Change State' => array(
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/block-template-utils.php
Expand Up @@ -70,7 +70,7 @@ function test_get_block_template_from_post() {
function test_get_block_templates() {
function get_template_ids( $templates ) {
return array_map(
function( $template ) {
static function( $template ) {
return $template->id;
},
$templates
Expand Down
8 changes: 4 additions & 4 deletions tests/phpunit/tests/blocks/context.php
Expand Up @@ -115,7 +115,7 @@ function test_provides_block_context() {
'gutenberg/contextWithAssigned',
'gutenberg/contextWithoutDefault',
),
'render_callback' => function( $attributes, $content, $block ) use ( &$provided_context ) {
'render_callback' => static function( $attributes, $content, $block ) use ( &$provided_context ) {
$provided_context[] = $block->context;

return '';
Expand Down Expand Up @@ -155,7 +155,7 @@ function test_provides_default_context() {
'gutenberg/test-context-consumer',
array(
'uses_context' => array( 'postId', 'postType' ),
'render_callback' => function( $attributes, $content, $block ) use ( &$provided_context ) {
'render_callback' => static function( $attributes, $content, $block ) use ( &$provided_context ) {
$provided_context[] = $block->context;

return '';
Expand Down Expand Up @@ -188,15 +188,15 @@ function test_default_context_is_filterable() {
'gutenberg/test-context-consumer',
array(
'uses_context' => array( 'example' ),
'render_callback' => function( $attributes, $content, $block ) use ( &$provided_context ) {
'render_callback' => static function( $attributes, $content, $block ) use ( &$provided_context ) {
$provided_context[] = $block->context;

return '';
},
)
);

$filter_block_context = function( $context ) {
$filter_block_context = static function( $context ) {
$context['example'] = 'ok';
return $context;
};
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/tests/blocks/register.php
Expand Up @@ -510,7 +510,7 @@ function test_has_blocks() {
* @ticket 49615
*/
public function test_filter_block_registration() {
$filter_registration = function( $args, $name ) {
$filter_registration = static function( $args, $name ) {
$args['attributes'] = array( $name => array( 'type' => 'boolean' ) );
return $args;
};
Expand All @@ -528,7 +528,7 @@ public function test_filter_block_registration() {
* @ticket 52138
*/
public function test_filter_block_registration_metadata() {
$filter_metadata_registration = function( $metadata ) {
$filter_metadata_registration = static function( $metadata ) {
$metadata['apiVersion'] = 3;
return $metadata;
};
Expand All @@ -546,7 +546,7 @@ public function test_filter_block_registration_metadata() {
* @ticket 52138
*/
public function test_filter_block_registration_metadata_settings() {
$filter_metadata_registration = function( $settings, $metadata ) {
$filter_metadata_registration = static function( $settings, $metadata ) {
$settings['api_version'] = $metadata['apiVersion'] + 1;
return $settings;
};
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/blocks/supportedStyles.php
Expand Up @@ -712,7 +712,7 @@ public function test_render_block_suppresses_warnings_without_at_suppression() {
// Custom error handler's see Warnings even if they are suppressed by the @ symbol.
$errors = array();
set_error_handler(
function ( $errno = 0, $errstr = '' ) use ( &$errors ) {
static function ( $errno = 0, $errstr = '' ) use ( &$errors ) {
$errors[] = $errstr;
return false;
}
Expand Down

0 comments on commit e83a341

Please sign in to comment.