|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Bug14893; |
| 4 | + |
| 5 | +use function is_array; |
| 6 | +use function is_object_in_taxonomy; |
| 7 | +use function PHPStan\Testing\assertType; |
| 8 | + |
| 9 | +/** |
| 10 | + * @template T of object |
| 11 | + */ |
| 12 | +class HelloWorld |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @param int $offset |
| 16 | + * @param \Closure(static): T $value |
| 17 | + */ |
| 18 | + public function offsetSet($offset, \Closure $value): void |
| 19 | + { |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * @param (\Closure(static): T)|(\Closure&T) $value |
| 24 | + */ |
| 25 | + public function foo($value): void |
| 26 | + { |
| 27 | + $this->offsetSet(0, $value); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @param (\Closure(static): T)|(\Closure&T) $value |
| 32 | + * @return \Closure(static): T |
| 33 | + */ |
| 34 | + public function bar($value): \Closure |
| 35 | + { |
| 36 | + return $value; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * @param HelloWorld<\DateTimeInterface> $h |
| 42 | + */ |
| 43 | +function callWithMatchingClosure(HelloWorld $h): void |
| 44 | +{ |
| 45 | + // Closure returning the concrete T is a genuine subtype - accepted. |
| 46 | + $h->offsetSet(0, static fn ($self): \DateTimeInterface => new \DateTime()); |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * @param HelloWorld<\DateTimeInterface> $h |
| 51 | + */ |
| 52 | +function callWithMismatchedClosure(HelloWorld $h): void |
| 53 | +{ |
| 54 | + // Closure returning an unrelated type is not a subtype - rejected. |
| 55 | + $h->offsetSet(0, static fn ($self): \stdClass => new \stdClass()); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * @template T of object |
| 60 | + */ |
| 61 | +class ArrayCase |
| 62 | +{ |
| 63 | + /** |
| 64 | + * @param T $value |
| 65 | + */ |
| 66 | + public function acceptsT($value): void |
| 67 | + { |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @param array<T> $value |
| 72 | + */ |
| 73 | + public function acceptsArrayT($value): void |
| 74 | + { |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @param T|array<T> $value |
| 79 | + */ |
| 80 | + public function acceptsTUnionArrayT($value): void |
| 81 | + { |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @param T|array<T> $value |
| 86 | + */ |
| 87 | + public function passUnion($value): void |
| 88 | + { |
| 89 | + // array<T> is genuinely not T - must still be rejected, the fix does not over-accept. |
| 90 | + $this->acceptsT($value); |
| 91 | + $this->acceptsArrayT($value); |
| 92 | + |
| 93 | + $this->acceptsTUnionArrayT($value); // fine |
| 94 | + |
| 95 | + if (is_array($value)) { |
| 96 | + // fine after narrow |
| 97 | + $this->acceptsArrayT($value); |
| 98 | + |
| 99 | + assertType('array<T of object (class Bug14893\ArrayCase, argument)>', $value); |
| 100 | + } else { |
| 101 | + assertType('T of object (class Bug14893\ArrayCase, argument)', $value); |
| 102 | + } |
| 103 | + |
| 104 | + if (is_object($value)) { |
| 105 | + assertType('T of object (class Bug14893\ArrayCase, argument)', $value); |
| 106 | + } else { |
| 107 | + assertType('array<T of object (class Bug14893\ArrayCase, argument)>', $value); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * @template T of object |
| 114 | + */ |
| 115 | +class ClassStringCase |
| 116 | +{ |
| 117 | + /** |
| 118 | + * @param class-string<T> $value |
| 119 | + */ |
| 120 | + public function acceptsClassString(string $value): void |
| 121 | + { |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @param non-empty-string $value |
| 126 | + */ |
| 127 | + public function acceptsNonEmptyString(string $value): void |
| 128 | + { |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @param non-empty-string|class-string<T> $value |
| 133 | + */ |
| 134 | + public function passUnion(string $value): void |
| 135 | + { |
| 136 | + // non-empty-string is not a class-string<T> - rejected. |
| 137 | + $this->acceptsClassString($value); |
| 138 | + // a class-string is always a non-empty-string - accepted. |
| 139 | + $this->acceptsNonEmptyString($value); |
| 140 | + } |
| 141 | +} |
0 commit comments