|
| 1 | +<?php // lint >= 8.1 |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace Bug9708; |
| 6 | + |
| 7 | +use function PHPStan\Testing\assertType; |
| 8 | + |
| 9 | +interface EventTopic { |
| 10 | + |
| 11 | +} |
| 12 | + |
| 13 | +enum FooEventTopic: string implements EventTopic { |
| 14 | + case Foo = 'foo'; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * @template TEventTopic of EventTopic |
| 19 | + */ |
| 20 | +interface EventType { |
| 21 | + |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +/** |
| 26 | + * @template-implements EventType<FooEventTopic> |
| 27 | + */ |
| 28 | +enum FooEventType: string implements EventType { |
| 29 | + case Foo = 'foo'; |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * @template TEventTopic of EventTopic |
| 34 | + * @template TEventType of EventType<TEventTopic> |
| 35 | + */ |
| 36 | +interface Event { |
| 37 | + |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * @template-implements Event<FooEventTopic, FooEventType> |
| 42 | + */ |
| 43 | +abstract class FooEvent implements Event { |
| 44 | + |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * This works as expected and is sufficient for this use-case |
| 49 | + * |
| 50 | + * @template TEvent of Event<EventTopic, EventType<EventTopic>> |
| 51 | + */ |
| 52 | +class EventDispatcher { |
| 53 | + |
| 54 | + /** |
| 55 | + * @param TEvent $bar |
| 56 | + */ |
| 57 | + public function dispatch (Event $bar): void { |
| 58 | + assertType('TEvent of Bug9708\Event<Bug9708\EventTopic, Bug9708\EventType<Bug9708\EventTopic>> (class Bug9708\EventDispatcher, argument)', $bar); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * The generic parameter must be specified twice! Even though it's part of the template declaration |
| 64 | + * |
| 65 | + * @template TEventTopic of EventTopic |
| 66 | + * @template TEventType of EventType<TEventTopic> |
| 67 | + * @template TEvent of Event<TEventTopic, TEventType<TEventTopic>> |
| 68 | + */ |
| 69 | +class EventDispatcher2 { |
| 70 | + |
| 71 | + /** |
| 72 | + * @param TEvent $bar |
| 73 | + */ |
| 74 | + public function dispatch (Event $bar): void { |
| 75 | + assertType('TEvent of Bug9708\Event<TEventTopic of Bug9708\EventTopic (class Bug9708\EventDispatcher2, argument), Bug9708\EventType<TEventTopic of Bug9708\EventTopic (class Bug9708\EventDispatcher2, argument)>> (class Bug9708\EventDispatcher2, argument)', $bar); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * This should work but doesn't |
| 81 | + * |
| 82 | + * @template TEventTopic of EventTopic |
| 83 | + * @template TEventType of EventType<TEventTopic> |
| 84 | + * @template TEvent of Event<TEventTopic, TEventType> |
| 85 | + */ |
| 86 | +class EventDispatcher3 { |
| 87 | + |
| 88 | + /** |
| 89 | + * @param TEvent $bar |
| 90 | + */ |
| 91 | + public function dispatch (Event $bar): void { |
| 92 | + assertType('TEvent of Bug9708\Event<TEventTopic of Bug9708\EventTopic (class Bug9708\EventDispatcher3, argument), TEventType of Bug9708\EventType<TEventTopic of Bug9708\EventTopic (class Bug9708\EventDispatcher3, argument)> (class Bug9708\EventDispatcher3, argument)> (class Bug9708\EventDispatcher3, argument)', $bar); |
| 93 | + } |
| 94 | +} |
0 commit comments