|
| 1 | +<?php // lint >= 8.0 |
| 2 | + |
| 3 | +namespace Bug15289; |
| 4 | + |
| 5 | +use LogicException; |
| 6 | +use function PHPStan\Testing\assertType; |
| 7 | + |
| 8 | +/** |
| 9 | + * @template-covariant T |
| 10 | + * @phpstan-sealed Some|None |
| 11 | + */ |
| 12 | +interface Option {} |
| 13 | + |
| 14 | +/** |
| 15 | + * @template-covariant T |
| 16 | + * @implements Option<T> |
| 17 | + */ |
| 18 | +final class Some implements Option {} |
| 19 | + |
| 20 | +/** @implements Option<never> */ |
| 21 | +final class None implements Option {} |
| 22 | + |
| 23 | +/** |
| 24 | + * @template-covariant T |
| 25 | + * @template-covariant E |
| 26 | + * @phpstan-sealed Ok|Err |
| 27 | + */ |
| 28 | +interface Result {} |
| 29 | + |
| 30 | +/** |
| 31 | + * @template-covariant T |
| 32 | + * @implements Result<T, never> |
| 33 | + */ |
| 34 | +final class Ok implements Result {} |
| 35 | + |
| 36 | +/** |
| 37 | + * @template-covariant E |
| 38 | + * @implements Result<never, E> |
| 39 | + */ |
| 40 | +final class Err implements Result {} |
| 41 | + |
| 42 | +/** |
| 43 | + * @param Option<int> $o |
| 44 | + * @return Option<string> |
| 45 | + */ |
| 46 | +function afterEmptyBranch(Option $o): Option |
| 47 | +{ |
| 48 | + if ($o instanceof Some) { |
| 49 | + } |
| 50 | + assertType('Bug15289\None|Bug15289\Some<int>', $o); |
| 51 | + |
| 52 | + return $o; // should be reported |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * @param Option<int> $o |
| 57 | + * @return Option<string> |
| 58 | + */ |
| 59 | +function inTheBranch(Option $o): Option |
| 60 | +{ |
| 61 | + if ($o instanceof Some) { |
| 62 | + assertType('Bug15289\Some<int>', $o); |
| 63 | + |
| 64 | + return $o; // should be reported |
| 65 | + } |
| 66 | + |
| 67 | + return $o; // correctly not reported: $o is None here |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * @param Result<int, string> $r |
| 72 | + * @return Err<int> |
| 73 | + */ |
| 74 | +function elseBranch(Result $r): Err |
| 75 | +{ |
| 76 | + if ($r instanceof Ok) { |
| 77 | + throw new LogicException(); |
| 78 | + } |
| 79 | + assertType('Bug15289\Err<string>', $r); |
| 80 | + |
| 81 | + return $r; // should be reported |
| 82 | +} |
| 83 | + |
| 84 | +/** @param Option<int> $o */ |
| 85 | +function exhaustiveMatch(Option $o): int |
| 86 | +{ |
| 87 | + return match (true) { |
| 88 | + $o instanceof Some => 1, |
| 89 | + $o instanceof None => 2, |
| 90 | + }; |
| 91 | +} |
| 92 | + |
| 93 | +/** @template-covariant T */ |
| 94 | +interface UnsealedOption {} |
| 95 | + |
| 96 | +/** |
| 97 | + * @template-covariant T |
| 98 | + * @implements UnsealedOption<T> |
| 99 | + */ |
| 100 | +final class UnsealedSome implements UnsealedOption {} |
| 101 | + |
| 102 | +/** |
| 103 | + * @param UnsealedOption<int> $o |
| 104 | + * @return UnsealedOption<string> |
| 105 | + */ |
| 106 | +function unsealedControl(UnsealedOption $o): UnsealedOption |
| 107 | +{ |
| 108 | + if ($o instanceof UnsealedSome) { |
| 109 | + assertType('Bug15289\UnsealedSome<int>', $o); |
| 110 | + } |
| 111 | + assertType('Bug15289\UnsealedOption<int>', $o); |
| 112 | + |
| 113 | + return $o; // reported |
| 114 | +} |
| 115 | + |
| 116 | +/** @param Result<int, string> $r */ |
| 117 | +function truthyBranchOfEachVariant(Result $r): void |
| 118 | +{ |
| 119 | + if ($r instanceof Err) { |
| 120 | + assertType('Bug15289\Err<string>', $r); |
| 121 | + } else { |
| 122 | + assertType('Bug15289\Ok<int>', $r); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * @template T |
| 128 | + * @param Option<T> $o |
| 129 | + */ |
| 130 | +function templateArgument(Option $o): void |
| 131 | +{ |
| 132 | + assertType('Bug15289\Option<T (function Bug15289\templateArgument(), argument)>', $o); |
| 133 | + if ($o instanceof Some) { |
| 134 | + assertType('Bug15289\Some<T (function Bug15289\templateArgument(), argument)>', $o); |
| 135 | + } |
| 136 | + assertType('Bug15289\None|Bug15289\Some<T (function Bug15289\templateArgument(), argument)>', $o); |
| 137 | +} |
| 138 | + |
| 139 | +/** @template T */ |
| 140 | +interface Invariant {} |
| 141 | + |
| 142 | +/** |
| 143 | + * @template T |
| 144 | + * @implements Invariant<T> |
| 145 | + */ |
| 146 | +final class InvariantImpl implements Invariant {} |
| 147 | + |
| 148 | +/** @param Invariant<covariant int> $i */ |
| 149 | +function callSiteVariance(Invariant $i): void |
| 150 | +{ |
| 151 | + if ($i instanceof InvariantImpl) { |
| 152 | + assertType('Bug15289\InvariantImpl', $i); |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +/** @phpstan-assert Some $o */ |
| 157 | +function assertSome(Option $o): void |
| 158 | +{ |
| 159 | +} |
| 160 | + |
| 161 | +/** @param Option<int>|null $o */ |
| 162 | +function otherNarrowings(?Option $o): void |
| 163 | +{ |
| 164 | + if ($o instanceof Some) { |
| 165 | + assertType('Bug15289\Some<int>', $o); |
| 166 | + } |
| 167 | + |
| 168 | + if ($o === null) { |
| 169 | + return; |
| 170 | + } |
| 171 | + |
| 172 | + if (is_a($o, Some::class)) { |
| 173 | + assertType('Bug15289\Some<int>', $o); |
| 174 | + } |
| 175 | + |
| 176 | + if ($o::class === Some::class) { |
| 177 | + assertType('Bug15289\Some<int>', $o); |
| 178 | + } |
| 179 | + |
| 180 | + assertSome($o); |
| 181 | + assertType('Bug15289\Some<int>', $o); |
| 182 | +} |
| 183 | + |
| 184 | +/** |
| 185 | + * @template-covariant T |
| 186 | + * @phpstan-sealed Plain|Tagged |
| 187 | + */ |
| 188 | +interface Wrapped {} |
| 189 | + |
| 190 | +/** |
| 191 | + * @template-covariant T |
| 192 | + * @implements Wrapped<T> |
| 193 | + */ |
| 194 | +final class Plain implements Wrapped {} |
| 195 | + |
| 196 | +/** |
| 197 | + * @template-covariant T |
| 198 | + * @template-covariant Tag |
| 199 | + * @implements Wrapped<T> |
| 200 | + */ |
| 201 | +final class Tagged implements Wrapped {} |
| 202 | + |
| 203 | +/** @param Wrapped<int> $w */ |
| 204 | +function undeterminedArgument(Wrapped $w): void |
| 205 | +{ |
| 206 | + if ($w instanceof Tagged) { |
| 207 | + assertType('Bug15289\Tagged', $w); |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +/** @param Wrapped<int> $w */ |
| 212 | +function undeterminedRemainder(Wrapped $w): void |
| 213 | +{ |
| 214 | + if ($w instanceof Plain) { |
| 215 | + assertType('Bug15289\Plain<int>', $w); |
| 216 | + return; |
| 217 | + } |
| 218 | + |
| 219 | + assertType('Bug15289\Tagged', $w); |
| 220 | +} |
| 221 | + |
| 222 | +function bareSupertype(UnsealedOption $o): void |
| 223 | +{ |
| 224 | + assertType('Bug15289\UnsealedOption', $o); |
| 225 | + if ($o instanceof UnsealedSome) { |
| 226 | + assertType('Bug15289\UnsealedSome', $o); |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +/** |
| 231 | + * @template O of Option<int> |
| 232 | + * @param O $o |
| 233 | + */ |
| 234 | +function templateRemainder(Option $o): void |
| 235 | +{ |
| 236 | + if ($o instanceof None) { |
| 237 | + return; |
| 238 | + } |
| 239 | + |
| 240 | + assertType('O of Bug15289\Some<int> (function Bug15289\templateRemainder(), argument)', $o); |
| 241 | +} |
0 commit comments