From e48191a9458e4f8d4a2cbbb6aef6ea5f765f51cf Mon Sep 17 00:00:00 2001 From: catch Date: Tue, 21 Mar 2023 19:34:56 +0000 Subject: [PATCH] Issue #3346645 by jweowu, catch, smustgrave: Eliminate anti-pattern isset($arr[$key]) || array_key_exists($key, $arr) --- core/includes/form.inc | 2 +- core/lib/Drupal/Component/DependencyInjection/Container.php | 6 +++--- .../Drupal/Component/Plugin/Factory/ReflectionFactory.php | 2 +- core/lib/Drupal/Component/Utility/NestedArray.php | 4 ++-- core/lib/Drupal/Core/Cache/CacheCollector.php | 4 ++-- core/lib/Drupal/Core/State/State.php | 2 +- core/lib/Drupal/Core/Theme/ThemeManager.php | 2 +- core/lib/Drupal/Core/Utility/ThemeRegistry.php | 2 +- core/modules/image/image.module | 4 ++-- .../views/src/Plugin/views/display/DisplayPluginBase.php | 2 +- 10 files changed, 15 insertions(+), 15 deletions(-) diff --git a/core/includes/form.inc b/core/includes/form.inc index d6db3ddeec9..2aabd2f6dc7 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -87,7 +87,7 @@ function form_select_options($element, $choices = NULL) { // array_key_exists() accommodates the rare event where $element['#value'] is NULL. // isset() fails in this situation. - $value_valid = isset($element['#value']) || array_key_exists('#value', $element); + $value_valid = \array_key_exists('#value', $element); $value_is_array = $value_valid && is_array($element['#value']); // Check if the element is multiple select and no value has been selected. $empty_value = (empty($element['#value']) && !empty($element['#multiple'])); diff --git a/core/lib/Drupal/Component/DependencyInjection/Container.php b/core/lib/Drupal/Component/DependencyInjection/Container.php index 4c8fa971586..7f6d54b4c05 100644 --- a/core/lib/Drupal/Component/DependencyInjection/Container.php +++ b/core/lib/Drupal/Component/DependencyInjection/Container.php @@ -320,7 +320,7 @@ class Container implements ContainerInterface, ResetInterface { * {@inheritdoc} */ public function getParameter($name): array|bool|string|int|float|NULL { - if (!(isset($this->parameters[$name]) || array_key_exists($name, $this->parameters))) { + if (!\array_key_exists($name, $this->parameters)) { if (!$name) { throw new ParameterNotFoundException(''); } @@ -335,7 +335,7 @@ class Container implements ContainerInterface, ResetInterface { * {@inheritdoc} */ public function hasParameter($name): bool { - return isset($this->parameters[$name]) || array_key_exists($name, $this->parameters); + return \array_key_exists($name, $this->parameters); } /** @@ -357,7 +357,7 @@ class Container implements ContainerInterface, ResetInterface { $id = $this->aliases[$id]; } - return isset($this->services[$id]) || array_key_exists($id, $this->services); + return \array_key_exists($id, $this->services); } /** diff --git a/core/lib/Drupal/Component/Plugin/Factory/ReflectionFactory.php b/core/lib/Drupal/Component/Plugin/Factory/ReflectionFactory.php index 8a42ce3d2a5..37e3519983c 100644 --- a/core/lib/Drupal/Component/Plugin/Factory/ReflectionFactory.php +++ b/core/lib/Drupal/Component/Plugin/Factory/ReflectionFactory.php @@ -64,7 +64,7 @@ class ReflectionFactory extends DefaultFactory { elseif ($param_name == 'configuration') { $arguments[] = $configuration; } - elseif (isset($configuration[$param_name]) || array_key_exists($param_name, $configuration)) { + elseif (\array_key_exists($param_name, $configuration)) { $arguments[] = $configuration[$param_name]; } elseif ($param->isDefaultValueAvailable()) { diff --git a/core/lib/Drupal/Component/Utility/NestedArray.php b/core/lib/Drupal/Component/Utility/NestedArray.php index 0521c0d5970..c9feb599d1b 100644 --- a/core/lib/Drupal/Component/Utility/NestedArray.php +++ b/core/lib/Drupal/Component/Utility/NestedArray.php @@ -69,7 +69,7 @@ class NestedArray { public static function &getValue(array &$array, array $parents, &$key_exists = NULL) { $ref = &$array; foreach ($parents as $parent) { - if (is_array($ref) && (isset($ref[$parent]) || array_key_exists($parent, $ref))) { + if (is_array($ref) && \array_key_exists($parent, $ref)) { $ref = &$ref[$parent]; } else { @@ -219,7 +219,7 @@ class NestedArray { public static function unsetValue(array &$array, array $parents, &$key_existed = NULL) { $unset_key = array_pop($parents); $ref = &self::getValue($array, $parents, $key_existed); - if ($key_existed && is_array($ref) && (isset($ref[$unset_key]) || array_key_exists($unset_key, $ref))) { + if ($key_existed && is_array($ref) && \array_key_exists($unset_key, $ref)) { $key_existed = TRUE; unset($ref[$unset_key]); } diff --git a/core/lib/Drupal/Core/Cache/CacheCollector.php b/core/lib/Drupal/Core/Cache/CacheCollector.php index 912b60f943f..5250e1b049a 100644 --- a/core/lib/Drupal/Core/Cache/CacheCollector.php +++ b/core/lib/Drupal/Core/Cache/CacheCollector.php @@ -134,7 +134,7 @@ abstract class CacheCollector implements CacheCollectorInterface, DestructableIn public function has($key) { // Make sure the value is loaded. $this->get($key); - return isset($this->storage[$key]) || array_key_exists($key, $this->storage); + return \array_key_exists($key, $this->storage); } /** @@ -142,7 +142,7 @@ abstract class CacheCollector implements CacheCollectorInterface, DestructableIn */ public function get($key) { $this->lazyLoadCache(); - if (isset($this->storage[$key]) || array_key_exists($key, $this->storage)) { + if (\array_key_exists($key, $this->storage)) { return $this->storage[$key]; } else { diff --git a/core/lib/Drupal/Core/State/State.php b/core/lib/Drupal/Core/State/State.php index a71e5b2752f..9a3b75cbdb2 100644 --- a/core/lib/Drupal/Core/State/State.php +++ b/core/lib/Drupal/Core/State/State.php @@ -63,7 +63,7 @@ class State implements StateInterface { foreach ($load as $key) { // If we find a value, even one that is NULL, add it to the cache and // return it. - if (isset($loaded_values[$key]) || array_key_exists($key, $loaded_values)) { + if (\array_key_exists($key, $loaded_values)) { $values[$key] = $loaded_values[$key]; $this->cache[$key] = $loaded_values[$key]; } diff --git a/core/lib/Drupal/Core/Theme/ThemeManager.php b/core/lib/Drupal/Core/Theme/ThemeManager.php index 6dfd871506d..9ddf6a7a567 100644 --- a/core/lib/Drupal/Core/Theme/ThemeManager.php +++ b/core/lib/Drupal/Core/Theme/ThemeManager.php @@ -190,7 +190,7 @@ class ThemeManager implements ThemeManagerInterface { $variables = []; if (isset($info['variables'])) { foreach (array_keys($info['variables']) as $name) { - if (isset($element["#$name"]) || array_key_exists("#$name", $element)) { + if (\array_key_exists("#$name", $element)) { $variables[$name] = $element["#$name"]; } } diff --git a/core/lib/Drupal/Core/Utility/ThemeRegistry.php b/core/lib/Drupal/Core/Utility/ThemeRegistry.php index 34b97dfb5db..c7da9e43051 100644 --- a/core/lib/Drupal/Core/Utility/ThemeRegistry.php +++ b/core/lib/Drupal/Core/Utility/ThemeRegistry.php @@ -98,7 +98,7 @@ class ThemeRegistry extends CacheCollector implements DestructableInterface { // are not registered, just check the existence of the key in the registry. // Use array_key_exists() here since a NULL value indicates that the theme // hook exists but has not yet been requested. - return isset($this->storage[$key]) || array_key_exists($key, $this->storage); + return \array_key_exists($key, $this->storage); } /** diff --git a/core/modules/image/image.module b/core/modules/image/image.module index 59127a41b9e..807b58c33a4 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -287,10 +287,10 @@ function template_preprocess_image_style(&$variables) { ]); } - if (isset($variables['alt']) || array_key_exists('alt', $variables)) { + if (\array_key_exists('alt', $variables)) { $variables['image']['#alt'] = $variables['alt']; } - if (isset($variables['title']) || array_key_exists('title', $variables)) { + if (\array_key_exists('title', $variables)) { $variables['image']['#title'] = $variables['title']; } diff --git a/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php b/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php index e1827a8104b..167a9f8c2c5 100644 --- a/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php +++ b/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php @@ -790,7 +790,7 @@ abstract class DisplayPluginBase extends PluginBase implements DisplayPluginInte return $this->default_display->getOption($option); } - if (isset($this->options[$option]) || array_key_exists($option, $this->options)) { + if (\array_key_exists($option, $this->options)) { return $this->options[$option]; } }