Skip to content

Closure returning never leaves the template unresolved when the parameter is Closure(): Foo<E> #15770

Open
phpstan/phpstan-src
#7117

Description

@devnix

Bug report

When the argument passed for a Closure(): Opt<E> parameter always throws, so that its return type is never, PHPStan does not resolve the template E. It reports argument.templateType, and the call returns Opt<mixed>.

This is close to #15167, which was fixed in phpstan/phpstan-src#6365. That issue covered a union parameter type, callable(): (P<TR>|TR). This case has no union, and it still fails.

Real-world example: Psl\Option\Option::orElse() in php-standard-library 6.x uses the same signature. A common idiom, $option->orElse(static fn () => throw new SomeException()), now produces Option<mixed>.

Code snippet that reproduces the problem

https://phpstan.org/r/5e4e611f-5e17-4120-b876-1779fa5312ad

Expected output

Dumped type: Opt<int>

and no argument.templateType error. E should resolve to never, and Opt<int|never> simplifies to Opt<int>.

Actual output

Dumped type: Opt<mixed>
Unable to resolve the template type E in call to method Opt<int>::orElse()

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions