Skip to content

Generic throw type reported as missing in @throws even when annotated #9497

Closed
phpstan/phpstan-src
#5874

Description

@janedbal

Bug report

Following snippets reports

 ------ ---------------------------------------------------------------------------------------------------------------------- 
  Line   Test.php                                                                                                              
 ------ ---------------------------------------------------------------------------------------------------------------------- 
  11     Method Test::sayHello() throws checked exception T of RuntimeException but it's missing from the PHPDoc @throws tag.  
 ------ ----------------------------------------------------------------------------------------------------------------------

when run with this settings:

parameters:
    exceptions:
        check:
            missingCheckedExceptionInThrows: true
        checkedExceptionClasses:
            - RuntimeException

Code snippet that reproduces the problem

https://phpstan.org/r/8ee8677d-3986-43ad-92de-aeed37fb75cd

Expected output

No error, RangeException is properly annotated.

Did PHPStan help you today? Did it make you happy in any way?

No response

Activity

  1. added this to the Easy fixes milestone on Jun 23, 2023
  2. devnix commented on Sep 24, 2023

    @devnix

    Watching closely, it would be great to have this!

    /**
     * @template T of \Throwable
     *
     * @param \Closure(): T $thrower
     *
     * @phpstan-assert true $fact
     * 
     * @throws T
     */
    function invariant(bool $fact, \Closure $thrower): void
    {
        if (!$fact) {
            throw $thrower();
        }
    }
    
    /**
     * @throws ClassDoesNotExistException
     */
    function buildClassHandler(string $className) 
    {
        $handlerClassName = $className.'handler';
    
        invariant(class_exists($handlerClassName), static fn () => ClassDoesNotExistException::on($handlerClassName));
    
        return $handlerClassName;
    }
  3. phpstan-bot commented on Sep 4, 2024

    @phpstan-bot
    Contributor

    @janedbal After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

    @@ @@
    -No errors
    +PHP 8.1 – 8.2
    +==========
    +
    +No errors
    +
    +PHP 7.2 – 8.0 (4 errors)
    +==========
    +
    + 9: Method Test::sayHello() has invalid return type never.
    +11: Method Test::sayHello() should return never but return statement is missing.
    +21: Tip: Method Test::throwIt() always throws an exception, it should have return type "never".
    +21: Method Test::throwIt() has invalid return type never.
    Full report

    PHP 8.1 – 8.2

    No errors

    PHP 7.2 – 8.0 (4 errors)

    Line Error
    9 Method Test::sayHello() has invalid return type never.
    11 Method Test::sayHello() should return never but return statement is missing.
    21 Tip: Method Test::throwIt() always throws an exception, it should have return type "never".
    21 Method Test::throwIt() has invalid return type never.
  4. JeroenTriptic commented on Nov 19, 2024

    @JeroenTriptic

    As this issue is still open, I would also like the same thing but with class string: https://phpstan.org/r/2eaf4c8a-3bf8-447f-8d99-4f5f758b22a2

  5. added a commit that references this issue on Jun 20, 2026
  6. added 3 commits that reference this issue on Jun 20, 2026
  7. added a commit that references this issue on Sep 19, 2026
  8. phpstan-bot commented on Sep 20, 2026

    @phpstan-bot
    Contributor

    Closed via merging phpstan/phpstan-src#5874

  9. github-actions commented on Oct 22, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  10. locked as resolved and limited conversation to collaborators on Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions