From 3a5f60be83191fb4e72097d50f7dd7dced3c7124 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Mon, 6 May 2024 00:24:20 +0100 Subject: [PATCH] Add daily email for 2024-05-05 Making PHPStan stricter --- source/_daily_emails/2024-05-05.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 source/_daily_emails/2024-05-05.md diff --git a/source/_daily_emails/2024-05-05.md b/source/_daily_emails/2024-05-05.md new file mode 100644 index 00000000..157c7b21 --- /dev/null +++ b/source/_daily_emails/2024-05-05.md @@ -0,0 +1,30 @@ +--- +title: Making PHPStan stricter +date: 2024-05-05 +permalink: archive/2024/05/05/making-phpstan-stricter +tags: + - software-development + - php + - phpstan +cta: ~ +snippet: | + As well as PHP, you can make PHPStan stricter. +--- + +Continuing yesterday's thought on [strictness in PHP][yesterday], today I want to talk about adding more strictness to PHPStan. + +Adding the [PHPStan Strict Rules extension][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. + +[extension]: https://github.com/phpstan/phpstan-strict-rules +[yesterday]: {{site.url}}/archive/2024/05/04/strict-typing-in-php