hereldar/results

Allows developers to handle the results in any way they choose.

0.9.0 2023-10-16 21:32 UTC

This package is auto-updated.

Last update: 2024-04-29 12:27:52 UTC


README

PHP Code Coverage Type Coverage Psalm Level Packagist License

This package includes an opinionated version of the Result type of Rust. It is not intended to replicate the original type one-to-one, but to allow developers to handle the results in any way they choose.

Use examples

This Result type allows ignoring errors without a try-catch block:

$value = getValue()->or($default);

It also allows throwing the error as a regular exception:

doSomething()->orFail();

Or throwing a custom exception:

doSomething()->orThrow(new MyException());

More complex flows can be handled by concatenating operations:

$record = fetchRecord()
    ->andThen(updateIt(...))
    ->orElse(insertIt(...))
    ->orFail();

And much more:

doSomething()
    ->onFailure(logFailure(...))
    ->onSuccess(logSuccess(...))
    ->onSuccess(doSomethingElse(...));

Installation

Via Composer:

composer require hereldar/results

Development

Run the following commands from the project folder:

make tests
make static-analysis
make coding-standards

To execute:

Documentation

Credits

License

The MIT License (MIT). Please see LICENSE for more information.