Skip to content

test+docs: error-side exhaustiveness for enum and sealed error types#86

Merged
valbeat merged 4 commits into
mainfrom
test/error-type-exhaustiveness
Jun 18, 2026
Merged

test+docs: error-side exhaustiveness for enum and sealed error types#86
valbeat merged 4 commits into
mainfrom
test/error-type-exhaustiveness

Conversation

@valbeat

@valbeat valbeat commented Jun 18, 2026

Copy link
Copy Markdown
Owner

概要

エラー型(Result<T, E>E)が native enum / @phpstan-sealed union の場合、エラー値を網羅的に分岐できることを assertType 型テストでピン留めし、README にも明記しました。

テスト(tests/Types/result.php

フィクスチャ: HttpError(enum), AppError(@phpstan-sealed)・ValidationFailure/NetworkFailure

テスト 検証内容
testEnumErrorExhaustiveness isErr() 経由なら unwrapErr() が enum 型を保ち、全ケースの match(default なし)が網羅と認識される
testSealedErrorExhaustiveness sealed union のエラーに対する match(true)+instanceof が網羅と認識される(エラークラスは非ジェネリックなので型引数喪失なし)
testInstanceofErrLosesErrorType 落とし穴のピン留め: instanceof 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-sealed union なら、エラー値の網羅分岐を PHPStan が強制する
  • エラー値は isErr()+unwrapErr()match()err アームで取り出す(instanceof Err は E を mixed に落とすので不可)
  • エラークラス自体は非ジェネリックなので、その instanceof には型引数喪失は起きない

確認

  • phpstan analyse(level max): No errors
  • phpunit: 69 tests OK(既存退行なし)
  • php-cs-fixer --dry-run: 整形差分なし

@valbeat valbeat self-assigned this Jun 18, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

codecov Bot commented Jun 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (b6fd968) to head (aa02daa).
✅ All tests successful. No failed tests found.

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           
Flag Coverage Δ
unittests 100.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@coderabbitai

coderabbitai Bot commented Jun 18, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@valbeat, we couldn't start this review because you've reached your PR review rate limit.

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 @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: fda6fdda-cc48-41d1-b4ba-671eb1fb3056

📥 Commits

Reviewing files that changed from the base of the PR and between 56eb783 and aa02daa.

📒 Files selected for processing (2)
  • README.md
  • tests/Types/result.php

Walkthrough

tests/Types/result.phpHttpError native enum と @phpstan-sealedAppError union フィクスチャを追加し、isErr()/unwrapErr() 経由の match 網羅性検証と instanceof Err 分岐での型落ち確認を行う3つのテスト関数を追加した。

Changes

エラー型網羅性テスト

Layer / File(s) Summary
テスト用フィクスチャ定義
tests/Types/result.php
HttpError native enum(NotFound/Forbidden)と、@phpstan-sealed 注釈付きの AppError interface、それを実装する ValidationFailure/NetworkFailure クラスを追加。
match 網羅性テスト関数
tests/Types/result.php
testEnumErrorExhaustiveness(enum の default なし match)、testSealedErrorExhaustiveness(sealed union の match(true))、testInstanceofErrLosesErrorTypeinstanceof Err 分岐での unwrapErr()mixed になることを assertType で確認)の3関数を追加。

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • valbeat/php-result#7: src/Result.phpisErr/unwrapErr 条件型アサーションに対して、本PRのテストが直接対応している。
  • valbeat/php-result#76: @phpstan-sealed Ok|Err の型絞り込み挙動と既存の網羅的 match テストの上に、本PRのテスト関数群が積み上げられている。
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed タイトルは、PR全体の主要な変更内容(enum及び@phpstan-sealed型のエラー側網羅性テスト)を正確かつ簡潔に表現しており、開発者が変更の意図を明確に理解できます。

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch test/error-type-exhaustiveness

Warning

Review ran into problems

🔥 Problems

Git: Failed to clone repository. Please run the @coderabbitai full review command to re-trigger a full review. If the issue persists, set path_filters to include or exclude specific files.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@valbeat valbeat changed the title test: verify error-side exhaustiveness for enum and sealed error types test+docs: error-side exhaustiveness for enum and sealed error types Jun 18, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between b6fd968 and 56eb783.

📒 Files selected for processing (1)
  • tests/Types/result.php

Comment thread tests/Types/result.php
@valbeat valbeat marked this pull request as ready for review June 18, 2026 15:07
Copilot AI review requested due to automatic review settings June 18, 2026 15:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 match behavior for enum and @phpstan-sealed error types.
  • Added fixtures demonstrating the known pitfall where instanceof Err drops E to mixed.
  • 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.

Comment thread README.md Outdated
Comment thread tests/Types/result.php
@valbeat valbeat merged commit 17120a5 into main Jun 18, 2026
9 checks passed
@valbeat valbeat deleted the test/error-type-exhaustiveness branch June 18, 2026 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants