Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Mass setting strict types to true using Laravel Pint

Laravel Pint is a handy little package that helps you fix PHP code style in a Laravel project. On top of fixing code style, you can also set some rules and simplify repetitive tasks using this package.

For instance, today I learned, if you want to set the strict_types directive to true in all your PHP files to enforce strict types, you can do so using Laravel Pint.

All you need is to create a pint.json file in the root of your Laravel project and set declare_strict_types to true under the rules key like so.

    "preset": "laravel",
    "rules": {
        "declare_strict_types": true

Once done, you can run the pint command and specify the path in which you want to add declare(strict_types=1); at the top of all your PHP files.

./vendor/bin/pint app/

Here’s what it looks like in action.

Laravel Pint Strict Types Rule

And this is where it will add the declare(strict_types=1); directive in all your PHP files.

Laravel Pint adding declare(strict_types=1) in PHP files

Learn the fundamentals of PHP 8 (including 8.1 and 8.2), 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.

👋 Hi there! I'm Amit. I write articles about all things web development. If you like what I do and want me to continue doing the same, I'd like you consider leaving a tip. I'd highly appreciate that. Cheers!