thecodingmachine / phpstan-strict-rules

A set of additional rules for PHPStan based on best practices followed at TheCodingMachine

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Empty catch block should consider comments

marcospassos opened this issue · comments

I want to propose considering annotated catch blocks as non-empty catch blocks. Sometimes you want to suppress an exception and handle it out of the catch block, either to avoid nested control flow or just because no treatment is needed. In those cases, a common practice is to annotate the block to make explicit the intention. The following example illustrates the point:

foreach ($handlers as $handler) {
	try {
	    $handler->trySomethind();
	} catch (HandlerFailedException $exception) {
	    // fail silently
	}
}

return $defaultValue;

Hey @marcospassos ,

I agree we need to find a way to silence this rule in some cases.

Here are a few ideas I can think of:

1- If there is a comment (any comment), let's disable the rule (this is what you propose)
2- If there is a comment that does NOT contain "TODO" or "FIXME", let's disable the rule (it's a bit more robust as this would catch things like:

	try {
	    $handler->trySomethind();
	} catch (HandlerFailedException $exception) {
	    // TODO: implement error handling
	}

3- Dictate the comment. If you want to disable the rule, the comment MUST be "Ignore this exception":

	try {
	    $handler->trySomethind();
	} catch (HandlerFailedException $exception) {
	    // Ignore this exception
	}

The third solution is the "safest", but it dictates a comment on the user, which might not be ideal.

What do you think?

The three options sound good!

I would go with the first just because it's the simplest and fastest to implement. In the future, if someone comes with a different use case, we can rethink based on this input. Does it sound reasonable?

I think I'll go with solution 3, using a custom annotation in the comments.

See #44.

Does that looks good to you?

In our case, we prefer to do not adopt a non-standard annotation. How are we supposed to suppress the warning?