Releases: thecodingmachine/phpstan-strict-rules
Releases · thecodingmachine/phpstan-strict-rules
v0.10.5
v0.10.4
Default is compulsory in switch statements
This release adds a new rule making "default" statement compulsory in "switch" statements. See #29
Rationale:
Just imagine you have a function that does some processing on an article, based on its status. Its status can be "active", "pending" or "inactive"
What you should NOT do:
function doStuff($status) {
switch ($status) {
case "active":
// Do some stuff
break;
case "pending":
// Do some stuff
break;
case "inactive":
// Do some stuff
break;
}
}
But in the future, what if a new status is added, like "archived"? How long will it take before you notice?
So your code should look like this:
function doStuff($status) {
switch ($status) {
case "active":
// Do some stuff
break;
case "pending":
// Do some stuff
break;
case "inactive":
// Do some stuff
break;
default:
// This is good! In case an unexpected status is sent, you will notice
throw InvalidStatusException(sprintf("Unknown Status '%s'", $status));
}
}
By adding a default statement that throws an exception, you are sure to notice if something goes wrong in the future.
v0.10.2 aka the "no-superglobals release"
This release adds a new rule that forbids the usage of HTTP related superglobals.
- The use of $_GET, $_POST, $_FILES, $_COOKIE, $_SESSION, $_REQUEST is forbidden. You should instead use your framework's request/session object.
- Superglobal usage is still tolerated at the root scope (because it is typically used once in
index.php
to initialize PSR-7 request object)
The rationale is that your framework should have a Request object to deal with it (PSR-7 FTW!)