|
| 1 | +<?php // lint >= 8.0 |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace Bug15198; |
| 6 | + |
| 7 | +use Exception; |
| 8 | +use function PHPStan\Testing\assertType; |
| 9 | + |
| 10 | +/** @template T */ |
| 11 | +class Foo |
| 12 | +{ |
| 13 | + |
| 14 | + /** @param T $value */ |
| 15 | + public function __construct( |
| 16 | + public mixed $value, |
| 17 | + ) |
| 18 | + { |
| 19 | + } |
| 20 | + |
| 21 | +} |
| 22 | + |
| 23 | +/** @template T */ |
| 24 | +interface FooInterface |
| 25 | +{ |
| 26 | + |
| 27 | +} |
| 28 | + |
| 29 | +/** @template-covariant T */ |
| 30 | +class CovariantFoo |
| 31 | +{ |
| 32 | + |
| 33 | +} |
| 34 | + |
| 35 | +/** @template-contravariant T */ |
| 36 | +class ContravariantFoo |
| 37 | +{ |
| 38 | + |
| 39 | +} |
| 40 | + |
| 41 | +/** @template T */ |
| 42 | +abstract class Option |
| 43 | +{ |
| 44 | + |
| 45 | +} |
| 46 | + |
| 47 | +/** @extends Option<mixed> */ |
| 48 | +final class None extends Option |
| 49 | +{ |
| 50 | + |
| 51 | +} |
| 52 | + |
| 53 | +function original(): void |
| 54 | +{ |
| 55 | + try { |
| 56 | + /** @var mixed */ |
| 57 | + $bar = 'bar'; |
| 58 | + $foo = new Foo($bar); |
| 59 | + } catch (Exception $e) { |
| 60 | + $foo = new Foo(123); |
| 61 | + } |
| 62 | + |
| 63 | + assertType('Bug15198\Foo<mixed>', $foo); |
| 64 | +} |
| 65 | + |
| 66 | +function ifElse(bool $c, mixed $bar): void |
| 67 | +{ |
| 68 | + if ($c) { |
| 69 | + $foo = new Foo($bar); |
| 70 | + } else { |
| 71 | + $foo = new Foo(123); |
| 72 | + } |
| 73 | + |
| 74 | + assertType('Bug15198\Foo<mixed>', $foo); |
| 75 | +} |
| 76 | + |
| 77 | +function ifElseReversed(bool $c, mixed $bar): void |
| 78 | +{ |
| 79 | + if ($c) { |
| 80 | + $foo = new Foo(123); |
| 81 | + } else { |
| 82 | + $foo = new Foo($bar); |
| 83 | + } |
| 84 | + |
| 85 | + assertType('Bug15198\Foo<mixed>', $foo); |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * @param Foo<mixed>|Foo<int> $a |
| 90 | + * @param Foo<int>|Foo<mixed> $b |
| 91 | + * @param CovariantFoo<mixed>|CovariantFoo<int> $c |
| 92 | + * @param CovariantFoo<int>|CovariantFoo<mixed> $d |
| 93 | + * @param ContravariantFoo<mixed>|ContravariantFoo<int> $e |
| 94 | + * @param ContravariantFoo<int>|ContravariantFoo<mixed> $f |
| 95 | + * @param FooInterface<mixed>|FooInterface<int> $g |
| 96 | + * @param FooInterface<int>|FooInterface<mixed> $h |
| 97 | + * @param class-string<Foo<mixed>>|class-string<Foo<int>> $i |
| 98 | + * @param class-string<Foo<int>>|class-string<Foo<mixed>> $j |
| 99 | + */ |
| 100 | +function unions($a, $b, $c, $d, $e, $f, $g, $h, $i, $j): void |
| 101 | +{ |
| 102 | + assertType('Bug15198\Foo<mixed>', $a); |
| 103 | + assertType('Bug15198\Foo<mixed>', $b); |
| 104 | + assertType('Bug15198\CovariantFoo<mixed>', $c); |
| 105 | + assertType('Bug15198\CovariantFoo<mixed>', $d); |
| 106 | + assertType('Bug15198\ContravariantFoo<int>', $e); |
| 107 | + assertType('Bug15198\ContravariantFoo<int>', $f); |
| 108 | + assertType('Bug15198\FooInterface<mixed>', $g); |
| 109 | + assertType('Bug15198\FooInterface<mixed>', $h); |
| 110 | + assertType('class-string<Bug15198\Foo<mixed>>', $i); |
| 111 | + assertType('class-string<Bug15198\Foo<mixed>>', $j); |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * @param Foo<mixed>&Foo<int> $a |
| 116 | + * @param Foo<int>&Foo<mixed> $b |
| 117 | + */ |
| 118 | +function intersections($a, $b): void |
| 119 | +{ |
| 120 | + assertType('Bug15198\Foo<int>', $a); |
| 121 | + assertType('Bug15198\Foo<int>', $b); |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * A class name written without type arguments has them resolved to the template |
| 126 | + * bounds, so `None` stays compatible with `Option<string>`. |
| 127 | + * |
| 128 | + * @param None|Option<string> $a |
| 129 | + * @param Option<string>|None $b |
| 130 | + */ |
| 131 | +function unparameterizedSubclass($a, $b): void |
| 132 | +{ |
| 133 | + assertType('Bug15198\Option<string>', $a); |
| 134 | + assertType('Bug15198\Option<string>', $b); |
| 135 | +} |
0 commit comments