generated from vormkracht10/laravel-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'too-long-sentences-check' of github.com:vormkracht10/la…
…ravel-seo-scanner into flesch-reading-ease-score
- Loading branch information
Showing
5 changed files
with
188 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace Vormkracht10\Seo\Checks\Content; | ||
|
||
use Illuminate\Http\Client\Response; | ||
use Symfony\Component\DomCrawler\Crawler; | ||
use Vormkracht10\Seo\Interfaces\Check; | ||
use Vormkracht10\Seo\Traits\PerformCheck; | ||
|
||
class TooLongSentenceCheck implements Check | ||
{ | ||
use PerformCheck; | ||
|
||
public string $title = 'Too long sentence check'; | ||
|
||
public string $priority = 'medium'; | ||
|
||
public int $timeToFix = 45; | ||
|
||
public int $scoreWeight = 5; | ||
|
||
public bool $continueAfterFailure = true; | ||
|
||
public ?string $failureReason; | ||
|
||
public mixed $actualValue = null; | ||
|
||
public mixed $expectedValue = null; | ||
|
||
public function check(Response $response, Crawler $crawler): bool | ||
{ | ||
if (! $this->validateContent($crawler)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function validateContent(Crawler $crawler): bool | ||
{ | ||
$realSentences = []; | ||
$sentences = $this->getSentencesFromCrawler($crawler); | ||
|
||
$sentences = $this->separateSentencesByDot($sentences); | ||
|
||
$sentencesWithTooManyWords = $this->calculateSentencesWithTooManyWords($sentences); | ||
|
||
$this->actualValue = $this->calculateSentencesWithTooManyWords($sentences); | ||
|
||
if (count($sentencesWithTooManyWords) === 0) { | ||
return true; | ||
} | ||
|
||
// If more than 20% of the total sentences are too long, fail | ||
if (count($sentencesWithTooManyWords) / count($sentences) > 0.2) { | ||
$this->failureReason = __('failed.content.too_long_sentence', [ | ||
'actualValue' => count($this->actualValue), | ||
]); | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private function separateSentencesByDot(array $sentences): array | ||
{ | ||
$newSentences = []; | ||
|
||
foreach ($sentences as $sentence) { | ||
$sentence = explode('.', $sentence); | ||
$newSentences = array_merge($newSentences, $sentence); | ||
} | ||
|
||
// Remove all sentences that are empty | ||
$sentences = array_filter($newSentences, function ($sentence) { | ||
return ! empty($sentence); | ||
}); | ||
|
||
return $sentences; | ||
} | ||
|
||
private function getSentencesFromCrawler(Crawler $crawler): array | ||
{ | ||
$content = $crawler->filterXPath('//body')->children(); | ||
|
||
// Get all elements that contain text | ||
$content = $content->filterXPath('//*/text()[normalize-space()]'); | ||
|
||
$content = $content->each(function (Crawler $node, $i) { | ||
return $node->text(); | ||
}); | ||
|
||
return $content; | ||
} | ||
|
||
private function calculateSentencesWithTooManyWords(array $sentences): array | ||
{ | ||
$tooLongSentences = []; | ||
|
||
foreach ($sentences as $sentence) { | ||
if (str_word_count($sentence) > 20) { | ||
$tooLongSentences[] = $sentence; | ||
} | ||
} | ||
|
||
return $tooLongSentences; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Http; | ||
use Symfony\Component\DomCrawler\Crawler; | ||
use Vormkracht10\Seo\Checks\Content\TooLongSentenceCheck; | ||
|
||
it('can perform the too long sentence check on page with too long sentence', function () { | ||
$check = new TooLongSentenceCheck(); | ||
$crawler = new Crawler(); | ||
|
||
$body = 'One two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty twenty-one.'; | ||
|
||
Http::fake([ | ||
'vormkracht10.nl' => Http::response( | ||
'<html> | ||
<head> | ||
<title>Test</title> | ||
</head> | ||
<body> | ||
<p>'.$body.'</p> | ||
</body>', | ||
200), | ||
]); | ||
|
||
$crawler->addHtmlContent(Http::get('vormkracht10.nl')->body()); | ||
|
||
$this->assertFalse($check->check(Http::get('vormkracht10.nl'), $crawler)); | ||
}); | ||
|
||
it('can perform the too long sentence check on page with no too long sentence', function () { | ||
$check = new TooLongSentenceCheck(); | ||
$crawler = new Crawler(); | ||
|
||
$body = 'One two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen'; | ||
|
||
Http::fake([ | ||
'vormkracht10.nl' => Http::response( | ||
'<html> | ||
<head> | ||
<title>Test</title> | ||
</head> | ||
<body> | ||
<p>'.$body.'</p> | ||
</body>', | ||
200), | ||
]); | ||
|
||
$crawler->addHtmlContent(Http::get('vormkracht10.nl')->body()); | ||
|
||
$check->check(Http::get('vormkracht10.nl'), $crawler); | ||
|
||
$this->assertTrue($check->check(Http::get('vormkracht10.nl'), $crawler)); | ||
}); | ||
|
||
it('can perform the too long sentence check on page with no body', function () { | ||
$check = new TooLongSentenceCheck(); | ||
$crawler = new Crawler(); | ||
|
||
Http::fake([ | ||
'vormkracht10.nl' => Http::response( | ||
'<html> | ||
<head> | ||
<title>Test</title> | ||
</head> | ||
<body></body>', | ||
200), | ||
]); | ||
|
||
$crawler->addHtmlContent(Http::get('vormkracht10.nl')->body()); | ||
|
||
$check->check(Http::get('vormkracht10.nl'), $crawler); | ||
|
||
$this->assertTrue($check->check(Http::get('vormkracht10.nl'), $crawler)); | ||
}); |