Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use ternary operators in Helpers #8529

Merged
merged 5 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions system/Helpers/array_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
ddevsr marked this conversation as resolved.
Show resolved Hide resolved
}

return null;
Expand Down
8 changes: 1 addition & 7 deletions system/Helpers/filesystem_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
ddevsr marked this conversation as resolved.
Show resolved Hide resolved
}
}
} catch (Throwable $e) {
Expand Down
8 changes: 1 addition & 7 deletions system/Helpers/form_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? $postInput ?? $default;
ddevsr marked this conversation as resolved.
Show resolved Hide resolved

if (is_array($input)) {
// Note: in_array('', array(0)) returns TRUE, do not use it
Expand Down
37 changes: 5 additions & 32 deletions system/Helpers/html_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,7 @@ function _list(string $type = 'ul', $list = [], $attributes = '', int $depth = 0
foreach ($list as $key => $val) {
$out .= str_repeat(' ', $depth + 2) . '<li>';

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);
Copy link
Member

Choose a reason for hiding this comment

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

Remove this change.


$out .= "</li>\n";
}
Expand Down Expand Up @@ -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'] . '"';
Copy link
Member

Choose a reason for hiding this comment

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

Remove this change.


unset($src['src']);
}
Expand Down Expand Up @@ -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 . '" ';
Copy link
Member

Choose a reason for hiding this comment

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

Remove this change.

} else {
// for attributes without values, like async or defer, use NULL.
$script .= $k . (null === $v ? ' ' : '="' . $v . '" ');
Expand Down Expand Up @@ -295,13 +280,7 @@ function video($src, string $unsupportedMessage = '', string $attributes = '', a

$video = '<video';

if (_has_protocol($src)) {
$video .= ' src="' . $src . '"';
} elseif ($indexPage === true) {
$video .= ' src="' . site_url($src) . '"';
} else {
$video .= ' src="' . slash_item('baseURL') . $src . '"';
}
$video .= _has_protocol($src) ? ' src="' . $src . '"' : ($indexPage === true ? ' src="' . site_url($src) . '"' : ' src="' . slash_item('baseURL') . $src . '"');
Copy link
Member

Choose a reason for hiding this comment

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

Remove this change.


if ($attributes !== '') {
$video .= ' ' . $attributes;
Expand Down Expand Up @@ -341,13 +320,7 @@ function audio($src, string $unsupportedMessage = '', string $attributes = '', a

$audio = '<audio';

if (_has_protocol($src)) {
$audio .= ' src="' . $src . '"';
} elseif ($indexPage === true) {
$audio .= ' src="' . site_url($src) . '"';
} else {
$audio .= ' src="' . slash_item('baseURL') . $src . '"';
}
$audio .= _has_protocol($src) ? ' src="' . $src . '"' : ($indexPage === true ? ' src="' . site_url($src) . '"' : ' src="' . slash_item('baseURL') . $src . '"');
Copy link
Member

Choose a reason for hiding this comment

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

Remove this change.


if ($attributes !== '') {
$audio .= ' ' . $attributes;
Expand Down
12 changes: 2 additions & 10 deletions system/Helpers/text_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,7 @@ function word_wrap(string $str, int $charlim = 76): string

// If $temp contains data it means we had to split up an over-length
// word into smaller chunks so we'll add it back to our current line
if ($temp !== '') {
$output .= $temp . "\n" . $line . "\n";
} else {
$output .= $line . "\n";
}
$output .= $temp !== '' ? $temp . "\n" . $line . "\n" : $line . "\n";
Copy link
Member

Choose a reason for hiding this comment

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

Remove this change.

}

// Put our markers back
Expand Down Expand Up @@ -430,11 +426,7 @@ function ellipsize(string $str, int $maxLength, $position = 1, string $ellipsis
$beg = mb_substr($str, 0, (int) floor($maxLength * $position));
$position = ($position > 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)));
Copy link
Member

Choose a reason for hiding this comment

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

Remove this change.


return $beg . $ellipsis . $end;
}
Expand Down
Loading