texthtml/maybe

Option & Result types

v0.6.0 2023-07-22 22:01 UTC

README

Help yourself by not returning null, false, -1 or throwing exception for everything by using Option & Result instead.

Using those makes it harder to make mistake, make it easy to do common operations on unknown returned values and can help static analysis tool detect incorrect logic in PHP code.

Installation

composer req texthtml/maybe

Documentation

Read the documentation full API description, detailed explainations and usages.

Usage

Option

TH\Maybe\Option is a type that represents an optional value: every Option is either Some and contains a value, or None, and does not.

/**
 * @return Option<float>
 */
function divide(float $numerator, float $denominator): Option {
    return match ($denomintor) {
        0.0 => Option\none(),
        default => Option\some($numerator / $denominator),
    };
}

// The return value of the function is an option
$result = divide(2.0, 3.0);

// Pattern match to retrieve the value
if ($result->isSome()) {
    // The division was valid
    echo "Result: {$option->unwrap()}";
} else {
    // The division was invalid
    echo "Cannot divide by 0";
}

Result

TH\Maybe\Result is a type that represents either success (Ok), containing the result of an operation or failure (Err), containing the reason of the failure.

/**
 * @return Result<int,string>
 */
function parse_version(string $header): Result {
    return match ($header[0] ?? null) {
        null => Result\err("invalid header length"),
        "1" => Result\ok(1),
        "2" => Result\ok(2),
        default => Result\err("invalid version"),
    };
}

$version = parse_version("1.x");
if ($version->isOk()) {
    echo "working with version: {$version->unwrap()}";
} else {
    echo "error parsing header: {$version->unwrapErr()}";
}
// @prints working with version: 1