Skip to content

Commit

Permalink
Merge pull request #2 from ash-jc-allen/feature/excluding-words
Browse files Browse the repository at this point in the history
Add ability to exclude words from profanity check
  • Loading branch information
JonPurvis authored Aug 4, 2024
2 parents da9e5b8 + 08549c2 commit bfa57d8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ works on your application. It also means your codebase is kept professional and

This is where Profanify comes in, the package makes it really easy to test your application for common swear words, that
you may have forgotten about or had no idea they were in there in the first place. If you have your tests running as
part of a CI/CD pipeline, then it should mean the pipeline will fail if there's any profanity in your codebase.
part of a CI/CD pipeline, then it should mean the pipeline will fail if there's any profanity in your codebase.

## Installation

Expand Down Expand Up @@ -82,6 +82,15 @@ expect('App\Providers')
->not->toHaveNoProfanity()
```

There may also be times when you want to ignore certain phrases included in the profanity list. To do this, you can pass an `excluding` argument to the `toHaveNoProfanity` method. This argument should be an array of strings that you want to ignore. For example:

```php
expect('App')
->toHaveNoProfanity(excluding: ['69']);
```

In the test above, the test would pass even if the word `69` was included in one of the tested files.

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 Down
8 changes: 5 additions & 3 deletions src/Expectations/Profanity.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
use Pest\Arch\Support\FileLineFinder;
use PHPUnit\Architecture\Elements\ObjectDescription;

expect()->extend('toHaveNoProfanity', fn (): ArchExpectation => Targeted::make(
expect()->extend('toHaveNoProfanity', fn (array $excluding = []): ArchExpectation => Targeted::make(
$this,
function (ObjectDescription $object) use (&$foundWords): bool {
function (ObjectDescription $object) use (&$foundWords, $excluding): bool {
$words = include __DIR__.'/../Config/words.php';

$words = array_diff($words, $excluding);

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

$foundWords = array_filter($words, fn ($word): bool => preg_match('/\b'.preg_quote($word, '/').'\b/i', $fileContents) === 1);

return (array) $foundWords === [];
return $foundWords === [];
},
'to not use profanity',
FileLineFinder::where(function (string $line) use (&$foundWords): bool {
Expand Down
5 changes: 5 additions & 0 deletions tests/toHaveNoProfanity.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@
expect('Tests\Fixtures\HasNoProfanity')
->toHaveNoProfanity();
});

it('passes if a file contains profanity but it is excluded', function () {
expect('Tests\Fixtures\HasProfanityInComment')
->toHaveNoProfanity(excluding: ['shit']);
});

0 comments on commit bfa57d8

Please sign in to comment.