PHP Linters I can’t live without them

Applying certain quality standards in our projects is one of the most important issues for us. Linters are one of the most important tools we use for this purpose.

This kind of article has probably been written many times. Nevertheless, let me explain which linter we use and why with this article.

PHP var-dump-checker

https://github.com/php-parallel-lint/PHP-Var-Dump-Check

From time to time, software developers use functions like dd, dump, var_dump to test or debug their code. It can be the fastest and most effective way to debug. But at the same time, when it is overlooked, it can turn into a problem.

To prevent this, we use this nice linter that scans the use of these functions in our projects. So we can make sure that these functions are not used in the repository in any way.

PHP Parallel Lint

https://github.com/php-parallel-lint/PHP-Parallel-Lint

I’m sure everyone makes sure that there are no syntax errors with tools like IDE etc.
But we can’t leave it to chance. That’s why we use this linter to really make sure that there are no errors in our code. It’s a fast and parallel tool that provides an extra check without extending our CI time at all.

PHP Cs Fixer

https://github.com/PHP-CS-Fixer/PHP-CS-Fixer

https://laravel.com/docs/12.x/pint

Even if there is only one developer working on the project, it is important for us that all projects have the same code standard. php-cs-fixer is one of PHP’s oldest friends who supports us in this regard. Based on PSR-12, we support it with our own rules and ensure that all projects have exactly the same code style.

In Laravel projects, we prefer to use pint built on php-cs-fixer.

PHPStan

https://github.com/phpstan/phpstan

https://github.com/larastan/larastan

PHPStan is one of the most powerful tools among static analyzers. With different levels of settings, it not only keeps your code quality at a certain level, but also helps you find potential problems. It’s a must-have tool for us. Of course, we also need to write level adjustments and special rules according to the project we are working on. Even the lowest level contributes significantly to the quality of the project.

I like their definiton on Github page and would like to share here as well.

PHPStan 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. It moves PHP closer to compiled languages in the sense that the correctness of each line of the code can be checked before you run the actual line.

Psalm

https://github.com/vimeo/psalm

Psalm is another powerful static analyzer tool developed by Vimeo. It came out much earlier than PHPStan and I think it’s an underrated tool. Nevertheless, we include Psalm in our projects because we are not comparing tools, but rather looking at what benefit we can get from each one. An extra and free eye is always good for us.

PHP Code Sniffer

https://github.com/PHPCSStandards/PHP_CodeSniffer

PHP Code Sniffer is a very old and capable tool. There are dozens of extensions and rule sets written on it. This tool, which is quite successful even in its basic form, aims to increase your code quality and standards with its plugins.

PHP Compatability

https://github.com/PHPCompatibility/PHPCompatibility

One of the best examples of PHPCS Plugins is PHP Compatability, which checks the compatibility of your code with different PHP versions. With this plugin you can see how compatible your next code is with the next PHP version. Especially if you are refactoring an old project, this tool will be very useful.

Rector PHP

https://github.com/rectorphp/rector

RectorPHP is a tool created especially to ease the transition of your codebase to new php versions. Just like PHPCS’s add-on PHP Compatability tool, it scans your code and informs you about appropriate changes. Not only that, it can even update your code automatically. You can also write extra rule sets and make adjustments to raise the standard of your code. By incorporating CI into your processes, you can measure the compatibility of every line of code you write with subsequent versions.

https://tech.osteel.me/posts/upgrade-your-project-to-the-latest-php-version-with-rector

Leave a Reply

Your email address will not be published. Required fields are marked *