Skip to content

Commit 345eb4a

Browse files
committed
refactor: possible one liner in Helper
1 parent c7bedbb commit 345eb4a

5 files changed

Lines changed: 10 additions & 62 deletions

File tree

system/Helpers/array_helper.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,7 @@ function _array_search_dot(array $indexes, array $array)
7272
$answer = array_filter($answer, static fn ($value) => $value !== null);
7373

7474
if ($answer !== []) {
75-
if (count($answer) === 1) {
76-
// If array only has one element, we return that element for BC.
77-
return current($answer);
78-
}
79-
80-
return $answer;
75+
return count($answer) === 1 ? current($answer) : $answer;
8176
}
8277

8378
return null;

system/Helpers/filesystem_helper.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,7 @@ function get_filenames(
218218
}
219219

220220
if ($includeDir || ! $object->isDir()) {
221-
if ($includePath === false) {
222-
$files[] = $basename;
223-
} elseif ($includePath === null) {
224-
$files[] = str_replace($sourceDir, '', $name);
225-
} else {
226-
$files[] = $name;
227-
}
221+
$files[] = $includePath === false ? $basename : ($includePath === null ? str_replace($sourceDir, '', $name) : $name);
228222
}
229223
}
230224
} catch (Throwable $e) {

system/Helpers/form_helper.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -656,13 +656,7 @@ function set_radio(string $field, string $value = '', bool $default = false): st
656656

657657
$postInput = $request->getPost($field);
658658

659-
if ($oldInput !== null) {
660-
$input = $oldInput;
661-
} elseif ($postInput !== null) {
662-
$input = $postInput;
663-
} else {
664-
$input = $default;
665-
}
659+
$input = $oldInput !== null ? $oldInput : ($postInput !== null ? $postInput : $default);
666660

667661
if (is_array($input)) {
668662
// Note: in_array('', array(0)) returns TRUE, do not use it

system/Helpers/html_helper.php

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,7 @@ function _list(string $type = 'ul', $list = [], $attributes = '', int $depth = 0
6666
foreach ($list as $key => $val) {
6767
$out .= str_repeat(' ', $depth + 2) . '<li>';
6868

69-
if (! is_array($val)) {
70-
$out .= $val;
71-
} else {
72-
$out .= $key
73-
. "\n"
74-
. _list($type, $val, '', $depth + 4)
75-
. str_repeat(' ', $depth + 2);
76-
}
69+
$out .= ! is_array($val) ? $val : $key . "\n" . _list($type, $val, '', $depth + 4) . str_repeat(' ', $depth + 2);
7770

7871
$out .= "</li>\n";
7972
}
@@ -109,11 +102,7 @@ function img($src = '', bool $indexPage = false, $attributes = ''): string
109102

110103
// Check for a relative URI
111104
if (! preg_match('#^([a-z]+:)?//#i', $src['src']) && strpos($src['src'], 'data:') !== 0) {
112-
if ($indexPage === true) {
113-
$img .= ' src="' . site_url($src['src']) . '"';
114-
} else {
115-
$img .= ' src="' . slash_item('baseURL') . $src['src'] . '"';
116-
}
105+
$img .= $indexPage === true ? ' src="' . site_url($src['src']) . '"' : ' src="' . slash_item('baseURL') . $src['src'] . '"';
117106

118107
unset($src['src']);
119108
}
@@ -203,11 +192,7 @@ function script_tag($src = '', bool $indexPage = false): string
203192

204193
foreach ($src as $k => $v) {
205194
if ($k === 'src' && ! preg_match('#^([a-z]+:)?//#i', $v)) {
206-
if ($indexPage === true) {
207-
$script .= 'src="' . site_url($v) . '" ';
208-
} else {
209-
$script .= 'src="' . slash_item('baseURL') . $v . '" ';
210-
}
195+
$script .= $indexPage === true ? 'src="' . site_url($v) . '" ' : 'src="' . slash_item('baseURL') . $v . '" ';
211196
} else {
212197
// for attributes without values, like async or defer, use NULL.
213198
$script .= $k . (null === $v ? ' ' : '="' . $v . '" ');
@@ -295,13 +280,7 @@ function video($src, string $unsupportedMessage = '', string $attributes = '', a
295280

296281
$video = '<video';
297282

298-
if (_has_protocol($src)) {
299-
$video .= ' src="' . $src . '"';
300-
} elseif ($indexPage === true) {
301-
$video .= ' src="' . site_url($src) . '"';
302-
} else {
303-
$video .= ' src="' . slash_item('baseURL') . $src . '"';
304-
}
283+
$video .= _has_protocol($src) ? ' src="' . $src . '"' : ($indexPage === true ? ' src="' . site_url($src) . '"' : ' src="' . slash_item('baseURL') . $src . '"');
305284

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

342321
$audio = '<audio';
343322

344-
if (_has_protocol($src)) {
345-
$audio .= ' src="' . $src . '"';
346-
} elseif ($indexPage === true) {
347-
$audio .= ' src="' . site_url($src) . '"';
348-
} else {
349-
$audio .= ' src="' . slash_item('baseURL') . $src . '"';
350-
}
323+
$audio .= _has_protocol($src) ? ' src="' . $src . '"' : ($indexPage === true ? ' src="' . site_url($src) . '"' : ' src="' . slash_item('baseURL') . $src . '"');
351324

352325
if ($attributes !== '') {
353326
$audio .= ' ' . $attributes;

system/Helpers/text_helper.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,7 @@ function word_wrap(string $str, int $charlim = 76): string
387387

388388
// If $temp contains data it means we had to split up an over-length
389389
// word into smaller chunks so we'll add it back to our current line
390-
if ($temp !== '') {
391-
$output .= $temp . "\n" . $line . "\n";
392-
} else {
393-
$output .= $line . "\n";
394-
}
390+
$output .= $temp !== '' ? $temp . "\n" . $line . "\n" : $line . "\n";
395391
}
396392

397393
// Put our markers back
@@ -430,11 +426,7 @@ function ellipsize(string $str, int $maxLength, $position = 1, string $ellipsis
430426
$beg = mb_substr($str, 0, (int) floor($maxLength * $position));
431427
$position = ($position > 1) ? 1 : $position;
432428

433-
if ($position === 1) {
434-
$end = mb_substr($str, 0, -($maxLength - mb_strlen($beg)));
435-
} else {
436-
$end = mb_substr($str, -($maxLength - mb_strlen($beg)));
437-
}
429+
$end = $position === 1 ? mb_substr($str, 0, -($maxLength - mb_strlen($beg))) : mb_substr($str, -($maxLength - mb_strlen($beg)));
438430

439431
return $beg . $ellipsis . $end;
440432
}

0 commit comments

Comments
 (0)