BadExceptionsProcessing
info off by defaultGroup: Architecture · PhpStorm name: BadExceptionsProcessingInspection
Two smells around try/catch: a try block that wraps too many statements (hard to tell which one is expected to throw), and a catch clause that binds the exception to a variable but never looks at it (the error is swallowed or its cause is lost).
Example
php
<?php
function load(string $path) {
try {
$h = fopen($path, 'r');
$line = fgets($h);
fclose($h);
return trim($line);
} catch (\ErrorException $problem) {
error_log($problem->getMessage());
}
try {
$a = 1;
// comments are not statements
$b = 2;
if ($a) { $b++; $b++; $b++; }
} catch (\LogicException $ignored) {
// nothing to do
} catch (\DomainException | \RangeException $lost) {
throw new \RuntimeException('cannot load');
} catch (\TypeError $kept) {
$log = function () use ($kept) { return $kept; };
} catch (\Error) {
}
}Reported:
- line 3: Too many statements in this try block; extract some of them so the failing call is obvious.
- line 17: Caught exception is silently discarded; at least log it.
- line 19: Caught exception is dropped; log it or pass it on as the previous exception.
Configure
In custos.json:
json
{
"rules": {
"BadExceptionsProcessing": {
"enabled": true
}
}
}Suppress
Before the statement or declaration (or the first statement of the file), either of:
php
// @custos-ignore BadExceptionsProcessing
/**
* @noinspection BadExceptionsProcessingInspection
*/