php-best-practices

What I consider the best practices for web and software development.


Back to technical skills

Static Analysis

If this is a completely new topic for you, I would recommend you to start with Psalm, for example.

Psalm vs PHPStan vs Phan

Psalm

Psalm is an open-source static analysis tool for PHP that helps you identify both obvious and hard-to-spot bugs in your code.

Psalm is designed to be useful on both large legacy codebases and small, modern ones. It can help you prevent the vast majority of type-related runtime errors, and also enables you to take advantage of safe coding patterns popular in other languages.

Lastly, Psalm can automatically fix a number of the errors it finds, allowing you to improve your code without breaking a sweat.

PHPStan

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.

Phan

Phan is a static analyzer for PHP. Phan prefers to avoid false-positives and attempts to prove incorrectness rather than correctness.

Standard checks

Of course, the analyzers perform all the standard checks on the code, to ensure that:

  • there are no syntax errors
  • all the classes, methods, functions, and constants exist
  • the variables exist
  • the hints in PHPDoc correspond to reality
  • there are no arguments or variables unused

Many of these errors inevitably lead to “fatals” in the code.

You can read more about these three in this Medium post: PHP static code analysis based on the example of PHPStan, Phan and Psalm

Conclusion

If you develop with PHP nowadays, it doesn’t matter which one do you use, as long as you’re using one of them.