Skip to content

Commit d6f7f4d

Browse files
committed
implement russian detection
1 parent 0306654 commit d6f7f4d

File tree

3 files changed

+12
-46
lines changed

3 files changed

+12
-46
lines changed

src/Expectations/Profanity.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use JonPurvis\Profanify\Expectations\TargetedProfanity;
6+
use JonPurvis\Profanify\Support\RussianNormalizer;
67
use Pest\Arch\Contracts\ArchExpectation;
78
use Pest\Arch\Support\FileLineFinder;
89
use PHPUnit\Architecture\Elements\ObjectDescription;
@@ -51,6 +52,13 @@ function (ObjectDescription $object) use (&$foundWords, $excluding, $including,
5152
return true;
5253
}
5354

55+
// Check if russian
56+
if (preg_match('/[А-Яа-яЁё]/u', $word)) {
57+
$normalized = RussianNormalizer::normalizeRussianText($fileContents);
58+
59+
return str_contains($normalized, $word);
60+
}
61+
5462
preg_match_all('/[a-zA-Z]\w*/', $fileContents, $matches);
5563

5664
foreach ($matches[0] as $token) {

src/Support/RussianNormalizer.php

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,50 +19,4 @@ public static function normalizeRussianText(string $text): string
1919

2020
return preg_replace('/[^а-я]+/u', '', $text) ?: '';
2121
}
22-
23-
/**
24-
* @return array<int, string>|null
25-
*/
26-
public static function filterRussianProfanity(string $text): ?array
27-
{
28-
/** @var array<int, string> $profanities */
29-
$profanities = include __DIR__.'/../Config/profanities/ru.php';
30-
31-
$normalized = self::normalizeRussianText($text);
32-
$found = [];
33-
34-
foreach ($profanities as $bad) {
35-
if ($bad !== '' && str_contains($normalized, (string) $bad)) {
36-
$found[] = $bad;
37-
}
38-
}
39-
40-
return $found !== [] ? $found : null;
41-
}
42-
43-
public static function assertNoRussianProfanity(string $filePath): void
44-
{
45-
$lines = file($filePath, FILE_IGNORE_NEW_LINES) ?: [];
46-
/** @var array<int, string> $badWords */
47-
$badWords = include __DIR__.'/../Config/profanities/ru.php';
48-
49-
$offended = [];
50-
51-
foreach ($lines as $num => $line) {
52-
$norm = self::normalizeRussianText($line);
53-
54-
foreach ($badWords as $bad) {
55-
if ($bad !== '' && str_contains($norm, (string) $bad)) {
56-
$offended[] = $num + 1;
57-
break;
58-
}
59-
}
60-
}
61-
62-
if ($offended !== []) {
63-
throw new \Exception(
64-
sprintf('Profanity in %s, lines: %s', $filePath, implode(', ', $offended)),
65-
);
66-
}
67-
}
6822
}

tests/toHaveProfanity.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
->toHaveNoProfanity();
2525
})->throws(ArchExpectationFailedException::class);
2626

27+
it('fails if a file contains russian profanity', function () {
28+
expect('Tests\Fixtures\HasExplicitRussianProfanity')->toHaveNoProfanity(language: 'ru');
29+
})->throws(ArchExpectationFailedException::class);
30+
2731
it('fails if file contains profanity manually included', function () {
2832
expect('Tests\Fixtures\HasUncoveredProfanity')
2933
->toHaveNoProfanity(including: ['dagnabbit']);

0 commit comments

Comments
 (0)