PHPackages                             ciarancoza/option-result - 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. ciarancoza/option-result

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

ciarancoza/option-result
========================

Rust-like Option and Result Classes for PHP

v0.10.0(5mo ago)24MITPHPCI passing

Since Nov 22Pushed 5mo agoCompare

[ Source](https://github.com/knightspore/option-result)[ Packagist](https://packagist.org/packages/ciarancoza/option-result)[ RSS](/packages/ciarancoza-option-result/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (4)Versions (16)Used By (0)

option-result - Rust-style `Option` and `Result` Classes for PHP
================================================================

[](#option-result---rust-style-option-and-result-classes-for-php)

> ["So you can take down ~20% of the internet, but with PHP"](https://blog.cloudflare.com/18-november-2025-outage/#memory-preallocation)

This library contains two classes: `Option` and `Result`.

`Option` represents an optional value. An option may be `some` or `none`, where `some(T)` contains a value and `none` does not.

`Result` represents a success (`ok(T)`) or an error (`err(E)`).

Installation
------------

[](#installation)

```
composer require ciarancoza/option-result
```

Usage
-----

[](#usage)

### Option

[](#option)

```
function findUser(int $id): Option {
    $user = User::find($id);
    return $user ? Option::Some($user) : Option::None();
}

function getUserTheme(int $userId): string {
    return findUser($userId)
        ->map(fn ($user) => $user->theme)
        ->map(fn ($theme) => strtolower($theme))
        ->unwrapOr('auto');
}
```

### Result

[](#result)

```
function fetchOrgData(int $id): Result {
    try {
        $response = Http::get("/api/orgs/{$id}");
        if ($response->failed()) return Result::Err("API request failed: " . $response->status());
        return Result::Ok($response->json());
    } catch (Exception $e) {
        return Result::Err("Connection error: " . $e->getMessage());
    }
}

function getActiveEmails(int $orgId): array {
    return fetchOrgData($orgId)
        ->map(fn ($org) => $org->getEmails())
        ->map(fn ($emails) => $emails['active'])
        ->mapErr(fn ($error) => "Failed to get active emails: " . $error)
        ->mapErr(fn ($error) => Log::error($error))
        ->unwrapOr([]);
}
```

You can view [the generated documentation](https://github.com/knightspore/option-result/tree/main/docs) for more usage details.

Road Map
--------

[](#road-map)

- Result
    - TODO
        - `inspect()`
        - `inspectErr()`
        - `or()`
        - `tryCatch()`
    - Refactor for v1

Contributing
------------

[](#contributing)

Go for it! There are plenty of useful Option / Result features in Rust we could implement in this library.

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance72

Regular maintenance activity

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity33

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% 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

Every ~0 days

Total

15

Last Release

161d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a562c2f861a1bed268792fa74f43eb12d24fe4d9dd070b63ae9eabe6a537c803?d=identicon)[ciarancoza](/maintainers/ciarancoza)

---

Top Contributors

[![knightspore](https://avatars.githubusercontent.com/u/51886151?v=4)](https://github.com/knightspore "knightspore (46 commits)")

---

Tags

maybephprust

###  Code Quality

TestsPHPUnit

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/ciarancoza-option-result/health.svg)

```
[![Health](https://phpackages.com/badges/ciarancoza-option-result/health.svg)](https://phpackages.com/packages/ciarancoza-option-result)
```

PHPackages © 2026

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