Skip to content

Commit

Permalink
specify language (#22)
Browse files Browse the repository at this point in the history
This PR adds the ability to be able to specify a language. By default,
the check will loop over any files with the `profanities` directory, but
personally, I really only care about words in the en.php file. I've had
a couple of instances where words within words are picked up within
other language files. By specifying `en` as the language, I can be sure
that my codebase is only getting scanned against the correct file.

My use case is one of my applications references "Customer". The word
"Customer" contains "Cu" which is listed as profane in the pt_BR.php
file because in Portuguese, it means "ass".
  • Loading branch information
JonPurvis authored Oct 6, 2024
1 parent 65c250d commit d661049
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 8 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ expect('App')
->toHaveNoProfanity(including: ['dagnabbit']);
```


If a test does fail because of Profanity, then the output will show the offending file and line. IDE's such as PHPStorm,
will allow you to click the file and be taken straight to the line that contains profanity:

Expand All @@ -110,6 +109,17 @@ at tests/Fixtures/HasProfanityInComment.php:10
Duration: 0.06s
```

By default, Profanify will scan all language files, which may cause some problems if a word in your language is fine but
is listed as profane in another language. To combat this, you can specify a default language, which means only that file will be
checked against when the test runs:

```php
expect('App')
->toHaveNoProfanity(language: 'en');
```

The example above means that only profanity in `Config/profanities/en.php` file will be picked up.

## Languages
Profanify currently supports the following languages:

Expand Down
22 changes: 15 additions & 7 deletions src/Expectations/Profanity.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use Pest\Arch\Support\FileLineFinder;
use PHPUnit\Architecture\Elements\ObjectDescription;

expect()->extend('toHaveNoProfanity', fn (array $excluding = [], array $including = []): ArchExpectation => Targeted::make(
expect()->extend('toHaveNoProfanity', fn (array $excluding = [], array $including = [], $language = null): ArchExpectation => Targeted::make(
$this,
function (ObjectDescription $object) use (&$foundWords, $excluding, $including): bool {
function (ObjectDescription $object) use (&$foundWords, $excluding, $including, $language): bool {

$words = [];
$profanitiesDir = __DIR__.'/../Config/profanities';
Expand All @@ -19,11 +19,19 @@ function (ObjectDescription $object) use (&$foundWords, $excluding, $including):
}

$profanitiesFiles = array_diff($profanitiesFiles, ['.', '..']);
foreach ($profanitiesFiles as $profanitiesFile) {
$words = array_merge(
$words,
include "$profanitiesDir/$profanitiesFile"
);

if ($language) {
$specificLanguage = "$profanitiesDir/$language.php";
if (file_exists($specificLanguage)) {
$words = include $specificLanguage;
}
} else {
foreach ($profanitiesFiles as $profanitiesFile) {
$words = array_merge(
$words,
include "$profanitiesDir/$profanitiesFile"
);
}
}

$toleratedWords = include __DIR__.'/../Config/tolerated.php';
Expand Down
15 changes: 15 additions & 0 deletions tests/Fixtures/HasDifferentLanguageProfanity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures;

class HasDifferentLanguageProfanity
{
/**
* just a merda comment
*/
public function __construct(
//
) {}
}
5 changes: 5 additions & 0 deletions tests/toHaveNoProfanity.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@
expect('Tests\Fixtures\HasCapitalisedToleratedProfanity')
->toHaveNoProfanity();
});

it('passes if a language is specified and a file contains profanity in another language', function () {
expect('Tests\Fixtures\HasDifferentLanguageProfanity')
->toHaveNoProfanity(language: 'en');
});
5 changes: 5 additions & 0 deletions tests/toHaveProfanity.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@
expect('Tests\Fixtures\HasUncoveredProfanity')
->toHaveNoProfanity(including: ['dagnabbit']);
})->throws(ArchExpectationFailedException::class);

it('fails if file contains profanity when a specific language has been set', function () {
expect('Tests\Fixtures\HasProfanityInComment')
->toHaveNoProfanity(language: 'en');
})->throws(ArchExpectationFailedException::class);

0 comments on commit d661049

Please sign in to comment.