Skip to content

Comments

feat(util): add binary unit label support to humanFileSize()#58026

Open
joshtrichards wants to merge 4 commits intomasterfrom
jtr/util-humanFileSize-opt-binary-label
Open

feat(util): add binary unit label support to humanFileSize()#58026
joshtrichards wants to merge 4 commits intomasterfrom
jtr/util-humanFileSize-opt-binary-label

Conversation

@joshtrichards
Copy link
Member

@joshtrichards joshtrichards commented Feb 3, 2026

Summary

Refactors humanFileSize() to support IEC binary unit labels (KiB, MiB, GiB) while maintaining backward compatibility with existing decimal labels (KB, MB, GB). Math stays the same, but permits us to make the string be technically accurate where we'd prefer that (i.e. in logs - e.g. see #58019) versus where the conventional (albeit inaccurate) end-user friendly facing convention of using KB/MB/GB is preferred even when doing base-1024 math.

Changes

  • New parameter: Added optional $useBinaryLabel parameter (defaults to false)
  • Refactored logic: Replaced cascading if-statements with maintainable loop-based structure
  • Class constants: Introduced DECIMAL_LABELS, BINARY_LABELS, and MAX_LABEL_INDEX for better maintainability
  • Improved documentation: Enhanced docblock with examples and clear explanation of binary vs decimal labels
  • Preserved behavior: Maintains existing rounding strategy (0 decimals for KB/KiB, 1 decimal for larger units) to prevent "1024 KB" displays
  • EB / EiB support: !

Motivation

  • Provides technically accurate option for displaying file sizes using IEC binary prefixes
  • Improves code maintainability by eliminating repetitive if-statement cascade
  • Makes it trivial to add support for additional units (ZB/ZiB!) in the future
  • Addresses the common confusion between binary calculation (÷1024) and decimal unit labels (KB, MB)

TODO

  • ...

Checklist

Signed-off-by: Josh <josh.t.richards@gmail.com>
Signed-off-by: Josh <josh.t.richards@gmail.com>
@joshtrichards joshtrichards added bug 3. to review Waiting for reviews labels Feb 3, 2026
@joshtrichards joshtrichards marked this pull request as ready for review February 3, 2026 15:31
@joshtrichards joshtrichards requested a review from a team as a code owner February 3, 2026 15:31
@joshtrichards joshtrichards requested review from ArtificialOwl, come-nc, leftybournes and sorbaugh and removed request for a team February 3, 2026 15:31
Signed-off-by: Josh <josh.t.richards@gmail.com>
Signed-off-by: Josh <josh.t.richards@gmail.com>
Comment on lines +373 to +380
if ($value < 1024 || $unitIndex === self::MAX_LABEL_INDEX) {
/** @var int<0, 5> $unitIndex */
return "$value {$units[$unitIndex]}";
}
}

$bytes = round($bytes / 1024, 1);
return "$bytes PB";
// Unreachable, but keeps static analyzer happy
return "$value {$units[self::MAX_LABEL_INDEX]}";
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if ($value < 1024 || $unitIndex === self::MAX_LABEL_INDEX) {
/** @var int<0, 5> $unitIndex */
return "$value {$units[$unitIndex]}";
}
}
$bytes = round($bytes / 1024, 1);
return "$bytes PB";
// Unreachable, but keeps static analyzer happy
return "$value {$units[self::MAX_LABEL_INDEX]}";
if ($value < 1024) {
/** @var int<0, 5> $unitIndex */
return "$value {$units[$unitIndex]}";
}
}
return "$value {$units[self::MAX_LABEL_INDEX]}";

I think this is easier to understand and also doesn't need to workaround the analyzer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3. to review Waiting for reviews bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Request used more than 300 MB of RAM: 288 MB

2 participants