Download the PHP package rasuvaeff/result without Composer
On this page you can find all versions of the php package rasuvaeff/result. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rasuvaeff/result
More information about rasuvaeff/result
Files in rasuvaeff/result
Package result
Short Description Typed Result and Option primitives for PHP
License BSD-3-Clause
Homepage https://github.com/rasuvaeff/result
Informations about the package result
rasuvaeff/result
Typed Result<T, E> and Option<T> 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 contains a compact API reference you can share with the model.
Requirements
- PHP 8.3+
- No runtime dependencies
Installation
Usage
Result
Result<T, E> represents either a successful Ok<T> value or an Err<E> error.
unwrap() returns the value for Ok and throws UnwrapException for Err.
| Method | Description |
|---|---|
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<T> represents either Some<T> or None. Option::fromNullable()
converts only null to None; falsey values such as 0, '', and false
remain Some.
| Method | Description |
|---|---|
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
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
See examples/ for runnable scripts.
| Script | Shows | Needs server? |
|---|---|---|
basic.php |
Result, Option, fromThrowable(), and toResult() usage |
No |
Development
No PHP/Composer on the host. Run commands in Docker via the composer:2 image:
Or with Make:
make test-coverage and make mutation bootstrap pcov inside the
composer:2 container because the base image has no coverage driver.
License
BSD-3-Clause