From 345eb4a005383f5d3deabb7624fcbdee2b89e546 Mon Sep 17 00:00:00 2001 From: Denny Septian Panggabean Date: Fri, 9 Feb 2024 09:43:45 +0700 Subject: [PATCH 1/5] refactor: possible one liner in Helper --- system/Helpers/array_helper.php | 7 +----- system/Helpers/filesystem_helper.php | 8 +----- system/Helpers/form_helper.php | 8 +----- system/Helpers/html_helper.php | 37 ++++------------------------ system/Helpers/text_helper.php | 12 ++------- 5 files changed, 10 insertions(+), 62 deletions(-) diff --git a/system/Helpers/array_helper.php b/system/Helpers/array_helper.php index b8bd1a4d956f..4c7f7f4b51e8 100644 --- a/system/Helpers/array_helper.php +++ b/system/Helpers/array_helper.php @@ -72,12 +72,7 @@ function _array_search_dot(array $indexes, array $array) $answer = array_filter($answer, static fn ($value) => $value !== null); if ($answer !== []) { - if (count($answer) === 1) { - // If array only has one element, we return that element for BC. - return current($answer); - } - - return $answer; + return count($answer) === 1 ? current($answer) : $answer; } return null; diff --git a/system/Helpers/filesystem_helper.php b/system/Helpers/filesystem_helper.php index 4e0e8b1532e9..dc64ce18a986 100644 --- a/system/Helpers/filesystem_helper.php +++ b/system/Helpers/filesystem_helper.php @@ -218,13 +218,7 @@ function get_filenames( } if ($includeDir || ! $object->isDir()) { - if ($includePath === false) { - $files[] = $basename; - } elseif ($includePath === null) { - $files[] = str_replace($sourceDir, '', $name); - } else { - $files[] = $name; - } + $files[] = $includePath === false ? $basename : ($includePath === null ? str_replace($sourceDir, '', $name) : $name); } } } catch (Throwable $e) { diff --git a/system/Helpers/form_helper.php b/system/Helpers/form_helper.php index d3abd7e247b0..cd3f874b28f9 100644 --- a/system/Helpers/form_helper.php +++ b/system/Helpers/form_helper.php @@ -656,13 +656,7 @@ function set_radio(string $field, string $value = '', bool $default = false): st $postInput = $request->getPost($field); - if ($oldInput !== null) { - $input = $oldInput; - } elseif ($postInput !== null) { - $input = $postInput; - } else { - $input = $default; - } + $input = $oldInput !== null ? $oldInput : ($postInput !== null ? $postInput : $default); if (is_array($input)) { // Note: in_array('', array(0)) returns TRUE, do not use it diff --git a/system/Helpers/html_helper.php b/system/Helpers/html_helper.php index 075dcd510b43..8fd4231dc975 100755 --- a/system/Helpers/html_helper.php +++ b/system/Helpers/html_helper.php @@ -66,14 +66,7 @@ function _list(string $type = 'ul', $list = [], $attributes = '', int $depth = 0 foreach ($list as $key => $val) { $out .= str_repeat(' ', $depth + 2) . '
  • '; - if (! is_array($val)) { - $out .= $val; - } else { - $out .= $key - . "\n" - . _list($type, $val, '', $depth + 4) - . str_repeat(' ', $depth + 2); - } + $out .= ! is_array($val) ? $val : $key . "\n" . _list($type, $val, '', $depth + 4) . str_repeat(' ', $depth + 2); $out .= "
  • \n"; } @@ -109,11 +102,7 @@ function img($src = '', bool $indexPage = false, $attributes = ''): string // Check for a relative URI if (! preg_match('#^([a-z]+:)?//#i', $src['src']) && strpos($src['src'], 'data:') !== 0) { - if ($indexPage === true) { - $img .= ' src="' . site_url($src['src']) . '"'; - } else { - $img .= ' src="' . slash_item('baseURL') . $src['src'] . '"'; - } + $img .= $indexPage === true ? ' src="' . site_url($src['src']) . '"' : ' src="' . slash_item('baseURL') . $src['src'] . '"'; unset($src['src']); } @@ -203,11 +192,7 @@ function script_tag($src = '', bool $indexPage = false): string foreach ($src as $k => $v) { if ($k === 'src' && ! preg_match('#^([a-z]+:)?//#i', $v)) { - if ($indexPage === true) { - $script .= 'src="' . site_url($v) . '" '; - } else { - $script .= 'src="' . slash_item('baseURL') . $v . '" '; - } + $script .= $indexPage === true ? 'src="' . site_url($v) . '" ' : 'src="' . slash_item('baseURL') . $v . '" '; } else { // for attributes without values, like async or defer, use NULL. $script .= $k . (null === $v ? ' ' : '="' . $v . '" '); @@ -295,13 +280,7 @@ function video($src, string $unsupportedMessage = '', string $attributes = '', a $video = ' 1) ? 1 : $position; - if ($position === 1) { - $end = mb_substr($str, 0, -($maxLength - mb_strlen($beg))); - } else { - $end = mb_substr($str, -($maxLength - mb_strlen($beg))); - } + $end = $position === 1 ? mb_substr($str, 0, -($maxLength - mb_strlen($beg))) : mb_substr($str, -($maxLength - mb_strlen($beg))); return $beg . $ellipsis . $end; } From eec94a4ab37d1b27efe151963767cdf92f52eefb Mon Sep 17 00:00:00 2001 From: Denny Septian Panggabean Date: Fri, 9 Feb 2024 09:50:43 +0700 Subject: [PATCH 2/5] run rector --- system/Helpers/form_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Helpers/form_helper.php b/system/Helpers/form_helper.php index cd3f874b28f9..b8ad34770464 100644 --- a/system/Helpers/form_helper.php +++ b/system/Helpers/form_helper.php @@ -656,7 +656,7 @@ function set_radio(string $field, string $value = '', bool $default = false): st $postInput = $request->getPost($field); - $input = $oldInput !== null ? $oldInput : ($postInput !== null ? $postInput : $default); + $input = $oldInput ?? $postInput ?? $default; if (is_array($input)) { // Note: in_array('', array(0)) returns TRUE, do not use it From b87ee39a7870dbf6600d8411c360f7a28c797678 Mon Sep 17 00:00:00 2001 From: Denny Septian Panggabean Date: Fri, 9 Feb 2024 11:13:58 +0700 Subject: [PATCH 3/5] revert comment --- system/Helpers/array_helper.php | 1 + 1 file changed, 1 insertion(+) diff --git a/system/Helpers/array_helper.php b/system/Helpers/array_helper.php index 4c7f7f4b51e8..4c1477f57d4a 100644 --- a/system/Helpers/array_helper.php +++ b/system/Helpers/array_helper.php @@ -72,6 +72,7 @@ function _array_search_dot(array $indexes, array $array) $answer = array_filter($answer, static fn ($value) => $value !== null); if ($answer !== []) { + // If array only has one element, we return that element for BC. return count($answer) === 1 ? current($answer) : $answer; } From 11db748cc14b16a375d9f0b84096e110d09d655b Mon Sep 17 00:00:00 2001 From: Denny Septian Panggabean Date: Sat, 10 Feb 2024 08:59:54 +0700 Subject: [PATCH 4/5] revert change in filesystem --- system/Helpers/filesystem_helper.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/system/Helpers/filesystem_helper.php b/system/Helpers/filesystem_helper.php index dc64ce18a986..4e0e8b1532e9 100644 --- a/system/Helpers/filesystem_helper.php +++ b/system/Helpers/filesystem_helper.php @@ -218,7 +218,13 @@ function get_filenames( } if ($includeDir || ! $object->isDir()) { - $files[] = $includePath === false ? $basename : ($includePath === null ? str_replace($sourceDir, '', $name) : $name); + if ($includePath === false) { + $files[] = $basename; + } elseif ($includePath === null) { + $files[] = str_replace($sourceDir, '', $name); + } else { + $files[] = $name; + } } } } catch (Throwable $e) { From aed6e969546534c4730bd86be3234f02deac0bdb Mon Sep 17 00:00:00 2001 From: Denny Septian Panggabean Date: Sat, 10 Feb 2024 20:37:50 +0700 Subject: [PATCH 5/5] remove change --- system/Helpers/html_helper.php | 37 +++++++++++++++++++++++++++++----- system/Helpers/text_helper.php | 12 +++++++++-- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/system/Helpers/html_helper.php b/system/Helpers/html_helper.php index 8fd4231dc975..0f7e154a02bf 100755 --- a/system/Helpers/html_helper.php +++ b/system/Helpers/html_helper.php @@ -66,7 +66,14 @@ function _list(string $type = 'ul', $list = [], $attributes = '', int $depth = 0 foreach ($list as $key => $val) { $out .= str_repeat(' ', $depth + 2) . '
  • '; - $out .= ! is_array($val) ? $val : $key . "\n" . _list($type, $val, '', $depth + 4) . str_repeat(' ', $depth + 2); + if (! is_array($val)) { + $out .= $val; + } else { + $out .= $key + . "\n" + . _list($type, $val, '', $depth + 4) + . str_repeat(' ', $depth + 2); + } $out .= "
  • \n"; } @@ -102,7 +109,11 @@ function img($src = '', bool $indexPage = false, $attributes = ''): string // Check for a relative URI if (! preg_match('#^([a-z]+:)?//#i', $src['src']) && strpos($src['src'], 'data:') !== 0) { - $img .= $indexPage === true ? ' src="' . site_url($src['src']) . '"' : ' src="' . slash_item('baseURL') . $src['src'] . '"'; + if ($indexPage === true) { + $img .= ' src="' . site_url($src['src']) . '"'; + } else { + $img .= ' src="' . slash_item('baseURL') . $src['src'] . '"'; + } unset($src['src']); } @@ -192,7 +203,11 @@ function script_tag($src = '', bool $indexPage = false): string foreach ($src as $k => $v) { if ($k === 'src' && ! preg_match('#^([a-z]+:)?//#i', $v)) { - $script .= $indexPage === true ? 'src="' . site_url($v) . '" ' : 'src="' . slash_item('baseURL') . $v . '" '; + if ($indexPage === true) { + $script .= 'src="' . site_url($v) . '" '; + } else { + $script .= 'src="' . slash_item('baseURL') . $v . '" '; + } } else { // for attributes without values, like async or defer, use NULL. $script .= $k . (null === $v ? ' ' : '="' . $v . '" '); @@ -280,7 +295,13 @@ function video($src, string $unsupportedMessage = '', string $attributes = '', a $video = ' 1) ? 1 : $position; - $end = $position === 1 ? mb_substr($str, 0, -($maxLength - mb_strlen($beg))) : mb_substr($str, -($maxLength - mb_strlen($beg))); + if ($position === 1) { + $end = mb_substr($str, 0, -($maxLength - mb_strlen($beg))); + } else { + $end = mb_substr($str, -($maxLength - mb_strlen($beg))); + } return $beg . $ellipsis . $end; }