Skip to content

Handle mb_strlen() failure in safe_strlen() - #203

Merged
swissspidy merged 1 commit into
mainfrom
fix-phpstan-2.3-errors
Oct 6, 2026
Merged

swissspidy merged 1 commit into
mainfrom
fix-phpstan-2.3-errors

Conversation

@swissspidy

@swissspidy swissspidy commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

Fixes the PHPStan job failing on main (run):

cli/cli.php
  195  Function cli\safe_strlen() should return int but returns int|false.

On PHP < 8 (the package supports >= 7.2.24), mb_strlen() returns false on failure, e.g. for an unsupported encoding, and safe_strlen() would return it as-is.

Changes:

  • If mb_strlen() fails, fall through to the final strlen() fallback.
  • Only subtract the combining-character count when preg_match_all() succeeds.

The guard is a truthiness check (if ( $length )) rather than false !== $length / is_int(): with treatPhpDocTypesAsCertain: false, PHPStan judges those against PHP 8's native mb_strlen(): int signature and flags them as always true. mb_strlen() only returns 0 for an empty string, where strlen() also returns 0, so the result is identical.

Verified locally with PHPStan 2.3.0: composer phpstan reports no errors; composer phpunit passes (99 tests).

🤖 Generated with Claude Code

https://claude.ai/code/session_01HsH4o5pKmm1Qd5cqwAm7vB


Generated by Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved string length handling for empty input and UTF-8 text containing combining characters. Results now remain accurate when character counting encounters these cases or cannot reliably account for combining characters. This helps keep command-line output and related operations consistent across different text inputs.

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
Copilot AI balanced review requested due to automatic review settings October 6, 2026 07:54
@swissspidy
swissspidy requested a review from a team as a code owner October 6, 2026 07:54

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d0c5ee21-ad92-4a41-8c79-2bcdf5d5032b
📥 Commits

Reviewing files that changed from the base of the PR and between 69774b1 and d481d1d.

📒 Files selected for processing (1)
  • lib/cli/cli.php

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.


📝 Walkthrough

Walkthrough

safe_strlen() now falls back to strlen() when mb_strlen() returns a falsey result. For UTF-8 input, it subtracts combining characters only when the regex match succeeds. The existing selective-test bitmask still controls whether the adjusted mbstring result is returned.

Changes

Safe string length

Layer / File(s) Summary
Length calculation and fallback
lib/cli/cli.php
safe_strlen() uses strlen() when the mb_strlen() result is falsey. For UTF-8 input, it subtracts combining characters only after a successful regex match. The selective-test bitmask still controls whether the adjusted result is returned.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Suggested reviewers: brianhenryie

Merge Risk: ⚪ Minimal · up to d481d

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)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: handling mb_strlen() failure in safe_strlen().
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files.
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.
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@codecov

codecov Bot commented Oct 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@swissspidy swissspidy added this to the 0.13.1 milestone Oct 6, 2026
@swissspidy
swissspidy merged commit 33ba0dd into main Oct 6, 2026
25 checks passed
@swissspidy
swissspidy deleted the fix-phpstan-2.3-errors branch October 6, 2026 08:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants