Skip to content

docs: document instanceof narrowing limitation, recommend isOk()/isErr()#82

Merged
valbeat merged 1 commit into
mainfrom
docs/instanceof-narrowing-note
Jun 10, 2026
Merged

docs: document instanceof narrowing limitation, recommend isOk()/isErr()#82
valbeat merged 1 commit into
mainfrom
docs/instanceof-narrowing-note

Conversation

@valbeat

@valbeat valbeat commented Jun 10, 2026

Copy link
Copy Markdown
Owner

Summary

instanceof ベースの絞り込みでは型引数が失われる(PHPStan の既知の制限)ことを README と Result の PHPDoc に明記し、値を取り出す分岐では isOk() / isErr() を使うよう誘導する。ドキュメントのみの変更。

Changes

  • README.md: Type Safety セクションの Sealed interface の項に注記を追加 — instanceof は網羅性チェックには使えるが、Result<int, E> が型引数なしの Ok に絞られ unwrap()mixed になるため、値の取り出しには isOk() / isErr() を推奨
  • src/Result.php: インターフェースの class-level PHPDoc に同様の注意書きを追加(PHPDoc のみの変更でコードへの影響なし)

Notes

  • この挙動は test: add assertType coverage for untested type inference cases #80 の型テスト(testInstanceofOkNarrowing / testMatchArmNarrowing)で「既知の制限」としてピン留め済み。PHPStan 側が将来改善されればテストが落ちて検知できる
  • 既存の README は「instanceof Ok の else 分岐は Err に絞られる」とだけ書いており、型引数が失われる事実に触れていなかった
  • 根本対応は PHPStan 本体の改善待ち。近縁の issue: phpstan/phpstan#10553(open / feature-request)

Verification

  • Local (PHP 8.4.7): phpstan analyse(level max)✅ / php-cs-fixer fix --dry-run ✅(差分なし)

Related Issues

Refs #83 — 本制限のトラッキング issue。本PRはそのうち「README / PHPDoc への注記」を担当する。

Summary by CodeRabbit

リリースノート

  • Documentation
    • 型安全性に関する説明を強化し、PHPStanの型絞り込み動作に関する注意事項と推奨される実装パターンを明確にしました。

instanceof による絞り込みは PHPStan の制限で型引数が失われ、
unwrap() が mixed になる(tests/Types/result.php の
testInstanceofOkNarrowing / testMatchArmNarrowing でピン留め済み)。
README と Result の PHPDoc に注記し、値を取り出す分岐では
isOk()/isErr() を使うよう誘導する。
@valbeat valbeat self-assigned this Jun 10, 2026
@coderabbitai

coderabbitai Bot commented Jun 10, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

PHPStan動作のドキュメント化。Resultインターフェースと対応するREADMEセクションに、PHPStanがinstanceof Okによる型絞り込みでResult<T, E>のジェネリック型引数を失う既知制限と、代わりにisOk()/isErr()での分岐を推奨する旨を説明する注釈を追加しました。

Changes

PHPStan型絞り込み制限の説明

Layer / File(s) Summary
PHPStan instanceof型絞り込み制限に関するドキュメント追加
src/Result.php, README.md
ResultインターフェースのドキュメントコメントおよびREADMEの「Type Safety」セクションに、PHPStanがinstanceof Okで型を絞り込む際にジェネリック型引数を失いunwrap()の戻り型がmixedになる点を説明し、値取得時にはisOk()/isErr()での型絞り込みを使うべき旨を記載しました。

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3分

Possibly related PRs

  • valbeat/php-result#7: Result.php内のunwrap()/isOk()/isErr()に関するPHPStan型絞り込みの実装が初期に行われており、本PRはその既知制限をドキュメントで説明します。

  • valbeat/php-result#76: Result/Ok/Err@phpstan-sealed注釈と型合成・型絞り込み機構を扱った基盤PR。本PRはその上で顕在化したinstanceofによる型引数喪失の制限を明示しています。

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed PRのタイトルは変更の主要な内容を正確に反映している。instanceofによる型絞り込みの制限をドキュメント化し、isOk()/isErr()の使用を推奨するという主な変更内容が明確に示されている。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch docs/instanceof-narrowing-note

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.

@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 updates the documentation in README.md and the docblock in src/Result.php to warn users about a known PHPStan limitation where using instanceof for type narrowing causes type arguments to be lost. It advises using isOk() and isErr() instead when retrieving values. 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.

@codecov

codecov Bot commented Jun 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (e94b3f6) to head (20203d3).
⚠️ Report is 2 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@             Coverage Diff             @@
##                main       #82   +/-   ##
===========================================
  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.

@valbeat valbeat marked this pull request as ready for review June 10, 2026 13:01
Copilot AI review requested due to automatic review settings June 10, 2026 13:01
@valbeat valbeat merged commit 27470ee into main Jun 10, 2026
9 checks passed
@valbeat valbeat deleted the docs/instanceof-narrowing-note branch June 10, 2026 13:01

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

instanceof ベースの型絞り込みで PHPStan が型引数を失う既知の制限について、README と Result インターフェースの PHPDoc に明記し、値の取り出しでは isOk() / isErr() を推奨するようドキュメントを補強する PR です(コード変更なし)。

Changes:

  • README の Type Safety セクションに、instanceof 絞り込み時にジェネリクスが失われる注意書きを追加
  • src/Result.php の class-level PHPDoc に同様の注意書きを追加し、isOk() / isErr() での絞り込みへ誘導

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
README.md instanceof ナローイングの型引数喪失(PHPStan の既知の制限)を明記し、値取得には isOk()/isErr() を推奨
src/Result.php Result インターフェースの PHPDoc に同注意書きを追記

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Result.php
Comment on lines +10 to +12
* 注意: instanceof による絞り込みでは型引数が失われます(PHPStan の既知の制限。
* Result<int, E> が型引数なしの Ok になり unwrap() は mixed になる)。
* 値を取り出す分岐では isOk() / isErr() で絞り込んでください。
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