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

Support for @template-contravariant tag #7502

Closed
oprypkhantc opened this issue Jun 20, 2022 · 6 comments
Closed

Support for @template-contravariant tag #7502

oprypkhantc opened this issue Jun 20, 2022 · 6 comments

Comments

@oprypkhantc
Copy link

oprypkhantc commented Jun 20, 2022

Feature request

PHPStan currently allows two types of variance for template types in class-likes: @template (invariant) and @template-covariant (covariant). PHPStan also internally supports the third type: contravariant, but lacks support for specifying it for class-likes with a PHPDoc.

This is, however, sometimes useful:

/**
 * @template-contravariant T
 */
interface Comparator
{
    /**
      * @param T $e1
      * @param T $e2
      */
    public function compare($e1, $e2): int;
}

The implementation should theoretically be trivial: add support for @template-contravariant tag in phpdoc-parser, then correctly parse those tags into respective variance on phpstan side. As far as I understand, the support has been there internally for a long time already.

We'll also need to modify https://github.com/phpstan/phpstan-src/blob/f8be122188aa6721cec9872bd61676ee150a5311/src/Rules/Generics/VarianceCheck.php#L53 to check contravariance in parameters.

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

PHPStan is awesome!

@ondrejmirtes
Copy link
Member

What's the use-case? Your example doesn't imply anything about the need for contravariant.

@oprypkhantc
Copy link
Author

I'll give a different example: https://phpstan.org/r/f19ee206-09de-4b84-968d-5d2a74373228

@ondrejmirtes ondrejmirtes added this to the Generics milestone Jun 22, 2022
@ondrejmirtes
Copy link
Member

@dktapps
Copy link
Contributor

dktapps commented Jun 29, 2022

Related issue (probably dupe): #3960

@ondrejmirtes
Copy link
Member

Yes, please follow the original issue.

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants