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

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

rasuvaeff/result
================

Typed Result and Option primitives for PHP

v1.0.0(today)00BSD-3-ClausePHPPHP 8.3 - 8.5CI passing

Since Jun 27Pushed todayCompare

[ Source](https://github.com/rasuvaeff/result)[ Packagist](https://packagist.org/packages/rasuvaeff/result)[ Docs](https://github.com/rasuvaeff/result)[ RSS](/packages/rasuvaeff-result/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (9)Versions (2)Used By (0)

rasuvaeff/result
================

[](#rasuvaeffresult)

[![Latest Stable Version](https://camo.githubusercontent.com/ab8b5cacc111fde378f16dbc4bcb81af60799f058ebe07bf26cff6f9d334a2b6/68747470733a2f2f706f7365722e707567782e6f72672f7261737576616566662f726573756c742f76)](https://packagist.org/packages/rasuvaeff/result)[![Total Downloads](https://camo.githubusercontent.com/1b27ef77acb0adfcfb7e00a0adf1d69e3e48c5b4d1b465dbb8df13352c3b0ae3/68747470733a2f2f706f7365722e707567782e6f72672f7261737576616566662f726573756c742f646f776e6c6f616473)](https://packagist.org/packages/rasuvaeff/result)[![Build](https://github.com/rasuvaeff/result/actions/workflows/build.yml/badge.svg)](https://github.com/rasuvaeff/result/actions/workflows/build.yml)[![Static analysis](https://github.com/rasuvaeff/result/actions/workflows/static-analysis.yml/badge.svg)](https://github.com/rasuvaeff/result/actions/workflows/static-analysis.yml)[![Psalm level](https://camo.githubusercontent.com/68f7f31799f2b93c710b14ba3877072e7fe07ec9d7cee3fdf67e14beab3e1b6f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7073616c6d2d6c6576656c5f312d626c75652e737667)](https://github.com/rasuvaeff/result/actions/workflows/static-analysis.yml)[![PHP](https://camo.githubusercontent.com/ce7d55727e06833e80a5fa2bc44881e2ede4e97dfb468af546ec486abf9a9c77/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f7261737576616566662f726573756c742f706870)](https://packagist.org/packages/rasuvaeff/result)[![License](https://camo.githubusercontent.com/6cb285b57819f8de0acfb34923298f4f569f962544e8fe35331da2d163f4e485/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4253442d2d332d2d436c617573652d626c75652e737667)](LICENSE.md)

Typed `Result` and `Option` primitives for PHP 8.3+. The package is framework-free and designed to keep Psalm/PHPStan generic inference useful in application code.

> Using an AI coding assistant? [llms.txt](llms.txt) contains a compact API reference you can share with the model.

Requirements
------------

[](#requirements)

- PHP 8.3+
- No runtime dependencies

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

[](#installation)

```
composer require rasuvaeff/result
```

Usage
-----

[](#usage)

```
use Rasuvaeff\Result\Option;
use Rasuvaeff\Result\Result;

$user = Result::fromThrowable(
    fn (): array => ['id' => 42, 'email' => 'user@example.com'],
)
    ->map(fn (array $row): string => $row['email'])
    ->unwrapOr(default: 'anonymous@example.com');

$name = Option::fromNullable(value: getenv('USER_NAME') ?: null)
    ->filter(fn (string $value): bool => $value !== '')
    ->unwrapOr(default: 'guest');
```

### Result

[](#result)

`Result` represents either a successful `Ok` value or an `Err` error. `unwrap()` returns the value for `Ok` and throws `UnwrapException` for `Err`.

```
use Rasuvaeff\Result\Result;

$result = Result::ok(value: 21)
    ->map(fn (int $value): int => $value * 2)
    ->flatMap(fn (int $value): Result => $value > 40
        ? Result::ok(value: $value)
        : Result::err(error: 'too small'));

$message = $result->match(
    ok: fn (int $value): string => "Value: {$value}",
    err: fn (string $error): string => "Error: {$error}",
);
```

MethodDescription`Result::ok($value)`Creates an `Ok` result.`Result::err($error)`Creates an `Err` result.`Result::fromThrowable($fn)`Runs a closure and converts thrown `Throwable` to `Err`.`isOk()` / `isErr()`Checks the active branch.`unwrap()`Returns the `Ok` value or throws `UnwrapException`.`unwrapOr($default)`Returns the `Ok` value or the supplied default.`map($fn)`Transforms the `Ok` value.`mapErr($fn)`Transforms the `Err` error.`flatMap($fn)`Chains a closure that returns another `Result`.`match(ok: $ok, err: $err)`Folds both branches to one return value.`value()` / `error()`Returns the branch payload or `null`.`flatMap()` keeps the error channel fixed: the `Result` returned by the closure must carry the same `E` error type as the receiver. Use `mapErr()` first if you need to align error types before chaining.

### Option

[](#option)

`Option` represents either `Some` or `None`. `Option::fromNullable()`converts only `null` to `None`; falsey values such as `0`, `''`, and `false`remain `Some`.

```
use Rasuvaeff\Result\Option;

$port = Option::fromNullable(value: getenv('PORT') ?: null)
    ->map(fn (string $value): int => (int) $value)
    ->filter(fn (int $value): bool => $value > 0)
    ->toResult(error: 'PORT is missing or invalid');
```

MethodDescription`Option::some($value)`Creates a `Some` option.`Option::none()`Creates a `None` option.`Option::fromNullable($value)`Converts `null` to `None`, other values to `Some`.`isSome()` / `isNone()`Checks the active branch.`unwrap()`Returns the `Some` value or throws `UnwrapException`.`unwrapOr($default)`Returns the `Some` value or the supplied default.`map($fn)`Transforms the `Some` value.`filter($predicate)`Keeps `Some` only when the predicate returns true.`toResult($error)`Converts `Some` to `Ok` and `None` to `Err`.Security
--------

[](#security)

This package does not execute I/O, SQL, shell commands, reflection, or dynamic code. It only stores values and calls closures supplied by your application. Exceptions thrown by closures passed to `map()`, `mapErr()`, `flatMap()`, `match()`, or `filter()` are not swallowed; use `Result::fromThrowable()` at the boundary where exception-to-value conversion is desired.

Examples
--------

[](#examples)

See [examples/](examples/) for runnable scripts.

ScriptShowsNeeds server?`basic.php``Result`, `Option`, `fromThrowable()`, and `toResult()` usageNoDevelopment
-----------

[](#development)

No PHP/Composer on the host. Run commands in Docker via the `composer:2` image:

```
docker run --rm -v "$PWD":/app -w /app composer:2 composer install
docker run --rm -v "$PWD":/app -w /app composer:2 composer build
docker run --rm -v "$PWD":/app -w /app composer:2 composer cs:fix
docker run --rm -v "$PWD":/app -w /app composer:2 composer test
docker run --rm -v "$PWD":/app -w /app composer:2 composer release-check
```

Or with Make:

```
make install
make build
make cs-fix
make test
make test-coverage
make mutation
make release-check
```

`make test-coverage` and `make mutation` bootstrap `pcov` inside the `composer:2` container because the base image has no coverage driver.

License
-------

[](#license)

[BSD-3-Clause](LICENSE.md)

###  Health Score

41

—

FairBetter than 87% of packages

Maintenance100

Actively maintained with recent releases

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 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

Unknown

Total

1

Last Release

0d ago

### Community

Maintainers

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

---

Top Contributors

[![rasuvaeff](https://avatars.githubusercontent.com/u/1352718?v=4)](https://github.com/rasuvaeff "rasuvaeff (2 commits)")

---

Tags

functional-programmingimmutablephpphpstanpsalm

###  Code Quality

Static AnalysisPsalm, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[eve/dto

Simplistic, flexible Data Transfer Object library

1214.8k](/packages/eve-dto)[elegantly/laravel-kpi

Advanced KPI for your Laravel application

174.4k1](/packages/elegantly-laravel-kpi)

PHPackages © 2026

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