From fef947c0b7e4f96cd758c26598bfaf8df537ba56 Mon Sep 17 00:00:00 2001 From: flack Date: Mon, 13 May 2024 22:10:29 +0200 Subject: [PATCH] Small simplifications --- lib/midcom/admin/help/help.php | 14 ++++++------- lib/midcom/helper/_dbfactory.php | 4 +--- lib/midcom/helper/reflector/main.php | 5 +---- lib/midcom/services/auth/acl.php | 5 +---- lib/midcom/services/indexer/client.php | 6 ++---- lib/midgard/admin/asgard/navigation.php | 4 +--- lib/midgard/admin/asgard/plugin.php | 26 +++++++++--------------- lib/midgard/admin/asgard/stylehelper.php | 7 ++----- 8 files changed, 24 insertions(+), 47 deletions(-) diff --git a/lib/midcom/admin/help/help.php b/lib/midcom/admin/help/help.php index f59b331a1..312746c05 100644 --- a/lib/midcom/admin/help/help.php +++ b/lib/midcom/admin/help/help.php @@ -129,14 +129,12 @@ private function _add_virtual_files(array $files, string $component) : array if ($component != 'midcom') { $options['handlers'] = $this->read_component_handlers($component); } - foreach ($options as $key => $values) { - if (!empty($values)) { - $files[$key] = [ - 'path' => '/' . $key, - 'subject' => $this->_l10n->get('help_' . $key), - 'lang' => 'en', - ]; - } + foreach (array_filter($options) as $key => $values) { + $files[$key] = [ + 'path' => '/' . $key, + 'subject' => $this->_l10n->get('help_' . $key), + 'lang' => 'en', + ]; $this->_request_data[$key] = $values; } diff --git a/lib/midcom/helper/_dbfactory.php b/lib/midcom/helper/_dbfactory.php index 17cbc0be6..bbf6087a1 100644 --- a/lib/midcom/helper/_dbfactory.php +++ b/lib/midcom/helper/_dbfactory.php @@ -75,9 +75,7 @@ public function get_cached(string $classname, string|int $src) : midcom_core_dba throw new midcom_error('invalid source identifier'); } - if (!isset($cache[$classname])) { - $cache[$classname] = []; - } + $cache[$classname] ??= []; if (isset($cache[$classname][$src])) { return $cache[$classname][$src]; diff --git a/lib/midcom/helper/reflector/main.php b/lib/midcom/helper/reflector/main.php index fd3c8d4a0..5ced2af85 100644 --- a/lib/midcom/helper/reflector/main.php +++ b/lib/midcom/helper/reflector/main.php @@ -55,10 +55,7 @@ public static function get(string|object $src) : self { $identifier = get_called_class() . (is_object($src) ? get_class($src) : $src); - if (!isset(self::$_cache['instance'][$identifier])) { - self::$_cache['instance'][$identifier] = new static($src); - } - return self::$_cache['instance'][$identifier]; + return self::$_cache['instance'][$identifier] ??= new static($src); } /** diff --git a/lib/midcom/services/auth/acl.php b/lib/midcom/services/auth/acl.php index cc973248e..97fc784dc 100644 --- a/lib/midcom/services/auth/acl.php +++ b/lib/midcom/services/auth/acl.php @@ -464,10 +464,7 @@ public function can_do_byguid(string $privilege, string $object_guid, string $ob $cache_key = "{$user_id}::{$object_guid}::{$privilege}"; - if (!isset($cache[$cache_key])) { - $cache[$cache_key] = $this->can_do_byguid_uncached($privilege, $object_guid, $object_class, $user_id); - } - return $cache[$cache_key]; + return $cache[$cache_key] ??= $this->can_do_byguid_uncached($privilege, $object_guid, $object_class, $user_id); } private function can_do_byguid_uncached(string $privilege, string $object_guid, string $object_class, string $user_id) : bool diff --git a/lib/midcom/services/indexer/client.php b/lib/midcom/services/indexer/client.php index 5ec5eccf3..25fe8ae02 100644 --- a/lib/midcom/services/indexer/client.php +++ b/lib/midcom/services/indexer/client.php @@ -72,10 +72,8 @@ public function add_query(string $name, midcom_core_querybuilder $qb, $dm) public function reindex() { foreach ($this->_queries as $name => [$qb, $dm]) { - $results = $qb->execute(); - if (!empty($results)) { - $documents = $this->process_results($name, $results, $dm); - if (!empty($documents)) { + if ($results = $qb->execute()) { + if ($documents = $this->process_results($name, $results, $dm)) { $this->_indexer->index($documents); } } diff --git a/lib/midgard/admin/asgard/navigation.php b/lib/midgard/admin/asgard/navigation.php index 7ba601bdb..4b3495afa 100644 --- a/lib/midgard/admin/asgard/navigation.php +++ b/lib/midgard/admin/asgard/navigation.php @@ -391,10 +391,8 @@ private function _draw_select_navigation() if (!empty($this->_object_path)) { $this->_request_data['root_object'] = $this->_object_path[0]; $this->_request_data['navigation_type'] = $this->_object_path[0]->__mgdschema_class_name__; - } elseif (isset($this->expanded_root_types[0])) { - $this->_request_data['navigation_type'] = $this->expanded_root_types[0]; } else { - $this->_request_data['navigation_type'] = ''; + $this->_request_data['navigation_type'] = $this->expanded_root_types[0] ?? ''; } $this->_request_data['label_mapping'] = midgard_admin_asgard_plugin::get_root_classes(); diff --git a/lib/midgard/admin/asgard/plugin.php b/lib/midgard/admin/asgard/plugin.php index 3a3f405ed..33084bc4a 100644 --- a/lib/midgard/admin/asgard/plugin.php +++ b/lib/midgard/admin/asgard/plugin.php @@ -284,25 +284,19 @@ public static function get_root_classes() : array { static $root_classes = []; - // Return cached results - if (!empty($root_classes)) { - return $root_classes; - } - - // Initialize the returnable array - $root_classes = []; - - // Get the classes - $classes = midcom_helper_reflector_tree::get_root_classes(); + if (empty($root_classes)) { + // Get the classes + $classes = midcom_helper_reflector_tree::get_root_classes(); + + // Get the translated name + foreach ($classes as $class) { + $ref = new midcom_helper_reflector($class); + $root_classes[$class] = $ref->get_class_label(); + } - // Get the translated name - foreach ($classes as $class) { - $ref = new midcom_helper_reflector($class); - $root_classes[$class] = $ref->get_class_label(); + asort($root_classes); } - asort($root_classes); - return $root_classes; } } diff --git a/lib/midgard/admin/asgard/stylehelper.php b/lib/midgard/admin/asgard/stylehelper.php index cdeb6f39b..6a9b9e35e 100644 --- a/lib/midgard/admin/asgard/stylehelper.php +++ b/lib/midgard/admin/asgard/stylehelper.php @@ -93,11 +93,8 @@ private function _get_style_elements_and_nodes(int $style_id) : array $style_nodes = $this->_get_nodes_using_style($style_path); foreach ($style_nodes as $node) { - if (!isset($results['nodes'][$node->component])) { - $results['nodes'][$node->component] = []; - // Get the list of style elements for the component - $results['elements'][$node->component] = $this->_get_component_default_elements($node->component); - } + // Get the list of style elements for the component + $results['elements'][$node->component] ??= $this->_get_component_default_elements($node->component); $results['nodes'][$node->component][] = $node; }