Repository navigation
fix: prevent TypeError in excerpt() when phrase is not found or radius is odd - #10756
Conversation
neznaika0
left a comment
There was a problem hiding this comment.
If you are approved to change excerpt() by running the command again, this is considered a change. It should be written in docs.
|
Overall, this is a solid and much-needed fix. Resolving the Here is feedback addressing the review comments and the PR: 1. Fallback vs. Throwing an ExceptionFalling back to extracting from the beginning (
2. The Missing Character Bug (
|
|
Thanks for the review @neznaika0 and @gr8man! I pushed a small update:
Agreed on keeping the missing-character quirk ( |
|
Thanks for the review @paulbalandan! Pushed an update adjusting the user guide wording and formatting the added test assertions as multiline. |
TypeError in excerpt() when phrase is not found or radius is odd
michalsn
left a comment
There was a problem hiding this comment.
Sorry, we need a changelog entry for this.
… or radius is odd
0630af0 to
8da58f6
Compare
|
@michalsn Added the changelog entry in |
Description
Because
system/Helpers/text_helper.phpusesdeclare(strict_types=1),excerpt()previously threw an unhandledTypeErrorin two scenarios:$phrasewas passed but did not exist in$text,mb_stripos($text, $phrase)returnedfalse, causingmb_substr($text, 0, $phrasePosition)to fail withTypeError: mb_substr(): Argument #3 ($length) must be of type ?int, bool given.$phrasewasnulland$radiuswas an odd integer (e.g.,101),$radius / 2evaluated tofloat(50.5), causingmb_substr($text, 0, $phrasePosition)to fail withTypeError: mb_substr(): Argument #3 ($length) must be of type ?int, float given.This PR:
excerpt($text, null, $radius, $ellipsis)(extracting from the beginning of$text) when$phrasePosition === false.intdiv($radius, 2)so$phrasePositionis always an integer.docs/helpers/text_helper.mdto document the fallback behavior when$phraseis not found.TextHelperTest(testExcerptPhraseNotFound()andtestExcerptOddRadius()).Checklist: