Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate options for sniffs for catching exceptions. #209

Open
herregroen opened this issue May 12, 2020 · 3 comments
Open

Investigate options for sniffs for catching exceptions. #209

herregroen opened this issue May 12, 2020 · 3 comments

Comments

@herregroen
Copy link
Contributor

With the 14.0 release we ran into unfortunate issues where we were not catching all our own exceptions, thus leading to fatals.

Because of this we'd like to explore options for sniffs that forbid the usage of functions that can throw exceptions without using try and catch.

So the following would be invalid code:

function_that_can_throw_exception();

With the following being a valid replacement;

try {
  function_that_can_throw_exception();
}
catch( Exception $e ) {
  // Anything, possibly even rethrowing the exception with added context.
  // In which case this function can't be used without catching it.
}

Even rethrowing the exception would help with developer awareness that exceptions have to be dealt with and can't simply be left.

@jrfnl
Copy link
Collaborator

jrfnl commented May 12, 2020

Two questions:

  1. Is this only global/namespaced function calls or also method calls ?
  2. Is there a list available of the functions which throw exceptions ?
    This would need to be a list-based sniff as 1) PHPCS only scans a codebase once, so can't keep track of where it has seen a throw statement and 2) add-on plugins may use functions which throw from Free/Premium.

@herregroen
Copy link
Contributor Author

This would also be for both function calls as well as for methods. For methods there'd be the complexity of finding out what the object is, can PHPCS determine this if either the object is passed as a type-hinted argument, constructed in the same function or is a type-hinted property?

Although PHPCS only scanning once does make it even more complex. Ideally we'd be able to detect which functions throw exceptions, adding free and/or premium as a dev-dependency to projects which use them to ensure the code is also available there.

First thing that comes to mind would be to use https://github.com/nikic/PHP-Parser to dynamically create a list of all functions and methods that throw exceptions and use that as the basis for a sniff.

@jrfnl
Copy link
Collaborator

jrfnl commented Sep 22, 2023

Investigate whether PHPStan (or one of the addons) or Psalm can do this. If not: close.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants