Skip to content

Commit 3c22de9

Browse files
committed
refactor(Misc): file size unit labeling
Replaces the `switch` statement with a more modern `match` expression in `get_accessible_file_size_label`. This improves code readability and conciseness while maintaining the existing functionality for parsing and presenting file size units.
1 parent 829a702 commit 3c22de9

File tree

1 file changed

+12
-28
lines changed

1 file changed

+12
-28
lines changed

inc/Helpers/Misc.php

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -108,34 +108,18 @@ function get_accessible_file_size_label( string $file_size ): string {
108108
$int_value = (int) $value; // Cast to int for _n() pluralization.
109109
$unit = strtolower( $matches[2] ?? '' );
110110

111-
switch ( $unit ) {
112-
case 'b':
113-
case 'o':
114-
/* translators: %s: file size */
115-
$unit_label = _n( 'byte', 'bytes', $int_value, 'beapi-frontend-framework' );
116-
break;
117-
case 'kb':
118-
case 'ko':
119-
/* translators: %s: file size */
120-
$unit_label = _n( 'kilobyte', 'kilobytes', $int_value, 'beapi-frontend-framework' );
121-
break;
122-
case 'mb':
123-
case 'mo':
124-
/* translators: %s: file size */
125-
$unit_label = _n( 'megabyte', 'megabytes', $int_value, 'beapi-frontend-framework' );
126-
break;
127-
case 'gb':
128-
case 'go':
129-
/* translators: %s: file size */
130-
$unit_label = _n( 'gigabyte', 'gigabytes', $int_value, 'beapi-frontend-framework' );
131-
break;
132-
case 'tb':
133-
case 'to':
134-
/* translators: %s: file size */
135-
$unit_label = _n( 'terabyte', 'terabytes', $int_value, 'beapi-frontend-framework' );
136-
break;
137-
default:
138-
return $file_size;
111+
/* translators: file size units (byte, kilobyte, megabyte, etc.) */
112+
$unit_label = match ( $unit ) {
113+
'b', 'o' => _n( 'byte', 'bytes', $int_value, 'beapi-frontend-framework' ),
114+
'kb', 'ko' => _n( 'kilobyte', 'kilobytes', $int_value, 'beapi-frontend-framework' ),
115+
'mb', 'mo' => _n( 'megabyte', 'megabytes', $int_value, 'beapi-frontend-framework' ),
116+
'gb', 'go' => _n( 'gigabyte', 'gigabytes', $int_value, 'beapi-frontend-framework' ),
117+
'tb', 'to' => _n( 'terabyte', 'terabytes', $int_value, 'beapi-frontend-framework' ),
118+
default => null,
119+
};
120+
121+
if ( null === $unit_label ) {
122+
return $file_size;
139123
}
140124

141125
return $value . ' ' . $unit_label;

0 commit comments

Comments
 (0)