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

Amit Merchant

A blog on PHP, JavaScript, and more

Tools to make your developer experience better in PHP

With the advent of frameworks like Laravel, Symfony, CakePHP, etc., PHP has become a very powerful language for building web applications. These frameworks have greatly improved the developer experience in the PHP ecosystem.

But, there are still some tools that can make your developer experience even better. Things such as code formatters, code analyzers, code sniffers, etc. can greatly improve your workflow and can help you write better code.

In this article, I’m going to list down some of the tools that I think can make your developer experience better in PHP.

PHPStan

PHPStan is a static analysis tool that focuses on finding errors in your code without actually running it. It catches whole classes of bugs even before you write tests for the code.

PHPStan can catch a wide variety of bugs, from identifying dead code to finding out if you’re using a variable that doesn’t exist. Or if you’re using a variable that is not of the type you’re expecting it to be or unknown method checks. The list goes on.

It can also find bugs that are not easy to spot by just looking at the code or you might overlook them unintentionally like an if condition that always evaluates to true or a variable that is never used.

And that means you’ll be able to ship your code with confidence you’ve never had before.

I have written a detailed article on PHPStan if you want to learn more about it.

PHPUnit

If PHPStan is a tool that can help you write better code, PHPUnit is a tool that can help you test your code.

Nowadays, most of the frameworks come bundled with PHPUnit. So, you don’t need to install it separately. But if you’re not using any framework, you can install it using Composer.

With PHPUnit, you’ll be able to write pretty detailed tests for your code. You can write unit tests, integration tests, functional tests, etc. using PHPUnit.

Writing unit tests is a great way to ensure that your code is working as expected over time. And it also helps you to refactor your code with confidence.

So, if you’re not writing tests for your code, you should start doing it right away.

Pest

Pest is a testing framework for PHP that is built on top of PHPUnit. It’s a relatively new framework but it’s gaining a lot of traction in the PHP community.

Essentially, Pest provides a more readable and fluent syntax for writing tests and this enhances the overall experience of writing tests cutting down on the boilerplate code.

For instance, if you want to write a test for a function that adds two numbers, you can do so in PHPUnit like so.

public function test_adds_two_numbers()
{
    $this->assertEquals(2, add(1, 1));
}

But, in Pest, you can write the same test like so.

it('adds two numbers', function () {
    expect(add(1, 1))->toBe(2);
});

As you can tell, Pest’s syntax is more readable and fluent than that of PHPUnit. And it’s also more expressive.

It derives this syntax from Jest, a JavaScript testing framework. So, if you’re coming from the JavaScript world, you’ll feel right at home with Pest.

PHP-CS-Fixer

PHP-CS-Fixer is a tool that can automatically fix your code style to follow standards like PSR-1, PSR-2, PSR-12, etc. It can also fix your code to follow your own custom rules.

Contrary to some linters, PHP-CS-Fixer does not only analyze your code syntax, but also reformats your code to follow the style you configure.

It can fix a lot of things such as indentation, line endings, blank lines, spaces around keywords, spaces around operators, spaces after type casts, etc.

Composer Normalize

Sometimes, when you’re working in a team, you might face issues with Composer because of the differences in the composer.json file. For instance, one developer might have used tabs for indentation while the other might have used spaces. Or one might have used single quotes for strings while the other might have used double quotes.

This can become annoying. So, to avoid this, you can use Composer Normalize which is a Composer plugin that normalizes your composer.json file to make it consistent with the rest of your team.

You can install this plugin using Composer. Once installed, you can run the following command to normalize your composer.json file.

composer normalize

Or, you can use the --dry-run option to see what changes will be made without actually making them.

composer normalize --dry-run

Rector

Rector is a tool that can automatically upgrade (or downgrade) your code to the latest standards. It can also help you refactor your code to make it more readable and maintainable.

For instance, if you want to upgrade your code to PHP 8, you can do so by running the following command.

vendor/bin/rector process src --set php80

This will upgrade your code to PHP 8. You can also use Rector to upgrade your code to the latest version of your framework.

For instance, if you want to upgrade your code to Laravel 8, you can do so by running the following command.

vendor/bin/rector process src --set laravel80

These are called presets in Rector. You can find more about them here.

Here are my guides on upgrading your code to PHP 8 and PHP 8.2 using Rector.

Whisky

Lastly, if you want to have things run automatically based on Git hooks, you can use Whisky which is a tool that can run commands based on Git hooks.

It’s a framework-agnostic PHP package that lets you manage Git hooks in a version-controlled way. It lets you define Git hooks in a JSON file and then it automatically creates the hooks for you in the .git/hooks directory of your Git repository.

The package can be installed as a dev dependency using Composer.

composer require --dev projektgopher/whisky

And then install it like so.

./vendor/bin/whisky install

Here’s the whisky.json file that can be used to define Git hooks.

{
  "disabled": [],
  "hooks": {
    "pre-commit": [
      "./vendor/bin/pint --dirty"
    ],
    "pre-push": [
      "composer test"
    ]
  }
}

Here, we are defining two Git hooks. One is pre-commit and the other is pre-push. The pre-commit hook will run the pint command before you commit your code. And the pre-push hook will run the composer test command before you push your code.

Learn more about Whisy in this article.

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

Buy me a coffee

👋 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?