Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/Expectations/Profanity.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use JonPurvis\Profanify\Expectations\TargetedProfanity;
use JonPurvis\Profanify\Support\Russian;
use Pest\Arch\Contracts\ArchExpectation;
use Pest\Arch\Support\FileLineFinder;
use PHPUnit\Architecture\Elements\ObjectDescription;
Expand Down Expand Up @@ -46,12 +47,19 @@ function (ObjectDescription $object) use (&$foundWords, $excluding, $including,

$fileContents = (string) file_get_contents($object->path);

$foundWords = array_filter($words, function (string $word) use ($fileContents): bool {
$russian = new Russian;

$foundWords = array_filter($words, function (string $word) use ($fileContents, $russian): bool {
if (preg_match('/\b'.preg_quote($word, '/').'\b/i', $fileContents)) {
return true;
}

preg_match_all('/[a-zA-Z]\w*/', $fileContents, $matches);
if ($russian->is($word)) {
$fileContents = Russian::normalize($fileContents);
preg_match_all(Russian::pattern(), $fileContents, $matches);
} else {
preg_match_all('/[a-zA-Z]\w*/', $fileContents, $matches);
}

foreach ($matches[0] as $token) {
$snakeParts = explode('_', $token);
Expand All @@ -78,6 +86,10 @@ function (ObjectDescription $object) use (&$foundWords, $excluding, $including,
return false;
});

if ($russian->isDetected()) {
$foundWords = Russian::backToOrigin($foundWords);
}

return $foundWords === [];
},
function ($path) use (&$foundWords): string {
Expand Down
65 changes: 65 additions & 0 deletions src/Support/Russian.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace JonPurvis\Profanify\Support;

final class Russian
{
private static string $pattern = '/[А-Яа-яЁё]+/u';

private bool $detected = false;

/** @var array<string, string> */
private static array $normalized = [];

/** @var array<int|string, string> */
private static array $toNormalize = [
'3' => 'з', '4' => 'ч', '6' => 'б',
'a' => 'а', 'c' => 'с', 'e' => 'е', 'o' => 'о', 'p' => 'р', 'x' => 'х', 'k' => 'к',
'A' => 'д', 'r' => 'г', 'H' => 'н', 'M' => 'м', 'T' => 'т', 'B' => 'в',
];

public function is(string $text): bool
{
if ((bool) preg_match(self::$pattern, $text)) {
$this->detected = true;
}

return $this->detected;
}

public function isDetected(): bool
{
return $this->detected;
}

public static function pattern(): string
{
return self::$pattern;
}

public static function normalize(string $text): string
{
preg_match_all('/\w+/u', $text, $words);
$toNormalizeKeysString = implode('', array_keys(self::$toNormalize));

foreach ($words[0] as $word) {
if (strpbrk($word, $toNormalizeKeysString)) {
$normalized = strtr($word, self::$toNormalize);
self::$normalized[$word] = $normalized;
}
}

return str_replace(array_keys(self::$normalized), array_values(self::$normalized), $text);
}

/**
* @param array<string> $profanities
* @return array<string>
*/
public static function backToOrigin(array $profanities): array
{
return array_map(fn ($profanity): string => array_search($profanity, self::$normalized) ?: $profanity, $profanities);
}
}
68 changes: 0 additions & 68 deletions src/Support/RussianNormalizer.php

This file was deleted.

5 changes: 5 additions & 0 deletions tests/Fixtures/HasExplicitRussianProfanity.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@
class HasExplicitRussianProfanity
{
public string $bad = 'Это полная хуйня!';

public function е6ёт()
{
// Comment...
}
}
10 changes: 0 additions & 10 deletions tests/Fixtures/HasMaskedRussianProfanity.php

This file was deleted.

4 changes: 4 additions & 0 deletions tests/toHaveProfanity.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
->toHaveNoProfanity();
})->throws(ArchExpectationFailedException::class);

it('fails if a file contains russian profanity', function () {
expect('Tests\Fixtures\HasExplicitRussianProfanity')->toHaveNoProfanity(language: 'ru');
})->throws(ArchExpectationFailedException::class);

it('fails if file contains profanity manually included', function () {
expect('Tests\Fixtures\HasUncoveredProfanity')
->toHaveNoProfanity(including: ['dagnabbit']);
Expand Down