Making PHPStan stricter

Continuing yesterday's thought on strictness in PHP, today I want to talk about adding more strictness to PHPStan.

Adding the PHPStan Strict Rules extension makes PHPStan stricter by adding new, more opinionated rules.

For example:

  • Require booleans in if, elseif, ternary operator, after !, and on both sides of && and ||.
  • Use the $strict parameter with in_array, array_search, array_keys and base64_decode.
  • Disallow empty().
  • Require calling parent constructor.

You can enable and disable rules as needed but, like setting the PHPStan level, ideally I like to enable them all by default and see how strict I go.

It depends on the code being tested and the preference of the team, though I find the stricter the rules, the less bugs there are.

- Oliver

P.S. There's less than a year until Drupal 7's end-of-life date. Plan your upgrade to Drupal 10 now!

Was this interesting?

Sign up here and get more like this delivered straight to your inbox every day.

About me

Picture of Oliver

I'm an Acquia-certified Drupal Triple Expert with 17 years of experience, an open-source software maintainer and Drupal core contributor, public speaker, live streamer, and host of the Beyond Blocks podcast.