Skip to content

Commit

Permalink
Merge branch 'flesch-reading-ease-score' of github.com:vormkracht10/l…
Browse files Browse the repository at this point in the history
…aravel-seo-scanner into flesch-reading-ease-score
  • Loading branch information
Baspa committed Aug 11, 2023
2 parents 18938c9 + b8d82b2 commit 70596c3
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/Checks/Content/FleschReadingEaseCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,33 +91,34 @@ private function getAverageSyllableCountPerWord(array $sentences): int
return round($totalSyllableCount / count($sentences), 0, PHP_ROUND_HALF_UP);

Check failure on line 91 in src/Checks/Content/FleschReadingEaseCheck.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Vormkracht10\Seo\Checks\Content\FleschReadingEaseCheck::getAverageSyllableCountPerWord() should return int but returns float.
}

private function countSyllables($word)
private function countSyllables($word)
{
$vowels = array('a', 'e', 'i', 'o', 'u', 'y', 'A', 'E', 'I', 'O', 'U', 'Y');
$vowels = ['a', 'e', 'i', 'o', 'u', 'y', 'A', 'E', 'I', 'O', 'U', 'Y'];
$syllables = 0;
$prevChar = null;

for ($i = 0; $i < strlen($word); $i++) {
$char = $word[$i];
if (in_array($char, $vowels) && ($prevChar === null || !in_array($prevChar, $vowels))) {
if (in_array($char, $vowels) && ($prevChar === null || ! in_array($prevChar, $vowels))) {
$syllables++;
}
$prevChar = $char;
}

return max(1, $syllables); // Ensure at least one syllable for non-empty word
}
private function averageSyllablesPerWord($text)

private function averageSyllablesPerWord($text)
{
$words = preg_split('/\s+/', $text);
$totalSyllables = array_reduce($words, function ($carry, $word) {
return $carry + $this->countSyllables($word);
}, 0);
$totalWords = count($words);

if ($totalWords > 0) {
$averageSyllables = $totalSyllables / $totalWords;

return $averageSyllables;
} else {
return 0; // Handle case of empty input
Expand Down

0 comments on commit 70596c3

Please sign in to comment.