Skip to content

Commit 1502f1b

Browse files
fix: Implemented whole word matching in ProfanityAnalyser class (#81)
Example in portuguese: 'Colar de pérola' would get incorrectly flagged as profanity because it contains the last 4 chars that is a [bad word in pt_BR config](https://github.com/pestphp/pest-plugin-profanity/blob/06c77bb058ef577ff1e744ba853c08c015b31792/src/Config/profanities/pt_BR.php#L114). After this fix: PASS No profanity found in your application! ``` \p{L} matches any Unicode letter. (?<!\p{L}) ensures the character before isn’t a letter. (?!\p{L}) ensures the character after isn’t a letter. u flag = Unicode mode i flag = case-insensitive ```
1 parent 06c77bb commit 1502f1b

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/ProfanityAnalyser.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,13 @@ public static function analyse(string $file, array $excludingWords = [], array $
5656
$foundProfanity = [];
5757

5858
foreach ($words as $word) {
59-
if (preg_match('/\b'.preg_quote($word, '/').'\b/i', $fileContents) === 1) {
60-
foreach ($lines as $lineNumber => $line) {
61-
$key = $lineNumber.'-'.$word;
62-
if (preg_match('/\b'.preg_quote($word, '/').'\b/i', $line) === 1 && ! isset($foundProfanity[$key])) {
63-
// Skip reporting profanity if the line contains the ignore annotation
64-
if (! str_contains($line, '@pest-ignore-profanity')) {
65-
$errors[] = new Error($file, $lineNumber + 1, $word);
66-
$foundProfanity[$key] = true;
67-
}
59+
foreach ($lines as $lineNumber => $line) {
60+
$key = $lineNumber.'-'.$word;
61+
if (preg_match('/(?<!\p{L})'.preg_quote($word, '/').'(?!\p{L})/iu', $line) === 1 && ! isset($foundProfanity[$key])) {
62+
// Skip reporting profanity if the line contains the ignore annotation
63+
if (! str_contains($line, '@pest-ignore-profanity')) {
64+
$errors[] = new Error($file, $lineNumber + 1, $word);
65+
$foundProfanity[$key] = true;
6866
}
6967
}
7068
}

0 commit comments

Comments
 (0)