Download the PHP package martyrer/throwless-php without Composer
On this page you can find all versions of the php package martyrer/throwless-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download martyrer/throwless-php
More information about martyrer/throwless-php
Files in martyrer/throwless-php
Package throwless-php
Short Description A PHP library for exception-free error handling using the Result monad pattern
License MIT
Informations about the package throwless-php
Throwless PHP
A PHP implementation of the Result type pattern, inspired by Rust's Result and JavaScript's neverthrow library. This library provides a type-safe way to handle errors without relying on exceptions, making your code more predictable and easier to reason about.
Features
- Type-safe error handling without exceptions
- Immutable Result type with
OkandErrvariants - Comprehensive set of methods for working with Results
- Pure functional programming approach
- PHP 8.2+ with strict typing support
Installation
Available Methods
-
Type Checking
isOk()- Check if the Result contains a success valueisErr()- Check if the Result contains an error valueisOkAnd(callable $fn)- Executes the closure and returns its result as a boolean if the result is OkisErrAnd(callable $fn)- Executes the closure and returns its result as a boolean if the result is Err
-
Transformation
map(callable $fn)- Transform the success valuemapErr(callable $fn)- Transform the error valueandThen(callable $fn)- Chain Result-returning operations
-
Matching
match(callable $okFn, callable $errFn)- Handle both success and error cases
-
Inspection
inspect(callable $fn)- Inspect the success valueinspectErr(callable $fn)- Inspect the error value
- Unwrapping
unwrap()- Get the success value or throwunwrapErr()- Get the error value or throwunwrapOr(mixed $default)- Get the success value or a defaultunwrapOrElse(callable $fn)- Get the success value or compute a defaultunwrapOrDefault(DefaultValueProvider $provider)- Get the success value or type's defaultexpect(string $msg)- Get the success value or throw with custom messageexpectErr(string $msg)- Get the error value or throw with custom messageunwrapUnchecked()- Get the success value without checking (unsafe)unwrapErrUnchecked()- Get the error value without checking (unsafe)
Basic Usage
Core Methods
Type Checking
isOk
Check if the Result contains a success value:
isErr
Check if the Result contains an error value:
isOkAnd
Check if Result is Ok and the value matches a predicate:
isErrAnd
Check if Result is Err and the error matches a predicate:
Transformation Methods
map
Transform the success value while preserving the error:
mapErr
Transform the error value while preserving the success:
andThen
Chain operations that might fail:
Matching
match
Handle both success and error cases:
Inspection Methods
inspect
Perform side effects on success value without consuming the Result:
inspectErr
Perform side effects on error value without consuming the Result:
Unwrapping Methods
unwrap
Get the success value or throw an exception:
unwrapErr
Get the error value or throw an exception:
unwrapOr
Get the value or a default:
unwrapOrElse
Get the value or compute a default:
unwrapOrDefault
Get the success value or the type's default value:
expect
Unwrap with a custom error message:
expectErr
Get the error value with a custom error message:
unwrapUnchecked
Get the success value without safety checks (use with caution):
unwrapErrUnchecked
Get the error value without safety checks (use with caution):
Real-world Examples
Example 1: User Authentication
Example 2: API Response Handling
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. Make sure to read the contributing guidelines before submitting your PR.
License
This project is licensed under the MIT License - see the LICENSE file for details.