PHP Exception Transformation not correctly recognized

IntelliJ IDEA 2024.2.4 (Ultimate Edition)

PHP Plugin Version: 242.23726.107

In the following PHP Code:

try {

            /**
             * Code which potentially throws any Exception, not only MyCustomException
             */

        } catch (Throwable $e) {

            throw (!($e instanceof MyCustomException) ? new MyCustomException(
                code       : 12,
                log_details: $e->getMessage()
            ) : $e);

        }

The throw keyword gets underlined in yellow and I get an IDE warning suggesting to add a “@throws Throwable” clause to the function using the code block above, but this is wrong, as the code above never throws a Throwable but precisely transforms the thrown $e into a MyCustomException instance if it isn't already a MyCustomException instance. MyCustomException extends from Exception and is tied to some custom error handling logic, but its constructor does not throw anything in any case, so I guess this is some kind of linter bug? Of course, MyCustomException is also a Throwable, but @throws MyCustomException should be the only @throws tag to be added to the PHPDoc of the function executing this block. Adding @throws Throwable would cause any controller calling this code block to require an additional catch (Throwable) clause, although this is not necessary, hence this is somewhat frustrating as soon as your code uses rethrowing patterns etc.

0

请先登录再写评论。