Get "PHP 8 in a Nuthshell" (Now comes with PHP 8.3)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Detect leftovers like dd() and dump() in your Laravel codebase using Pest

Debugging is an essential part of the development process. And when it comes to debugging in Laravel, the first thing that comes to mind is the dd() and dump() functions. These functions are used to dump the contents of a variable and stop the execution of the script.

$users = User::all();

dd($users);

But sometimes, it’s easy to forget to remove these functions from your codebase. And if you’re working in a team, it’s even more likely that someone else might have left these functions in the codebase.

But if you’re using Pest as your testing framework, you can easily detect these leftovers in your codebase.

Essentially, Pest allows you to write tests that can be run against your codebase to detect any architectural issues. And one of the architectural issues that Pest can detect is the presence of dd() and dump() functions in your codebase.

Here’s how you can write a test to detect these leftovers.

test('if_there_are_leftovers', function () {
    arch('globals')
        ->expect(['dd', 'dump', 'var_dump', 'env'])
        ->not->toBeUsed();
});

As you can tell, this test uses the arch function which is used to detect architectural issues in your codebase. Here, we’re using the globals preset which is used to detect the use of global functions in your codebase. And we’re expecting the use of dd, dump, var_dump, and env functions in our codebase. If any of these functions are used, the test will fail.

And that’s how you can detect leftovers in your Laravel codebase using Pest.

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article? Consider leaving a

Tip

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?