Skip to content

Conversation

@alganet
Copy link
Member

@alganet alganet commented Feb 3, 2026

Includes formatters that perform unit promotion. We DO NOT do unit conversion (for example, inches to centimeters), we only choose within the appropriate system (metric or imperial) the best representation for a given value.

For example, saying 10cm is much better than saying 0.1m. Both are still within the metric system.

Considerations made during this change:

  • We only use unit symbols, not the full names. This simplifies aspects such as translations, since most units are universal but their localized full names are not. There is some space for later accepting full names (new MetricFormatter('meter')) and deciding the output representation automatically. This is also the reason why PHP enums were not used as the source of truth for supported units.
  • Time unit symbols are not universal, but they were considered for inclusion for their value in technical systems (simplifying a log entry or measure).
  • The SI (colloquially known as "metric system") is the widest adopted standard, so only "Imperial" was prefixed. We used the "Metric" name to denote SI Length units.

@codecov-commenter
Copy link

codecov-commenter commented Feb 3, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.42%. Comparing base (abb8a25) to head (27bed86).

Additional details and impacted files
@@             Coverage Diff              @@
##               main       #6      +/-   ##
============================================
+ Coverage     99.31%   99.42%   +0.10%     
- Complexity      141      162      +21     
============================================
  Files            14       21       +7     
  Lines           294      349      +55     
============================================
+ Hits            292      347      +55     
  Misses            2        2              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements unit formatters that perform unit promotion within measurement systems (metric and imperial). The formatters automatically select the most appropriate unit representation based on magnitude, converting values like 0.1m to 10cm or 60s to 1min.

Changes:

  • Added UnitPromoter trait providing shared promotion logic for all unit formatters
  • Implemented 6 new formatters: TimeFormatter, MetricFormatter (length), MassFormatter (metric), ImperialLengthFormatter, ImperialMassFormatter, and ImperialAreaFormatter
  • Updated Builder and Chain interfaces to support the new formatters

Reviewed changes

Copilot reviewed 22 out of 22 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/Internal/UnitPromoter.php Core trait implementing unit promotion logic with ratio-based conversion
src/TimeFormatter.php Time unit formatter supporting ns to millennia
src/MetricFormatter.php Metric length formatter (mm, cm, m, km)
src/MassFormatter.php Metric mass formatter (mg, g, kg, t)
src/ImperialLengthFormatter.php Imperial length formatter (in, ft, yd, mi)
src/ImperialMassFormatter.php Imperial mass formatter (oz, lb, st, ton)
src/ImperialAreaFormatter.php Imperial area formatter (in², ft², yd², ac, mi²)
src/Mixin/Builder.php Added builder methods for new formatters
src/Mixin/Chain.php Added chain methods for new formatters
tests/Unit/*FormatterTest.php Comprehensive unit tests for all formatters
docs/*Formatter.md Documentation for each new formatter
README.md Updated formatter listing table

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Includes formatters that perform unit promotion. We DO NOT do
unit conversion (for example, inches to centimeters), we only
choose within the appropriate system (metric or imperial) the
best representation for a given value.

For example, saying `10cm` is much better than saying `0.1m`.
Both are still within the metric system.

Considerations made during this change:

 - We only use unit symbols, not the full names. This simplifies
   aspects such as translations, since most units are universal
   but their localized full names are not. There is some space
   for later accepting full names (new MetricFormatter('meter'))
   and deciding the output representation automatically. This is
   also the reason why PHP enums were not used as the source of
   truth for supported units.
 - Time unit symbols are *not universal*, but they were considered
   for inclusion for their value in technical systems (simplifying
   a log entry or measure).
 - The SI (colloquially known as "metric system") is the widest
   adopted standard, so only "Imperial" was prefixed. We used the
   "Metric" name to denote SI Length units.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants