Skip to content

Commit

Permalink
Replace array_key_exists with isset for speed
Browse files Browse the repository at this point in the history
Update var name
  • Loading branch information
ramonjd committed Dec 19, 2022
1 parent bb95fde commit da1093d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,32 +44,38 @@ public function __construct( $base_metadata = array() ) {
/**
* Adds block style metadata.
*
* @param array $metadata The $metadata.
* @param array $new_metadata The $metadata to be added.
*
* @return WP_Style_Engine_Block_Style_Metadata Returns the object to allow chaining methods.
*/
public function add_metadata( $metadata = array() ) {
foreach ( $metadata as $definition_group_key => $definition_group_style ) {
public function add_metadata( $new_metadata = array() ) {
if ( empty( $new_metadata ) ) {
return $this;
}

foreach ( $new_metadata as $definition_group_key => $definition_group_style ) {
if ( ! is_array( $definition_group_style ) || empty( $definition_group_style ) ) {
continue;
}

if ( ! array_key_exists( $definition_group_key, $this->merged_block_support_metadata ) ) {
// Adds a new top-level group if it doesn't exist already.
if ( ! isset( $this->merged_block_support_metadata[ $definition_group_key ] ) ) {
$this->merged_block_support_metadata[ $definition_group_key ] = array();
}

foreach ( $definition_group_style as $style_definition_key => $style_definition ) {
// Bails early if merging metadata is attempting to overwrite existing, original style metadata.
if ( array_key_exists( $definition_group_key, $this->base_metadata )
&& array_key_exists( $style_definition_key, $this->base_metadata[ $definition_group_key ] ) ) {
if ( isset( $this->base_metadata[ $definition_group_key ] )
&& isset( $this->base_metadata[ $definition_group_key ][ $style_definition_key ] ) ) {
continue;
}

if ( ! is_array( $style_definition ) || empty( $style_definition ) ) {
continue;
}

$array_to_extend = array_key_exists( $style_definition_key, $this->merged_block_support_metadata[ $definition_group_key ] ) ? $this->merged_block_support_metadata[ $definition_group_key ][ $style_definition_key ] : array();
$array_to_extend = isset( $this->merged_block_support_metadata[ $definition_group_key ][ $style_definition_key ] )
? $this->merged_block_support_metadata[ $definition_group_key ][ $style_definition_key ] : array();
$merged_style_definition = $this->merge_custom_style_definitions_metadata( $array_to_extend, $style_definition );

if ( $merged_style_definition ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function test_should_add_new_top_level_metadata() {
}

/**
* Tests adding new second-level property metadata to the block styles definition.
* Tests adding new second-level property metadata to the block styles definition and ignore `value_func` values.
*
* @covers ::add_metadata
*/
Expand Down

1 comment on commit da1093d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3728257456
📝 Reported issues:

Please sign in to comment.