Skip to content

ArielEspinoza07/result-pattern

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

134 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Result Pattern

Build Status Total Downloads Latest Stable Version License


A modern, immutable implementation of the Result pattern for PHP 8.3+.

Requires PHP 8.3+


Features

  • Type-safe result handling with strict type hints and PHPStan level max
  • Immutable objects using PHP 8.3+ readonly classes
  • Fluent, chainable API: map β†’ flatMap β†’ recover β†’ fold
  • Full generic type inference via @template annotations
  • Comprehensive test suite with Pest PHP and type coverage

Project Structure

src/
β”œβ”€β”€ Failure.php
β”œβ”€β”€ Success.php
└── Result.php

Installation

composer require arielespinoza07/result-pattern

Quick Start

use ArielEspinoza07\ResultPattern\Result;

// Create results
$ok  = Result::success(42);
$err = Result::failure('something went wrong');

// Wrap a throwing operation
$result = Result::attempt(fn () => riskyOperation());

// Convert nullable to Result
$result = Result::fromNullable($user, new UserNotFoundException());

// Transform and compose
$value = Result::success(2)
    ->map(fn ($x) => $x * 10)
    ->flatMap(fn ($x) => Result::success($x + 5))
    ->getValueOr(0); // 25

// Handle both branches
$output = $result->fold(
    onSuccess: fn ($v) => "Got: {$v}",
    onFailure: fn ($e) => "Error: {$e}",
);

API Reference

Static constructors

Method Description
Result::success($value) Wraps a value in a Success
Result::failure($error) Wraps an error in a Failure
Result::attempt(callable, array $only = []) Executes a callable; catches Throwable as Failure. Pass $only to catch only specific exception types.
Result::fromNullable($value, $error) null β†’ Failure($error), non-null β†’ Success($value)
Result::zip(Result ...$results) All succeed β†’ Success([values]), first failure wins (fail-fast)
Result::collect(array $results) Processes all results; returns Failure([errors]) with every error collected (fail-all)

Instance methods

Method Description
isSuccess() true if Success
isFailure() true if Failure
getValue() Returns value (throws on Failure) β€” prefer getValueOr()
getError() Returns error (throws on Success) β€” prefer getErrorOr()
getValueOr($default) Safe value access with fallback
getErrorOr($default) Safe error access with fallback
toNullable() Success($v) β†’ $v, Failure β†’ null
map(callable) Transform the value; no-op on Failure
mapError(callable) Transform the error; no-op on Success
flatMap(callable) Chain a Result-returning callable on Success
flatMapError(callable) Chain a Result-returning callable on Failure
recover(callable) Convert Failure β†’ Success via fallback value
recoverWith(callable) Convert Failure β†’ Result (recovery can itself fail)
tap(callable) Run a side-effect on Success without modifying the result
onSuccess(callable) Run a side-effect on Success
onFailure(callable) Run a side-effect on Failure
fold(onSuccess, onFailure) Collapse both branches into a single value

Documentation


Development

Requirements

  • PHP 8.3+
  • Composer 2.0+

Setup

git clone https://github.com/ArielEspinoza07/result-pattern.git
cd result-pattern
composer install

Quality Tools

composer check       # lint + analyse + test (all-in-one)
composer lint        # check code style (Pint)
composer lint:fix    # fix code style
composer analyse     # static analysis (PHPStan level max)
composer test        # run tests (Pest PHP)
composer test:coverage # run tests with coverage report

Contributing

See CONTRIBUTING.md for setup instructions, code conventions, and PR guidelines.


License

MIT License

About

🎯 Modern PHP 8.3+ Result pattern library. Immutable objects, strict typing (PHPStan max), and full tests. Ideal for APIs, services, and web apps.

Topics

Resources

Contributing

Security policy

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages