Skip to content

Commit ae19ffd

Browse files
authored
fix: prevent TypeError in excerpt() when phrase is not found or radius is odd (#10756)
* fix(Helpers): prevent TypeError in excerpt() when phrase is not found or radius is odd * fix(Helpers): use intdiv() and document excerpt() fallback * style(Helpers): multiline excerpt tests and clarify fallback guide * test(Helpers): inline expected strings in excerpt assertions * docs: add changelog entry for excerpt() TypeError fix
1 parent 1fa7729 commit ae19ffd

4 files changed

Lines changed: 33 additions & 4 deletions

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Release Date: Unreleased
2626
- **Database:** Fixed a bug where `SQLSRV\Forge::_alterTable()` interpolated table and column names without escaping in default constraint lookups.
2727
- **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.
2828
- **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.
29+
- **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`.
2930
- **HTTP:** Fixed a bug where query-string based routing truncated additional query parameters in `SiteURIFactory::parseRequestURI()` due to incorrect array index access.
3031
- **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.
3132
- **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.

‎docs/helpers/text_helper.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ this_string_is_e…ak_my_design.jpg
489489

490490
This function will extract `$radius` number of characters before and after the central `$phrase` with an ellipsis before and after.
491491

492-
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.
492+
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.
493493

494494
Example:
495495

‎system/Helpers/text_helper.php‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -709,16 +709,20 @@ function alternator(...$args): string
709709
* @param int $radius The amount of characters returned around the phrase.
710710
* @param string $ellipsis Ending that will be appended
711711
*
712-
* If no $phrase is passed, will generate an excerpt of $radius characters
713-
* from the beginning of $text.
712+
* If no $phrase is passed or $phrase is not found, will generate an excerpt
713+
* of $radius characters from the beginning of $text.
714714
*/
715715
function excerpt(string $text, ?string $phrase = null, int $radius = 100, string $ellipsis = '...'): string
716716
{
717717
if (isset($phrase)) {
718718
$phrasePosition = mb_stripos($text, $phrase);
719719
$phraseLength = mb_strlen($phrase);
720+
721+
if ($phrasePosition === false) {
722+
return excerpt($text, null, $radius, $ellipsis);
723+
}
720724
} else {
721-
$phrasePosition = $radius / 2;
725+
$phrasePosition = intdiv($radius, 2);
722726
$phraseLength = 1;
723727
}
724728

‎tests/system/Helpers/TextHelperTest.php‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,30 @@ public function testExcerptRadius(): void
450450
$this->assertSame($multibyteResult, excerpt($this->mbLongString, 'печально', 10));
451451
}
452452

453+
public function testExcerptPhraseNotFound(): void
454+
{
455+
$this->assertSame(
456+
' 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. ...',
457+
excerpt($this->longString, 'nonexistent'),
458+
);
459+
$this->assertSame(
460+
' Давным-давно во фреймворке не было тестов. Это печ льно. И вот несколько хороших людей начали писать тесты. Чем больше времени проходило, тем ...',
461+
excerpt($this->mbLongString, 'несуществующее'),
462+
);
463+
}
464+
465+
public function testExcerptOddRadius(): void
466+
{
467+
$this->assertSame(
468+
' 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. ...',
469+
excerpt($this->longString, null, 101),
470+
);
471+
$this->assertSame(
472+
' Давным-давно во фреймворке не было тестов. Это печ льно. И вот несколько хороших людей начали писать тесты. Чем больше времени проходило, тем ...',
473+
excerpt($this->mbLongString, null, 101),
474+
);
475+
}
476+
453477
public function testAlternator(): void
454478
{
455479
$phrase = ' scream! ';

0 commit comments

Comments
 (0)