Handle mb_strlen() failure in safe_strlen() - #203
Conversation
On PHP < 8, mb_strlen() returns false on failure. Fall back to strlen() in that case instead of returning false, and only subtract combining characters if preg_match_all() succeeds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HsH4o5pKmm1Qd5cqwAm7vB
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configuration
📒 Files selected for processing (1)
Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review. 📝 WalkthroughWalkthrough
ChangesSafe string length
Priority: ⬇️ Low Estimated code review effort: 2 (Simple) | ~8 minutes Change: Bug fix Suggested reviewers: Merge Risk: ⚪ Minimal · up to The length calculation retains its normal UTF-8 behavior and falls back when mbstring provides no usable length. No actionable merge-blocking risk is evident in this change. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ 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 |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Fixes the PHPStan job failing on
main(run):On PHP < 8 (the package supports
>= 7.2.24),mb_strlen()returnsfalseon failure, e.g. for an unsupported encoding, andsafe_strlen()would return it as-is.Changes:
mb_strlen()fails, fall through to the finalstrlen()fallback.preg_match_all()succeeds.The guard is a truthiness check (
if ( $length )) rather thanfalse !== $length/is_int(): withtreatPhpDocTypesAsCertain: false, PHPStan judges those against PHP 8's nativemb_strlen(): intsignature and flags them as always true.mb_strlen()only returns0for an empty string, wherestrlen()also returns0, so the result is identical.Verified locally with PHPStan 2.3.0:
composer phpstanreports no errors;composer phpunitpasses (99 tests).🤖 Generated with Claude Code
https://claude.ai/code/session_01HsH4o5pKmm1Qd5cqwAm7vB
Generated by Claude Code
Summary by CodeRabbit