docs: document instanceof narrowing limitation, recommend isOk()/isErr()#82
Conversation
instanceof による絞り込みは PHPStan の制限で型引数が失われ、 unwrap() が mixed になる(tests/Types/result.php の testInstanceofOkNarrowing / testMatchArmNarrowing でピン留め済み)。 README と Result の PHPDoc に注記し、値を取り出す分岐では isOk()/isErr() を使うよう誘導する。
WalkthroughPHPStan動作のドキュメント化。 ChangesPHPStan型絞り込み制限の説明
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3分 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 docstrings
🧪 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 |
There was a problem hiding this comment.
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 Report✅ All modified and coverable lines are covered by tests. 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
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.
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.
| * 注意: instanceof による絞り込みでは型引数が失われます(PHPStan の既知の制限。 | ||
| * Result<int, E> が型引数なしの Ok になり unwrap() は mixed になる)。 | ||
| * 値を取り出す分岐では isOk() / isErr() で絞り込んでください。 |
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
testInstanceofOkNarrowing/testMatchArmNarrowing)で「既知の制限」としてピン留め済み。PHPStan 側が将来改善されればテストが落ちて検知できるinstanceof Okの else 分岐はErrに絞られる」とだけ書いており、型引数が失われる事実に触れていなかったVerification
phpstan analyse(level max)✅ /php-cs-fixer fix --dry-run✅(差分なし)Related Issues
Refs #83 — 本制限のトラッキング issue。本PRはそのうち「README / PHPDoc への注記」を担当する。
Ok<int>への絞り込みが本来 sound である論拠・アップストリーム関連 issue)は instanceof ベースの絞り込みで Result の型引数が失われる(PHPStan の制限) #83 を参照ClosesではなくRefsなのは意図的: 根本対応は PHPStan 本体の改善待ちで、修正後に test: add assertType coverage for untested type inference cases #80 でピン留めした型テストの期待値更新と本注記の削除を行ってからクローズするため(手順は instanceof ベースの絞り込みで Result の型引数が失われる(PHPStan の制限) #83 のチェックリストに記載)Summary by CodeRabbit
リリースノート