} */ public function analyze( string $html_or_text ): array { $plain = $this->strip_html( $html_or_text ); $sentence_lengths = array_map( array( $this, 'word_count' ), $this->split_sentences( $plain ) ); $paragraph_lengths = array_map( array( $this, 'word_count' ), $this->split_paragraphs( $html_or_text ) ); $long_sentences = count( array_filter( $sentence_lengths, fn( $len ) => $len > self::LONG_SENTENCE_WORD_THRESHOLD ) ); $long_paragraphs = count( array_filter( $paragraph_lengths, fn( $len ) => $len > self::LONG_PARAGRAPH_WORD_THRESHOLD ) ); $avg_sentence_length = $sentence_lengths ? array_sum( $sentence_lengths ) / count( $sentence_lengths ) : 0.0; $issues = array(); if ( $long_sentences > 0 ) { $issues[] = 'long_sentences'; } if ( $long_paragraphs > 0 ) { $issues[] = 'long_paragraphs'; } $score = 100; $score -= min( 40, $long_sentences * 5 ); $score -= min( 30, $long_paragraphs * 10 ); $score = max( 0, $score ); return array( 'score' => $score, 'avg_sentence_length' => round( $avg_sentence_length, 1 ), 'long_sentences' => $long_sentences, 'long_paragraphs' => $long_paragraphs, 'issues' => $issues, ); } /** * Splits plain text into sentences on sentence-ending punctuation. * * @param string $text Plain text (no HTML). * @return string[] Sentences. */ protected function split_sentences( string $text ): array { if ( '' === $text ) { return array(); } $parts = preg_split( '/(?<=[.!?…])\s+/u', $text ); return array_values( array_filter( array_map( 'trim', $parts ) ) ); } /** * Splits HTML into paragraphs, using

tags when present and falling * back to blank-line splitting otherwise. * * @param string $html Post content, HTML or plain text. * @return string[] Paragraph contents. */ protected function split_paragraphs( string $html ): array { if ( preg_match_all( '/]*>(.*?)<\/p>/is', $html, $matches ) && ! empty( $matches[1] ) ) { return $matches[1]; } return array_values( array_filter( array_map( 'trim', preg_split( '/\n\s*\n/', $html ) ) ) ); } /** * Unicode-safe word count for a fragment of text. * * @param string $text Text (may contain HTML) to count words in. * @return int Number of whitespace-separated tokens. */ protected function word_count( string $text ): int { $text = $this->strip_html( $text ); if ( '' === $text ) { return 0; } return count( preg_split( '/\s+/u', $text ) ); } /** * Strips HTML down to plain, whitespace-normalized text. * * Tags are replaced with a single space, not removed outright — removing * them outright (as bare strip_tags() does) glues adjacent block-level * text together with no separator, e.g. "...word.

Next..." becomes * "...word.Next..." with no space after the period. That silently merges * what should be separate sentences/paragraphs into one run-on sentence * for split_sentences() (which splits on whitespace *following* * sentence-ending punctuation), undercounting long sentences whenever * paragraphs are back-to-back with no whitespace between them. *