test+docs: error-side exhaustiveness for enum and sealed error types#86
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds type-level tests to tests/Types/result.php to verify and document error exhaustiveness behavior under PHPStan. It demonstrates that checking isErr() preserves specific types for native enums and @phpstan-sealed union interfaces, while documenting a known limitation where using instanceof Err loses the error type, resulting in mixed. There are no review comments, so I have no 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.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #86 +/- ##
===========================================
Coverage 100.00% 100.00%
Complexity 40 40
===========================================
Files 2 2
Lines 80 80
===========================================
Hits 80 80
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
|
Warning Review limit reached
More reviews will be available in 25 minutes and 2 seconds. Learn how PR review limits work. Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file). ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits. 🚦 How do rate limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan refill rate. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, the refill rate gradually slows as usage increases. The highest same-day bursts are limited more strictly. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (2)
Walkthrough
Changesエラー型網羅性テスト
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Warning Review ran into problems🔥 ProblemsGit: Failed to clone repository. Please run the 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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@tests/Types/result.php`:
- Around line 420-430: The test function testInstanceofErrLosesErrorType only
validates the instanceof Err type loss with the enum variant Result<int,
HttpError>, but the docstring claims this behavior occurs for both enum and
sealed classes. Add a complementary test function that validates the same type
loss behavior using the sealed class variant Result<int, AppError> to ensure
test coverage aligns with the documented behavior and improves edge case
regression detection for both type variants.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 4c186b55-a769-431f-8661-19df7b43f228
📒 Files selected for processing (1)
tests/Types/result.php
There was a problem hiding this comment.
Pull request overview
This PR pins (via PHPStan assertType fixtures) that Result<T, E> preserves error-side type information well enough to allow exhaustive matching when E is a native enum or a @phpstan-sealed union, and documents the recommended narrowing/extraction patterns in the README.
Changes:
- Added new type fixtures validating exhaustive
matchbehavior forenumand@phpstan-sealederror types. - Added fixtures demonstrating the known pitfall where
instanceof ErrdropsEtomixed. - Documented “Exhaustive error matching” guidance in the Type Safety section of the README.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| tests/Types/result.php | Adds enum/sealed-union error fixtures and PHPStan assertType checks to pin exhaustive matching behavior and the instanceof Err pitfall. |
| README.md | Documents exhaustive error matching guidance and recommended extraction/narrowing patterns. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
概要
エラー型(
Result<T, E>のE)が native enum /@phpstan-sealedunion の場合、エラー値を網羅的に分岐できることを assertType 型テストでピン留めし、README にも明記しました。テスト(
tests/Types/result.php)フィクスチャ:
HttpError(enum),AppError(@phpstan-sealed)・ValidationFailure/NetworkFailuretestEnumErrorExhaustivenessisErr()経由ならunwrapErr()が enum 型を保ち、全ケースのmatch(default なし)が網羅と認識されるtestSealedErrorExhaustivenessmatch(true)+instanceofが網羅と認識される(エラークラスは非ジェネリックなので型引数喪失なし)testInstanceofErrLosesErrorTypeinstanceof Errでは E が失われ enum/sealed でもunwrapErr()がmixed厳密性の確認:
HttpError::Forbiddenアームを一時的に外すとMatch expression does not handle remaining value:で解析エラーになることを確認済み(ケース漏れが検出される=網羅が強制されている)。ドキュメント(
README.md)Type Safety セクションに Exhaustive error matching の項目を追加:
Eが enum /@phpstan-sealedunion なら、エラー値の網羅分岐を PHPStan が強制するisErr()+unwrapErr()かmatch()のerrアームで取り出す(instanceof Errは E をmixedに落とすので不可)instanceofには型引数喪失は起きない確認
phpstan analyse(level max): No errorsphpunit: 69 tests OK(既存退行なし)php-cs-fixer --dry-run: 整形差分なし