Skip to content

Commit 75e8881

Browse files
committed
fix(Helpers): preserve TypeError for non-numeric position and add changelog
1 parent 14e5472 commit 75e8881

3 files changed

Lines changed: 10 additions & 1 deletion

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Release Date: Unreleased
3434
- **Database:** Fixed a bug where `SQLite3\Connection::_foreignKeyData()` did not escape table identifiers in `PRAGMA foreign_key_list`, causing syntax errors on table names containing hyphens or schemas.
3535
- **Database:** Fixed a bug where `SQLite3\Table::dropIndexes()` used single-quoted string literals instead of escaped identifiers in `DROP INDEX IF EXISTS`, causing syntax errors on index names containing quotes or special characters.
3636
- **Debug Toolbar:** Fixed a bug where `History::setFiles()` emitted a PHP warning when attempting to delete a debugbar history file that had already been removed.
37+
- **Helpers:** Fixed a bug in `ellipsize()` where `$position` was clamped after slicing the beginning of the string and where float `1.0` was not treated identically to integer `1`.
3738
- **Helpers:** Fixed a bug where `excerpt()` threw a `TypeError` when `$phrase` was not found in `$text` or when `$phrase` was `null` and `$radius` was an odd integer. When `$phrase` is not found, `excerpt()` now falls back to extracting an excerpt from the beginning of `$text`.
3839
- **Helpers:** Fixed a bug where `format_number()` with `$precision = 0` appended a trailing comma in locales using a comma as the decimal separator.
3940
- **Helpers:** Fixed a bug where `parse_subdomain()` incorrectly treated hostnames with ports (such as IP addresses and two-part TLD domains) as having unexpected subdomains.

‎system/Helpers/text_helper.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ function ellipsize(string $str, int $maxLength, $position = 1, string $ellipsis
437437
return $str;
438438
}
439439

440-
$position = max(0.0, min(1.0, (float) $position));
440+
$position = max(0.0, min(1.0, $position * 1.0));
441441
$beg = mb_substr($str, 0, (int) floor($maxLength * $position));
442442
$remaining = $maxLength - mb_strlen($beg);
443443
$end = $remaining > 0 ? mb_substr($str, -$remaining) : '';

‎tests/system/Helpers/TextHelperTest.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\Test\CIUnitTestCase;
1818
use PHPUnit\Framework\Attributes\DataProvider;
1919
use PHPUnit\Framework\Attributes\Group;
20+
use TypeError;
2021

2122
/**
2223
* @internal
@@ -378,6 +379,13 @@ public static function provideEllipsizePositionEdgeCases(): iterable
378379
yield 'negative float' => [-0.5, '… my string'];
379380
}
380381

382+
public function testEllipsizeInvalidPositionThrowsTypeError(): void
383+
{
384+
$this->expectException(TypeError::class);
385+
386+
$this->assertSame('', ellipsize('this is my string', 10, 'invalid')); // @phpstan-ignore argument.type
387+
}
388+
381389
/**
382390
* A character limit below 2 used to make word_wrap() loop forever.
383391
*/

0 commit comments

Comments
 (0)