Skip to content

fix(Helpers): clamp position and handle float 1.0 in ellipsize() - #10809

Open
banghamid wants to merge 2 commits into
codeigniter4:developfrom
banghamid:fix/text-helper-ellipsize
Open

banghamid wants to merge 2 commits into
codeigniter4:developfrom
banghamid:fix/text-helper-ellipsize

Conversation

@banghamid

@banghamid banghamid commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Description
In system/Helpers/text_helper.php, ellipsize() had two bugs in how it handled $position:

  1. $position was clamped to 1 after $beg was already sliced using $maxLength * $position:
    $beg      = mb_substr($str, 0, (int) floor($maxLength * $position));
    $position = ($position > 1) ? 1 : $position;
    When $position > 1 (e.g. 2 or 1.5), $beg took more than $maxLength characters and $end received a positive length, duplicating the beginning of the string:
    • ellipsize('this is my string', 10, 2) returned 'this is my string…this is' instead of 'this is my…'.
  2. $position === 1 used strict integer comparison even though ellipsize() documents @param float|int $position. Passing 1.0 (float) failed the check and fell into the else branch, computing mb_substr($str, -0) (equivalent to mb_substr($str, 0)) and appending the entire original string after the ellipsis:
    • ellipsize('this is my string', 10, 1.0) returned 'this is my…this is my string' instead of 'this is my…'.

This PR:

  • Clamps $position to [0.0, 1.0] via max(0.0, min(1.0, $position * 1.0)) before slicing $beg, handling float 1.0 identically to integer 1 while preserving native TypeError for non-numeric types.
  • Only slices $end when $remaining > 0 ($maxLength - mb_strlen($beg)), preventing duplicated string content when $position >= 1.0.
  • Adds regression test cases in tests/system/Helpers/TextHelperTest.php:
    • testEllipsizePositionEdgeCases() covering edge cases 1.0, 2, 1.5, and -0.5.
    • testEllipsizeInvalidPositionThrowsTypeError() verifying TypeError for non-numeric position arguments.
  • Adds a changelog entry in docs/changelogs/v4.7.7.md.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@carson-codeigniter4 carson-codeigniter4 Bot added the bug Verified issues on the current code behavior or pull requests that will fix them label Nov 21, 2026
@banghamid
banghamid force-pushed the fix/text-helper-ellipsize branch from f174d04 to 14e5472 Compare November 21, 2026 13:35
@banghamid
banghamid marked this pull request as ready for review November 21, 2026 13:51

@michalsn michalsn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We also need a changelog entry.

Comment thread system/Helpers/text_helper.php Outdated
} else {
$end = mb_substr($str, -($maxLength - mb_strlen($beg)));
}
$position = max(0.0, min(1.0, (float) $position));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
$position = max(0.0, min(1.0, (float) $position));
$position = max(0.0, min(1.0, $position * 1.0));

Could we preserve the TypeError for non-numeric strings? It would be nice to have a test for this too.

@banghamid banghamid Nov 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 75e8881 — switched to $position * 1.0 to preserve the TypeError for non-numeric strings and added testEllipsizeInvalidPositionThrowsTypeError().

@banghamid

banghamid commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @michalsn! Updated in 75e8881:

  • Preserved TypeError for non-numeric $position strings using $position * 1.0 and added a regression test (testEllipsizeInvalidPositionThrowsTypeError()).
  • Added a changelog entry in docs/changelogs/v4.7.7.md.

@banghamid
banghamid requested a review from michalsn November 22, 2026 02:29

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Verified issues on the current code behavior or pull requests that will fix them

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants