fix(twig): readtime() returns "0" for texts shorter than 200 words - #2473
Merged
Merged
Conversation
`floor()` returns a float, so the `$min === 0` guard meant to enforce a 1 minute minimum was never true: a text of less than 200 words returned the string "0" instead of "1". Cast the result to int and use `max()` so the minimum is enforced whatever the word count. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
readtime()is documented to return the estimated reading time in minutes, with a 1 minute minimum. For any text shorter than 200 words it returns"0"instead:{{ readtime(page.content) }} min {# → "0 min" on a short post #}Cause
floor()returns a float, so the strict comparison0.0 === 0is alwaysfalseand the minimum guard is dead code.(string) 0.0then yields"0".Fix
Cast to
intand enforce the minimum withmax(), so it holds whatever the word count:Tests
Adds
tests/Unit/Renderer/Extension/ContentTest.phpcovering the 1 minute minimum (null, empty string, short text), the 200 words/minute rate, and that markup is ignored.The full unit suite passes (243 tests),
phpstanis clean, andphp-cs-fixerreports no change on the touched files.Note (out of scope)
While writing the tests I noticed two unrelated quirks of the current implementation, left untouched here:
strip_tags()concatenates words across tag boundaries (<p>a</p><p>b</p>→ab), so the count is slightly low on markup without whitespace between blocks;str_word_count()is ASCII-oriented and undercounts accented words, so non-English content reads as shorter than it is.Happy to open a follow-up if you want either addressed.
🤖 Generated with Claude Code