Skip to content

fix: count whitespace runs as one word separator in chopString - #4943

Open
arpitjain099 wants to merge 1 commit into
thorsten:mainfrom
arpitjain099:fix/chopstring-whitespace-word-count
Open

arpitjain099 wants to merge 1 commit into
thorsten:mainfrom
arpitjain099:fix/chopstring-whitespace-word-count

Conversation

@arpitjain099

@arpitjain099 arpitjain099 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Utils::chopString() splits on a single space, so a run of whitespace yields empty pieces that still count as words, and a line break is not a separator at all. An answer written as a bullet list arrives as one space-separated piece, so FaqHelper::renderAnswerPreview() hands back the whole answer instead of the 20 words the search results page asks for.

Its neighbour Utils::makeShorterText() already collapses \s+ first, and testMakeShorterTextWithMultipleSpaces() asserts that. This gives chopString() the same normalisation plus a trim(), so a leading space no longer eats a word. Single-spaced, empty and one-word input come back byte identical.

The 4.1 branch carries the same line, so please say if a backport would help.

Checklist

  • composer check passes (format, lint, static analysis)
  • composer test passes (10568 tests, 3 redis tests skipped, no redis extension here). pnpm test not run, no JS/TS changes
  • pnpm tsc and pnpm oxlint pass for TypeScript changes - n/a
  • New or changed behaviour is covered by tests
  • Database schema changes ship with an upgrade step in phpMyFAQ\Setup\Update - n/a
  • CHANGELOG.md is updated for user-facing changes - n/a, a bug fix rather than a user-visible feature
  • Dependency major version bumps: upstream changelog read, old APIs grepped for, build green - n/a

Summary by CodeRabbit

  • Bug Fixes
    • Text previews now handle repeated spaces and line breaks consistently, producing clean word-limited excerpts.
  • Tests
    • Added coverage for whitespace normalization in text previews and multiline answers.

@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Utils::chopString now collapses consecutive whitespace and trims input before splitting. Tests cover repeated spaces, line breaks, and a multiline answer preview.

Changes: Preview text normalization

Layer / File(s) Summary
Normalize input and verify previews
phpmyfaq/src/phpMyFAQ/Utils.php, tests/phpMyFAQ/UtilsTest.php, tests/phpMyFAQ/Helper/FaqHelperTest.php
chopString collapses consecutive whitespace and trims the input before splitting. If normalization does not return a string, it trims the original input. Tests check repeated spaces, line breaks, and a three-word preview from a multiline HTML list.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Suggested reviewers: thorsten

Merge Risk: Minimal · up to 564bc

This change makes answer previews count words correctly when the text has repeated spaces or line breaks. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: chopString now treats whitespace runs as one word separator. The raw summary confirms whitespace collapsing and trimming in Utils::chopString, and the objectives descr…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: chopString now treats whitespace runs as one word separator. The raw summary confirms whitespace collapsing and trimming in Utils::chopString, and the objectives describe the same fix for runs and line breaks. The title is specific and accurate, and it has a conventional fix: prefix.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 415e343f-c3ef-45ca-aeb3-01ccea451b15
Commits

Reviewing files that changed from the base of the PR and between ccdaa88 and 564bc99.

Files selected for processing: 3

  • phpmyfaq/src/phpMyFAQ/Utils.php
  • tests/phpMyFAQ/Helper/FaqHelperTest.php
  • tests/phpMyFAQ/UtilsTest.php

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant