Introduction
We've all been there. You're working on a project, and you just can't seem to get the code right. You're getting frustrated and you're starting to lose your cool. Then you're tempted to leave a comment in your code that expresses your frustration.
It's really easy to want to vent your frustrations in your code. Not only can it help you express your feelings, but you can highlight to the next developer that the code is a bit of a mess and that you couldn't quite get it right.
But leaving profanity in your code is never a good idea. It's unprofessional and can be offensive to others, especially if you're leaving the comment on someone else's code. In fact, keeping your cool when auditing someone else's code is something I've written about before.
In this Quickfire article, we're going to look at a package called Profanify that can help you find and remove profanity from your PHP code.
What is Profanify?
Profanify is an awesome package built by Jon Purvis. It's a Pest plugin that you can use within your Pest architecture tests to find any profanity in your PHP code.
If any profanity is found (whether it be in a comment, or in the code itself), your Pest architecture tests will fail. This is great for running within your CI/CD pipeline to ensure that no profanity is left in your codebase.
As a result of using Profanify, you can ensure that your codebase remains professional and respectful to others.
If you're unsure what Pest architecture tests are, I've got an article that covers them in more detail: Architecture Testing in Laravel with Pest.
As a side note, Jon also has another Pest plugin called Lawman that you can use for writing architecture tests for your Saloon API integrations. I've got an article on that too if you're interested in learning more about it: Lawman: Pest Architecture Testing for Saloon API Integrations.
Install Profanify
To install Profanify, you can run the following command in your project's root:
1composer require jonpurvis/profanify --dev
That's it! You're ready to start using Profanify.
Find Profanity in Your PHP Code
Let's write our first Profanify test.
The package provides a toHaveNoProfanity
expectation that you can use in your Pest tests. This method will check the code in the namespace you provide and look for any profanity.
We'll imagine we're working on a Laravel project that contains the following model with profanity in the comment:
app/Models/User.php
1namespace App\Models;23use Illuminate\Foundation\Auth\User as Authenticatable;45class User extends Authenticatable6{7 /**8 * What the fuck were they thinking when they wrote this?9 *10 * @return string11 */12 public function myMethod(): string13 {14 return 'Hello!';15 }16}
We'll create a new test file in our tests/Architecture
directory called GlobalTest.php
. I like to place my architecture tests that cover the entire codebase in a GlobalTest
file, but you can structure your tests however you like.
The test will look like this:
tests/Architecture/GlobalTest.php
1test('no profanity is in the code')2 ->expect('App')3 ->toHaveNoProfanity();
If any profanity is found (which in this case, it would be found in the App\Models\User
model class), the test will fail with the given message:
1Expecting 'app/Models/User.php' to not use profanity.
Similarly, you can also test specific parts of your codebase. For example, if you want to test your controllers, you can create a new test file called ControllersTest.php
in your tests/Architecture
directory.
We can then write a test to check for profanity in our controllers:
tests/Architecture/ControllersTest.php
1test('no profanity is in the controllers')2 ->expect('App\Controllers')3 ->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:
1expect('App')2 ->toHaveNoProfanity(excluding: ['69']);
In the test above, the test would pass even if the word 69
was included in one of the tested files.
Want to Contribute?
If you want to contribute to the Profanify package, you can find the source code on GitHub: https://github.com/JonPurvis/profanify.
Conclusion
Hopefully, this post has shown you how you can use the Profanify package to find profanity in your PHP code. It's a great tool to help you keep your codebase professional and respectful to others.
If you enjoyed reading this post, you might be interested in checking out my 220+ page ebook "Battle Ready Laravel" which covers similar topics in more depth.
Or, you might want to check out my other 440+ page ebook "Consuming APIs in Laravel" which teaches you how to use Laravel to consume APIs from other services.
If you're interested in getting updated each time I publish a new post, feel free to sign up for my newsletter below.
Keep on building awesome stuff! 🚀