feat: add expect() and expectErr()#95
Conversation
expect(string $message) returns the success value or throws LogicException with the caller-supplied message; expectErr() is the mirror for the error side, matching Rust's Result::expect/expect_err. Currently RED: the methods do not exist yet. Claude-Session: https://claude.ai/code/session_017XTM7pxbWPVNLV639i5WgK
Rust's Result::expect/expect_err equivalents: return the value or throw LogicException with a caller-supplied message that explains why the value was expected to be present. Conditional return types match unwrap()/unwrapErr() (never on the impossible side). Claude-Session: https://claude.ai/code/session_017XTM7pxbWPVNLV639i5WgK
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. 🗂️ Base branches to auto review (1)
Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #95 +/- ##
===========================================
Coverage 100.00% 100.00%
- Complexity 40 44 +4
===========================================
Files 2 2
Lines 80 88 +8
===========================================
+ Hits 80 88 +8
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
There was a problem hiding this comment.
Code Review
This pull request introduces the expect and expectErr methods to the Result interface and its implementations, Ok and Err. These methods allow users to extract values or throw a LogicException with a custom error message. The changes are well-documented in the README, fully tested with unit tests, and include static analysis type assertions to ensure correct type resolution. There are no review comments, and I have no additional feedback to provide.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
There was a problem hiding this comment.
Pull request overview
This PR adds Rust-like expect() / expectErr() APIs to the Result abstraction to let callers attach contextual failure messages when extracting Ok/Err values.
Changes:
- Add
Result::expect(string $message)andResult::expectErr(string $message)with conditional return types matchingunwrap()/unwrapErr(). - Implement the methods in
OkandErr(return value on the “correct” variant; throwLogicException($message)on the “wrong” variant). - Add runtime tests and PHPStan type-level assertions, and update the README API reference.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| tests/Types/result.php | Adds PHPStan assertType() coverage for expect() / expectErr() conditional return types on a generic Result<T,E> receiver. |
| tests/OkTest.php | Adds behavior tests for Ok->expect() returning the value and Ok->expectErr() throwing with the provided message. |
| tests/ErrTest.php | Adds behavior tests for Err->expect() throwing with the provided message and Err->expectErr() returning the error value. |
| src/Result.php | Extends the Result interface with expect() / expectErr() and their conditional return types. |
| src/Ok.php | Implements expect() (returns stored value) and expectErr() (throws LogicException($message)). |
| src/Err.php | Implements expect() (throws LogicException($message)) and expectErr() (returns stored error). |
| README.md | Documents expect() / expectErr() in the API Reference “Value Extraction” list. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| - `expect(string $message): mixed` - Returns the success value or throws LogicException with the given message | ||
| - `expectErr(string $message): mixed` - Returns the error value or throws LogicException with the given message | ||
| - `unwrapOr(mixed $default): mixed` - Returns the success value or a default |
Stack this PR on #94 per review, resolving overlapping test and README hunks. expect()/expectErr() switch to UnwrapException in the following commits. Claude-Session: https://claude.ai/code/session_017XTM7pxbWPVNLV639i5WgK
Review feedback: expect()/expectErr() throwing bare LogicException while unwrap()/unwrapErr() throw UnwrapException leaves an inconsistent contract — catch (UnwrapException) would miss expect() failures — and drops the value context Rust's expect includes. Currently RED. Claude-Session: https://claude.ai/code/session_017XTM7pxbWPVNLV639i5WgK
|
#94 のブランチ削除に伴い自動クローズされました(クローズ済み PR は base 変更不可)。同一ブランチ・同一内容の後継 PR を main ベースで作成しています → 上記タイムライン参照。 |
概要
コードレビューで提案した Rust API とのギャップ解消の 1 つ目、
expect()/expectErr()の追加です。unwrap()と違い、呼び出し側が「なぜ値があるはずなのか」を説明するメッセージを付けられるため、失敗時の原因調査が容易になります(Rust で常用されるパターン)。変更内容
Result::expect(string $message)— 成功値を返す。Err なら$messageでLogicExceptionを throwResult::expectErr(string $message)— エラー値を返す。Ok なら$messageでLogicExceptionを throwunwrap()/unwrapErr()と同じ(不可能側はnever、具象Ok<T>/Err<E>レシーバで正確な型)備考
expectはメッセージに加えてエラー値の Debug 表現も含めます。feat: throw UnwrapException with value context from unwrap()/unwrapErr() #94(UnwrapException)がマージされたら、この 2 メソッドも同例外+値要約付きメッセージに揃える follow-up を推奨します(独立してレビューできるよう本 PR では素のLogicExceptionに留めています)。TDD
assertType)を先に追加し RED を確認してコミットhttps://claude.ai/code/session_017XTM7pxbWPVNLV639i5WgK