Fix: FK reading grade of 0 < x < 1 collapses to 0 (normalize to 1)#1725
Conversation
Raw floor() on a Flesch-Kincaid grade between 0.01 and 0.99 collapsed to 0, causing valid simple-grade content to be treated as "not enough content" and flagged as failing. Introduces edac_normalize_fk_grade() which returns 0 only for a true zero grade and otherwise returns max(1, floor($fk_grade)). Applied to all four call sites across class-ajax.php (×2), class-summary-generator.php, and class-rest-api.php. Adds unit tests covering the boundary cases. Fixes #1497 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughA new helper normalizes Flesch-Kincaid grades so values between 0 and 1 become 1 instead of 0. That normalization is then used in AJAX, REST API, and summary grade calculations, with tests covering boundary cases. ChangesFlesch-Kincaid Grade Normalization
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related issues
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Code Review
This pull request introduces a new helper function edac_normalize_fk_grade to safely normalize Flesch-Kincaid grade levels, preventing fractional grades between 0 and 1 from being incorrectly collapsed to 0. It also integrates this helper across various summary and readability calculation points and adds corresponding unit tests. The review feedback highlights a potential TypeError in PHP 8.0+ because the underlying library can return false (or null) on empty content, which would violate the strict float type hint on the helper function. It is recommended to remove the strict type hints from both the helper function and the test cases, cast the input to a float internally, and add test cases for false and null inputs.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@includes/classes/class-rest-api.php`:
- Around line 1080-1083: The REST path incorrectly marks exactly 9th grade as
failing by setting $simplified_summary_grade_failed with the condition
"$simplified_summary_grade >= 9"; change the threshold to strictly greater-than
so it matches the “above 9th grade” semantics used elsewhere (i.e. use
"$simplified_summary_grade > 9"). Update the assignment that computes
$simplified_summary_grade_failed (after edac_normalize_fk_grade(
$text_statistics->fleschKincaidGradeLevel( $simplified_summary ) )) to use the >
operator so REST/sidebar agrees with the AJAX path.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 85e27011-3414-4f9a-be15-a271efea6a2b
📒 Files selected for processing (5)
admin/class-ajax.phpincludes/classes/class-rest-api.phpincludes/classes/class-summary-generator.phpincludes/helper-functions.phptests/phpunit/helper-functions/NormalizeFkGradeTest.php
| $simplified_summary_grade = edac_normalize_fk_grade( $text_statistics->fleschKincaidGradeLevel( $simplified_summary ) ); | ||
| } | ||
|
|
||
| $simplified_summary_grade_failed = $simplified_summary_grade >= 9; |
There was a problem hiding this comment.
Align REST failure threshold with “above 9th grade” semantics.
Line 1083 uses >= 9, which treats exactly 9th grade as failed. That conflicts with the “above 9th grade” behavior used in other updated paths, so REST/sidebar can disagree with AJAX for grade 9.
Proposed fix
- $simplified_summary_grade_failed = $simplified_summary_grade >= 9;
+ $simplified_summary_grade_failed = $simplified_summary_grade > 9;📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| $simplified_summary_grade = edac_normalize_fk_grade( $text_statistics->fleschKincaidGradeLevel( $simplified_summary ) ); | |
| } | |
| $simplified_summary_grade_failed = $simplified_summary_grade >= 9; | |
| $simplified_summary_grade = edac_normalize_fk_grade( $text_statistics->fleschKincaidGradeLevel( $simplified_summary ) ); | |
| } | |
| $simplified_summary_grade_failed = $simplified_summary_grade > 9; |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@includes/classes/class-rest-api.php` around lines 1080 - 1083, The REST path
incorrectly marks exactly 9th grade as failing by setting
$simplified_summary_grade_failed with the condition "$simplified_summary_grade
>= 9"; change the threshold to strictly greater-than so it matches the “above
9th grade” semantics used elsewhere (i.e. use "$simplified_summary_grade > 9").
Update the assignment that computes $simplified_summary_grade_failed (after
edac_normalize_fk_grade( $text_statistics->fleschKincaidGradeLevel(
$simplified_summary ) )) to use the > operator so REST/sidebar agrees with the
AJAX path.
There was a problem hiding this comment.
@pattonwebz Understood — thanks for clarifying. Deferring this from #1725 and handling the grade-9 threshold consistency work in #1797 makes sense, especially if you want to align all affected paths together with the WCAG 3.1.5 rationale. I’m good to leave this out of the current PR.
The fleschKincaidGradeLevel() library method returns false when content has no words or sentences. Remove the strict float type hint and cast to float internally so empty-content posts don't throw a TypeError. Add false and null test cases to cover this path. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Release tooling will replace x.x.x with the actual version on release. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
pattonwebz
left a comment
There was a problem hiding this comment.
I tested this and it is working for the most part. I did run into the issue that gemini called out about possible false value instead of a float. I tackled the AI review comments about it and added the test cases to cover false and null scenarios.
I also changed the since tags to be x.x.x and am opening a PR that will add a tool to fill their version numbers in as part of the release prep phase.
Fixes #1497
Description
floor()applied directly to a Flesch-Kincaid grade in the range (0, 1) silently collapses it to0. That0is then interpreted as "not enough content to determine a reading level," so content with a legitimately simple FK grade (e.g. 0.5 or 0.8) is misrepresented as uncalculable and incorrectly flagged as failing.What changed
Introduces
edac_normalize_fk_grade( float $fk_grade ): intinincludes/helper-functions.php:0when the library returns ≤ 0 (truly not enough content)max(1, floor($fk_grade))otherwise — so any positive sub-1 value becomes grade 1Replaces the raw
floor()pattern at all four affected call sites:admin/class-ajax.phpadmin/class-ajax.phpincludes/classes/class-summary-generator.phpcalculate_content_grade()includes/classes/class-rest-api.phpget_readability_data()Tests
Adds
tests/phpunit/helper-functions/NormalizeFkGradeTest.phpwith 11 data-driven cases covering: exact zero, negatives, the 0.01–0.99 boundary, 1.0, mid-grade values, and grades above 9.Checklist
Summary by CodeRabbit