From 34b9b3f089469363fab89f09e588b9f2f7bd1cee Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Tue, 10 Nov 2026 23:36:01 +0700 Subject: [PATCH 1/5] fix(Helpers): prevent TypeError in excerpt() when phrase is not found or radius is odd --- system/Helpers/text_helper.php | 6 +++++- tests/system/Helpers/TextHelperTest.php | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/system/Helpers/text_helper.php b/system/Helpers/text_helper.php index 09d14abbe2ee..c6076d354668 100644 --- a/system/Helpers/text_helper.php +++ b/system/Helpers/text_helper.php @@ -717,8 +717,12 @@ function excerpt(string $text, ?string $phrase = null, int $radius = 100, string if (isset($phrase)) { $phrasePosition = mb_stripos($text, $phrase); $phraseLength = mb_strlen($phrase); + + if ($phrasePosition === false) { + return excerpt($text, null, $radius, $ellipsis); + } } else { - $phrasePosition = $radius / 2; + $phrasePosition = (int) ($radius / 2); $phraseLength = 1; } diff --git a/tests/system/Helpers/TextHelperTest.php b/tests/system/Helpers/TextHelperTest.php index 88878bcf8294..e83b39a647b5 100644 --- a/tests/system/Helpers/TextHelperTest.php +++ b/tests/system/Helpers/TextHelperTest.php @@ -450,6 +450,24 @@ public function testExcerptRadius(): void $this->assertSame($multibyteResult, excerpt($this->mbLongString, 'печально', 10)); } + public function testExcerptPhraseNotFound(): void + { + $result = ' Once upon a time, a framework had no tests. It sad So some nice people began to write tests. The more time that went on, the happier it became. ...'; + $this->assertSame($result, excerpt($this->longString, 'nonexistent')); + + $multibyteResult = ' Давным-давно во фреймворке не было тестов. Это печ льно. И вот несколько хороших людей начали писать тесты. Чем больше времени проходило, тем ...'; + $this->assertSame($multibyteResult, excerpt($this->mbLongString, 'несуществующее')); + } + + public function testExcerptOddRadius(): void + { + $result = ' Once upon a time, a framework had no tests. It sad So some nice people began to write tests. The more time that went on, the happier it became. ...'; + $this->assertSame($result, excerpt($this->longString, null, 101)); + + $multibyteResult = ' Давным-давно во фреймворке не было тестов. Это печ льно. И вот несколько хороших людей начали писать тесты. Чем больше времени проходило, тем ...'; + $this->assertSame($multibyteResult, excerpt($this->mbLongString, null, 101)); + } + public function testAlternator(): void { $phrase = ' scream! '; From 9c72a0fc6d4b0e1c9c0b13ffced8c8c346dab6e5 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Wed, 11 Nov 2026 23:55:54 +0700 Subject: [PATCH 2/5] fix(Helpers): use intdiv() and document excerpt() fallback --- docs/helpers/text_helper.md | 2 +- system/Helpers/text_helper.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/helpers/text_helper.md b/docs/helpers/text_helper.md index 826b87188e8c..162ba2e83e1a 100644 --- a/docs/helpers/text_helper.md +++ b/docs/helpers/text_helper.md @@ -489,7 +489,7 @@ this_string_is_e…ak_my_design.jpg This function will extract `$radius` number of characters before and after the central `$phrase` with an ellipsis before and after. -The first parameter is the text to extract an excerpt from, and the second is the central word or phrase to count before and after. The third parameter is the number of characters to count before and after the central phrase. If no phrase is passed, the excerpt will include the first `$radius` characters with the ellipsis at the end. +The first parameter is the text to extract an excerpt from, and the second is the central word or phrase to count before and after. The third parameter is the number of characters to count before and after the central phrase. If no phrase is passed, or if the phrase is not found in the text, the excerpt will include the first `$radius` characters with the ellipsis at the end. Example: diff --git a/system/Helpers/text_helper.php b/system/Helpers/text_helper.php index c6076d354668..20abc3d5f7de 100644 --- a/system/Helpers/text_helper.php +++ b/system/Helpers/text_helper.php @@ -709,8 +709,8 @@ function alternator(...$args): string * @param int $radius The amount of characters returned around the phrase. * @param string $ellipsis Ending that will be appended * - * If no $phrase is passed, will generate an excerpt of $radius characters - * from the beginning of $text. + * If no $phrase is passed or $phrase is not found, will generate an excerpt + * of $radius characters from the beginning of $text. */ function excerpt(string $text, ?string $phrase = null, int $radius = 100, string $ellipsis = '...'): string { @@ -722,7 +722,7 @@ function excerpt(string $text, ?string $phrase = null, int $radius = 100, string return excerpt($text, null, $radius, $ellipsis); } } else { - $phrasePosition = (int) ($radius / 2); + $phrasePosition = intdiv($radius, 2); $phraseLength = 1; } From cc99d3f55aca113e3e928f717065e0c62dda7cca Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Sat, 14 Nov 2026 01:01:11 +0700 Subject: [PATCH 3/5] style(Helpers): multiline excerpt tests and clarify fallback guide --- docs/helpers/text_helper.md | 2 +- tests/system/Helpers/TextHelperTest.php | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/docs/helpers/text_helper.md b/docs/helpers/text_helper.md index 162ba2e83e1a..c17223f82de4 100644 --- a/docs/helpers/text_helper.md +++ b/docs/helpers/text_helper.md @@ -489,7 +489,7 @@ this_string_is_e…ak_my_design.jpg This function will extract `$radius` number of characters before and after the central `$phrase` with an ellipsis before and after. -The first parameter is the text to extract an excerpt from, and the second is the central word or phrase to count before and after. The third parameter is the number of characters to count before and after the central phrase. If no phrase is passed, or if the phrase is not found in the text, the excerpt will include the first `$radius` characters with the ellipsis at the end. +The first parameter is the text to extract an excerpt from, and the second is the central word or phrase to count before and after. The third parameter is the number of characters to count before and after the central phrase. If no phrase is passed, or if the phrase is not found in the text, it falls back to extracting an excerpt of whole words from the beginning of the text based on the radius with the ellipsis at the end. Example: diff --git a/tests/system/Helpers/TextHelperTest.php b/tests/system/Helpers/TextHelperTest.php index e83b39a647b5..8fe708dd6fbb 100644 --- a/tests/system/Helpers/TextHelperTest.php +++ b/tests/system/Helpers/TextHelperTest.php @@ -453,19 +453,31 @@ public function testExcerptRadius(): void public function testExcerptPhraseNotFound(): void { $result = ' Once upon a time, a framework had no tests. It sad So some nice people began to write tests. The more time that went on, the happier it became. ...'; - $this->assertSame($result, excerpt($this->longString, 'nonexistent')); + $this->assertSame( + $result, + excerpt($this->longString, 'nonexistent'), + ); $multibyteResult = ' Давным-давно во фреймворке не было тестов. Это печ льно. И вот несколько хороших людей начали писать тесты. Чем больше времени проходило, тем ...'; - $this->assertSame($multibyteResult, excerpt($this->mbLongString, 'несуществующее')); + $this->assertSame( + $multibyteResult, + excerpt($this->mbLongString, 'несуществующее'), + ); } public function testExcerptOddRadius(): void { $result = ' Once upon a time, a framework had no tests. It sad So some nice people began to write tests. The more time that went on, the happier it became. ...'; - $this->assertSame($result, excerpt($this->longString, null, 101)); + $this->assertSame( + $result, + excerpt($this->longString, null, 101), + ); $multibyteResult = ' Давным-давно во фреймворке не было тестов. Это печ льно. И вот несколько хороших людей начали писать тесты. Чем больше времени проходило, тем ...'; - $this->assertSame($multibyteResult, excerpt($this->mbLongString, null, 101)); + $this->assertSame( + $multibyteResult, + excerpt($this->mbLongString, null, 101), + ); } public function testAlternator(): void From 246ae42fcded0947d58207d2ec8965057b2dd5a9 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Sat, 14 Nov 2026 20:31:27 +0700 Subject: [PATCH 4/5] test(Helpers): inline expected strings in excerpt assertions --- tests/system/Helpers/TextHelperTest.php | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/tests/system/Helpers/TextHelperTest.php b/tests/system/Helpers/TextHelperTest.php index 8fe708dd6fbb..ec81c921c097 100644 --- a/tests/system/Helpers/TextHelperTest.php +++ b/tests/system/Helpers/TextHelperTest.php @@ -452,30 +452,24 @@ public function testExcerptRadius(): void public function testExcerptPhraseNotFound(): void { - $result = ' Once upon a time, a framework had no tests. It sad So some nice people began to write tests. The more time that went on, the happier it became. ...'; $this->assertSame( - $result, + ' Once upon a time, a framework had no tests. It sad So some nice people began to write tests. The more time that went on, the happier it became. ...', excerpt($this->longString, 'nonexistent'), ); - - $multibyteResult = ' Давным-давно во фреймворке не было тестов. Это печ льно. И вот несколько хороших людей начали писать тесты. Чем больше времени проходило, тем ...'; $this->assertSame( - $multibyteResult, + ' Давным-давно во фреймворке не было тестов. Это печ льно. И вот несколько хороших людей начали писать тесты. Чем больше времени проходило, тем ...', excerpt($this->mbLongString, 'несуществующее'), ); } public function testExcerptOddRadius(): void { - $result = ' Once upon a time, a framework had no tests. It sad So some nice people began to write tests. The more time that went on, the happier it became. ...'; $this->assertSame( - $result, + ' Once upon a time, a framework had no tests. It sad So some nice people began to write tests. The more time that went on, the happier it became. ...', excerpt($this->longString, null, 101), ); - - $multibyteResult = ' Давным-давно во фреймворке не было тестов. Это печ льно. И вот несколько хороших людей начали писать тесты. Чем больше времени проходило, тем ...'; $this->assertSame( - $multibyteResult, + ' Давным-давно во фреймворке не было тестов. Это печ льно. И вот несколько хороших людей начали писать тесты. Чем больше времени проходило, тем ...', excerpt($this->mbLongString, null, 101), ); } From 8da58f6d54176e94e75478ed4c2dc6ca23475d10 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Sun, 15 Nov 2026 14:15:32 +0700 Subject: [PATCH 5/5] docs: add changelog entry for excerpt() TypeError fix --- docs/changelogs/v4.7.7.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/changelogs/v4.7.7.md b/docs/changelogs/v4.7.7.md index 367c69f9cb37..d393b5a91982 100644 --- a/docs/changelogs/v4.7.7.md +++ b/docs/changelogs/v4.7.7.md @@ -23,6 +23,7 @@ Release Date: Unreleased - **Database:** Fixed a bug where `SQLSRV\Forge::_alterTable()` interpolated table and column names without escaping in default constraint lookups. - **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. - **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. +- **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`. - **HTTP:** Fixed a bug where query-string based routing truncated additional query parameters in `SiteURIFactory::parseRequestURI()` due to incorrect array index access. - **HTTP:** Fixed a bug where `UploadedFile::move()` and `UploadedFile::store()` on Windows could store a file under a different name than `getName()` reported, or write its contents to an NTFS alternate data stream. Filenames ending in a period or space or containing a colon are now rejected, including explicitly supplied names. - **Images:** Fixed a bug in `ImageMagickHandler::save()` where saving without an extension or with an unsupported extension threw an `ImagickException` instead of falling back to the image's format.