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

Contravariant bounds in generics #3960

Closed
dktapps opened this issue Oct 16, 2020 · 6 comments · Fixed by phpstan/phpstan-src#2051
Closed

Contravariant bounds in generics #3960

dktapps opened this issue Oct 16, 2020 · 6 comments · Fixed by phpstan/phpstan-src#2051

Comments

@dktapps
Copy link
Contributor

dktapps commented Oct 16, 2020

Feature request

I have an event handler system where things that want to listen to an event can subscribe to it, by class.

This system also allows handlers to receive events which are of a subclass of the event they want to receive.

Closures already support contravariance on parameters, so I can implement this using bare closures, like this: https://phpstan.org/r/e59a6d1d-3bbd-411e-83ba-049a85782c9e
But I can't wrap the closures in objects, because generics don't support contravariant bounds: https://phpstan.org/r/d3180b26-22a2-44c0-83ce-01e757299b90

(These samples are entirely manufactured to make the point; the real system I need this for is implemented in PocketMine-MP since 2018 or so).

@phpstan-bot
Copy link
Contributor

@dktapps PHPStan now reports different result with your code snippet:

@@ @@
-47: Parameter #2 $handlers of function callEvent expects array<EventHandler<SomeEventSubclass>>, array<EventHandler<SomeEvent>|EventHandler<SomeEventSubclass>> given.
+47: Parameter #2 $handlers of function callEvent expects array<EventHandler<SomeEvent>>, array<EventHandler<SomeEvent>|EventHandler<SomeEventSubclass>>&nonEmpty given.
Full report
Line Error
47 `Parameter #2 $handlers of function callEvent expects array<EventHandler>, array<EventHandler

@phpstan phpstan deleted a comment from phpstan-bot Jun 17, 2021
@phpstan-bot
Copy link
Contributor

@dktapps After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-No errors
+-1: Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument #2 ($classReflection) must be of type PHPStan\Reflection\ClassReflection, null given, called in /var/task/vendor/phpstan/phpstan-strict-rules/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php on line 40
+Run PHPStan with --debug option and post the stack trace to:
+https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md
Full report
Line Error
-1 Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument #2 ($classReflection) must be of type PHPStan\Reflection\ClassReflection, null given, called in /var/task/vendor/phpstan/phpstan-strict-rules/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php on line 40Run PHPStan with --debug option and post the stack trace to:https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md

@phpstan-bot
Copy link
Contributor

@dktapps After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-47: Parameter #2 $handlers of function callEvent expects array<EventHandler<SomeEventSubclass>>, array<EventHandler<SomeEvent>|EventHandler<SomeEventSubclass>> given.
+-1: Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument #2 ($classReflection) must be of type PHPStan\Reflection\ClassReflection, null given, called in /var/task/vendor/phpstan/phpstan-strict-rules/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php on line 40
+Run PHPStan with --debug option and post the stack trace to:
+https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md
Full report
Line Error
-1 Internal error: PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule::findMethod(): Argument #2 ($classReflection) must be of type PHPStan\Reflection\ClassReflection, null given, called in /var/task/vendor/phpstan/phpstan-strict-rules/src/Rules/Methods/WrongCaseOfInheritedMethodRule.php on line 40Run PHPStan with --debug option and post the stack trace to:https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md

@phpstan-bot
Copy link
Contributor

@dktapps After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-47: Parameter #2 $handlers of function callEvent expects array<EventHandler<SomeEventSubclass>>, array<EventHandler<SomeEvent>|EventHandler<SomeEventSubclass>> given.
+47: Parameter #2 $handlers of function callEvent expects array<EventHandler<SomeEvent>>, non-empty-array<EventHandler<SomeEvent>|EventHandler<SomeEventSubclass>> given.
Full report
Line Error
47 `Parameter #2 $handlers of function callEvent expects array<EventHandler>, non-empty-array<EventHandler

@ondrejmirtes
Copy link
Member

Here's how to implement it: #7502 (comment)

@github-actions
Copy link

github-actions bot commented Jan 6, 2023

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 Jan 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
3 participants