PHPackages                             withinboredom/distance - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. withinboredom/distance

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

withinboredom/distance
======================

typesafe distance

v1.0.0(1y ago)401[1 PRs](https://github.com/withinboredom/distance/pulls)MITPHPPHP ^8.4CI failing

Since May 27Pushed 2mo agoCompare

[ Source](https://github.com/withinboredom/distance)[ Packagist](https://packagist.org/packages/withinboredom/distance)[ RSS](/packages/withinboredom-distance/feed)WikiDiscussions main Synced today

READMEChangelog (1)Dependencies (2)Versions (3)Used By (0)

An advanced library offering type safety and identity with Distance
===================================================================

[](#an-advanced-library-offering-type-safety-and-identity-with-distance)

Identity
--------

[](#identity)

This library uses some tricks to intern values so that two distances are always equal to one another, no matter the distance in time or space.

```
use Withinboredom\Distance;
use Withinboredom\Distance\Unit;

$meter = Distance::from(Unit::Meters, 1);
$centimeters = Distance::from(Unit::Centimeters, 100);

echo $meter === $centimeters ? 'true' : 'false'
// outputs: true
```

Type Safety
-----------

[](#type-safety)

You can ensure nobody will accidentally confuse inches with centimeters or Nautical Miles with Miles:

```
function moveForward(Distance $distance) { /* move */ }

moveForward(Distance::from(Unit::Miles, 10));
```

Conversions and Math
--------------------

[](#conversions-and-math)

You can convert between units and even perform operations, like sorting and arithmetic:

```
$meter = Meters(1);

$meter = $meter->multiply(10)->add(Inches(10)); // get 10 meters and 10 inches

echo Centimeters(10) < $meter ? 'true' : 'false';
// output: true
```

Support for Crell\\Serde
------------------------

[](#support-for-crellserde)

You cannot serialize/deserialize/clone `Distance` objects. However, if you use something like Serde, you can still serialize your value objects:

```
class Robot {
    public function __construct(
        #[Field('height_in_centimeters')]
        #[DistanceAs(Unit::Centimeters)]
        public Distance $height,
    ) {}
}

$serde = new SerdeCommon(handlers: new \Withinboredom\Distance\SerdeExporter());
$serde->serialize(new Robot(Meters(5)), 'json');
```

The above will be serialized (and deserialized) from:

```
{
    "height_in_centimeters": 500
}
```

Units
-----

[](#units)

There are several distance units supported

### SI Units

[](#si-units)

- Micrometers
- Millimeters
- Centimeters
- Meters
- Kilometers

### Imperial Units

[](#imperial-units)

- Inches
- Feet
- Yards
- Miles
- Furlongs

### Misc.

[](#misc)

- Nautical Miles
- Astronomical Units

FAQ
---

[](#faq)

> How far can I go?

The base unit is in micrometers, so on a 64-bit system you can go ~60AU with micrometer accuracy. For reference, Pluto is usually about 40 AU away from the sun.

If you are limited to 32-bit systems, this is not for you. The maximum distance on a 32-bit system is just a couple of meters.

> Why does this exist?

I don’t like [magic numbers](https://en.wikipedia.org/wiki/Magic_number_(programming)#:~:text=Magic%20numbers%20are%20common%20in%20programs%20across%20many,have%20such%20constants%20that%20identify%20the%20contained%20data.).

> How performant is this?

The main overhead is in autoloading and function-call overhead. Thus, if realtime performance is a concern, you might want to stick to magic numbers.

Developing
----------

[](#developing)

If you wish to create a PR or update the code here:

1. Clone the repo
2. `composer install` to install test dependencies
3. `yarn` to install git hooks for formatting
4. Open in favorite IDE.

Code Standards
--------------

[](#code-standards)

[Per](https://www.php-fig.org/per/coding-style/) coding styles are followed.

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance67

Regular maintenance activity

Popularity5

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 90.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

402d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1883296?v=4)[Rob Landers](/maintainers/withinboredom)[@withinboredom](https://github.com/withinboredom)

---

Top Contributors

[![withinboredom](https://avatars.githubusercontent.com/u/1883296?v=4)](https://github.com/withinboredom "withinboredom (28 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (2 commits)")[![Crell](https://avatars.githubusercontent.com/u/254863?v=4)](https://github.com/Crell "Crell (1 commits)")

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/withinboredom-distance/health.svg)

```
[![Health](https://phpackages.com/badges/withinboredom-distance/health.svg)](https://phpackages.com/packages/withinboredom-distance)
```

###  Alternatives

[magefan/module-geoip

Allows to get country code by IP

55253.7k1](/packages/magefan-module-geoip)[sensiolabs/minify-bundle

Assets Minifier (CSS, JS) for Symfony &amp; Minify integration in Asset Mapper

57155.0k3](/packages/sensiolabs-minify-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
