{"id":36986244,"url":"https://github.com/valbeat/php-result","last_synced_at":"2026-01-13T23:04:11.208Z","repository":{"id":302916118,"uuid":"1009485712","full_name":"valbeat/php-result","owner":"valbeat","description":"Result type for PHP","archived":false,"fork":false,"pushed_at":"2026-01-05T02:19:36.000Z","size":133,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-01-06T18:03:29.785Z","etag":null,"topics":["php","result-pattern","result-type"],"latest_commit_sha":null,"homepage":"https://packagist.uihtm.com/packages/valbeat/result","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/valbeat.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-06-27T07:58:02.000Z","updated_at":"2026-01-05T02:19:32.000Z","dependencies_parsed_at":"2025-11-28T17:05:39.058Z","dependency_job_id":null,"html_url":"https://github.com/valbeat/php-result","commit_stats":null,"previous_names":["valbeat/php-result"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/valbeat/php-result","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valbeat%2Fphp-result","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valbeat%2Fphp-result/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valbeat%2Fphp-result/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valbeat%2Fphp-result/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/valbeat","download_url":"https://codeload.github.com/valbeat/php-result/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/valbeat%2Fphp-result/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28405121,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-13T21:51:37.118Z","status":"ssl_error","status_checked_at":"2026-01-13T21:45:14.585Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["php","result-pattern","result-type"],"created_at":"2026-01-13T23:04:10.695Z","updated_at":"2026-01-13T23:04:11.200Z","avatar_url":"https://github.com/valbeat.png","language":"PHP","readme":"# PHP Result\n\n![Packagist Version](https://img.shields.io/packagist/v/valbeat/result)\n[![codecov](https://codecov.io/gh/valbeat/php-result/branch/main/graph/badge.svg)](https://codecov.io/gh/valbeat/php-result)\n\nA Result type implementation for PHP inspired by Rust's `Result\u003cT, E\u003e` type.\n\nThis library provides a robust way to handle operations that might fail, without relying on exceptions. It encourages explicit error handling and makes it impossible to accidentally ignore errors.\n\n## Installation\n\n```bash\ncomposer require valbeat/result\n```\n\n## Requirements\n\n- PHP 8.4 or higher\n- Composer\n\n## Basic Usage\n\n```php\nuse Valbeat\\Result\\Ok;\nuse Valbeat\\Result\\Err;\nuse Valbeat\\Result\\Result;\n\n// Creating Results\n$success = new Ok(42);\n$failure = new Err(\"Something went wrong\");\n\n// Pattern matching with match expression\n$message = $success-\u003ematch(\n    ok: fn($value) =\u003e \"Success: $value\",\n    err: fn($error) =\u003e \"Error: $error\"\n);\necho $message; // \"Success: 42\"\n\n// Checking if a Result is Ok or Err\nif ($success-\u003eisOk()) {\n    echo \"Operation succeeded!\";\n}\n\nif ($failure-\u003eisErr()) {\n    echo \"Operation failed!\";\n}\n\n// Unwrapping values (throws exception on error)\n$value = $success-\u003eunwrap(); // 42\n// $failure-\u003eunwrap(); // throws LogicException\n\n// Safe unwrapping with default values\n$value = $failure-\u003eunwrapOr(0); // 0\n$value = $failure-\u003eunwrapOrElse(fn($err) =\u003e strlen($err)); // 19\n```\n\n## Advanced Usage\n\n### Transforming Results\n\n```php\n// Map over success values\n$result = (new Ok(5))\n    -\u003emap(fn($x) =\u003e $x * 2)\n    -\u003emap(fn($x) =\u003e $x + 1);\necho $result-\u003eunwrap(); // 11\n\n// Map over error values\n$result = (new Err(\"error\"))\n    -\u003emapErr(fn($e) =\u003e strtoupper($e));\necho $result-\u003eunwrapErr(); // \"ERROR\"\n```\n\n### Chaining Operations\n\n```php\n// Chain operations that might fail\n$result = (new Ok(10))\n    -\u003eandThen(fn($x) =\u003e $x \u003e 5 ? new Ok($x * 2) : new Err(\"Too small\"))\n    -\u003eandThen(fn($x) =\u003e new Ok($x + 5));\necho $result-\u003eunwrap(); // 25\n\n// Short-circuit on first error\n$result = (new Ok(2))\n    -\u003eandThen(fn($x) =\u003e $x \u003e 5 ? new Ok($x * 2) : new Err(\"Too small\"));\necho $result-\u003eunwrapErr(); // \"Too small\"\n```\n\n### Combining Results\n\n```php\n// Use first Ok value\n$result = (new Err(\"first error\"))\n    -\u003eor(new Err(\"second error\"))\n    -\u003eor(new Ok(42));\necho $result-\u003eunwrap(); // 42\n\n// Use first Ok or call function\n$result = (new Err(\"error\"))\n    -\u003eorElse(fn($e) =\u003e new Ok(strlen($e)));\necho $result-\u003eunwrap(); // 5\n```\n\n### Side Effects\n\n```php\n// Inspect values without consuming the Result\n$result = (new Ok(42))\n    -\u003einspect(fn($x) =\u003e print(\"Value is: $x\\n\"))\n    -\u003emap(fn($x) =\u003e $x * 2);\n\n// Inspect errors\n$result = (new Err(\"oops\"))\n    -\u003einspectErr(fn($e) =\u003e error_log(\"Error occurred: $e\"));\n```\n\n## API Reference\n\n### Result Methods\n\nAll Result types (both Ok and Err) implement these methods:\n\n#### Type Checking\n- `isOk(): bool` - Returns true if the Result is Ok\n- `isOkAnd(callable $fn): bool` - Returns true if the Result is Ok and the predicate returns true\n- `isErr(): bool` - Returns true if the Result is Err\n- `isErrAnd(callable $fn): bool` - Returns true if the Result is Err and the predicate returns true\n\n#### Value Extraction\n- `unwrap(): mixed` - Returns the success value or throws LogicException\n- `unwrapErr(): mixed` - Returns the error value or throws LogicException\n- `unwrapOr(mixed $default): mixed` - Returns the success value or a default\n- `unwrapOrElse(callable $fn): mixed` - Returns the success value or computes it from the error\n\n#### Transformation\n- `map(callable $fn): Result` - Maps a Result\u003cT, E\u003e to Result\u003cU, E\u003e by applying a function to the success value\n- `mapErr(callable $fn): Result` - Maps a Result\u003cT, E\u003e to Result\u003cT, F\u003e by applying a function to the error value\n- `mapOr(mixed $default, callable $fn): mixed` - Maps the success value or returns a default\n- `mapOrElse(callable $defaultFn, callable $fn): mixed` - Maps the success value or computes a default from the error\n\n#### Combination\n- `and(Result $res): Result` - Returns the second Result if the first is Ok, otherwise returns the first Err\n- `andThen(callable $fn): Result` - Chains another operation that returns a Result\n- `or(Result $res): Result` - Returns the first Ok or the second Result if the first is Err\n- `orElse(callable $fn): Result` - Returns the first Ok or calls a function with the error to produce a Result\n\n#### Side Effects\n- `inspect(callable $fn): Result` - Calls a function with the success value if Ok\n- `inspectErr(callable $fn): Result` - Calls a function with the error value if Err\n\n#### Pattern Matching\n- `match(callable $okFn, callable $errFn): mixed` - Pattern match on the Result\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvalbeat%2Fphp-result","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvalbeat%2Fphp-result","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvalbeat%2Fphp-result/lists"}