|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Bug11427; |
| 4 | + |
| 5 | +/** @implements \ArrayAccess<int, int> */ |
| 6 | +class C implements \ArrayAccess { |
| 7 | + #[\ReturnTypeWillChange] |
| 8 | + public function offsetExists($offset) { |
| 9 | + throw new \Exception("exists"); |
| 10 | + } |
| 11 | + |
| 12 | + #[\ReturnTypeWillChange] |
| 13 | + public function offsetGet($offset) { |
| 14 | + throw new \Exception("get"); |
| 15 | + } |
| 16 | + |
| 17 | + #[\ReturnTypeWillChange] |
| 18 | + public function offsetSet($offset, $value) { |
| 19 | + throw new \Exception("set"); |
| 20 | + } |
| 21 | + |
| 22 | + #[\ReturnTypeWillChange] |
| 23 | + public function offsetUnset($offset) { |
| 24 | + throw new \Exception("unset"); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +function test(C $c): void { |
| 29 | + try { |
| 30 | + $x = isset($c[1]); |
| 31 | + } catch (\Exception $e) { |
| 32 | + // offsetExists can throw |
| 33 | + } |
| 34 | + |
| 35 | + try { |
| 36 | + $x = $c[1]; |
| 37 | + } catch (\Exception $e) { |
| 38 | + // offsetGet can throw |
| 39 | + } |
| 40 | + |
| 41 | + try { |
| 42 | + $c[1] = 1; |
| 43 | + } catch (\Exception $e) { |
| 44 | + // offsetSet can throw |
| 45 | + } |
| 46 | + |
| 47 | + try { |
| 48 | + unset($c[1]); |
| 49 | + } catch (\Exception $e) { |
| 50 | + // offsetUnset can throw |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Union type where isArray() returns maybe and isSuperTypeOf(ArrayAccess) returns maybe. |
| 56 | + * This ensures the conditions in NodeScopeResolver are tested with types |
| 57 | + * that distinguish !->yes() from ->no() and !->no() from ->yes(). |
| 58 | + * |
| 59 | + * @param array<int, int>|C $c |
| 60 | + */ |
| 61 | +function testArrayOrArrayAccess($c): void { |
| 62 | + try { |
| 63 | + $x = isset($c[1]); |
| 64 | + } catch (\Exception $e) { |
| 65 | + // offsetExists can throw when $c is C |
| 66 | + } |
| 67 | + |
| 68 | + try { |
| 69 | + $x = $c[1]; |
| 70 | + } catch (\Exception $e) { |
| 71 | + // offsetGet can throw when $c is C |
| 72 | + } |
| 73 | + |
| 74 | + try { |
| 75 | + $c[1] = 1; |
| 76 | + } catch (\Exception $e) { |
| 77 | + // offsetSet can throw when $c is C |
| 78 | + } |
| 79 | + |
| 80 | + try { |
| 81 | + unset($c[1]); |
| 82 | + } catch (\Exception $e) { |
| 83 | + // offsetUnset can throw when $c is C |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +class D implements \ArrayAccess { |
| 88 | + /** |
| 89 | + * @throws void |
| 90 | + */ |
| 91 | + #[\ReturnTypeWillChange] |
| 92 | + public function offsetExists($offset) { |
| 93 | + throw new \Exception("exists"); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @throws void |
| 98 | + */ |
| 99 | + #[\ReturnTypeWillChange] |
| 100 | + public function offsetGet($offset) { |
| 101 | + throw new \Exception("get"); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @throws void |
| 106 | + */ |
| 107 | + #[\ReturnTypeWillChange] |
| 108 | + public function offsetSet($offset, $value) { |
| 109 | + throw new \Exception("set"); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @throws void |
| 114 | + */ |
| 115 | + #[\ReturnTypeWillChange] |
| 116 | + public function offsetUnset($offset) { |
| 117 | + throw new \Exception("unset"); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +function test2(D $c): void { |
| 122 | + try { |
| 123 | + $x = isset($c[1]); |
| 124 | + } catch (\Exception $e) { |
| 125 | + // offsetExists cannot throw |
| 126 | + } |
| 127 | + |
| 128 | + try { |
| 129 | + $x = $c[1]; |
| 130 | + } catch (\Exception $e) { |
| 131 | + // offsetGet cannot throw |
| 132 | + } |
| 133 | + |
| 134 | + try { |
| 135 | + $c[1] = 1; |
| 136 | + } catch (\Exception $e) { |
| 137 | + // offsetSet cannot throw |
| 138 | + } |
| 139 | + |
| 140 | + try { |
| 141 | + unset($c[1]); |
| 142 | + } catch (\Exception $e) { |
| 143 | + // offsetUnset cannot throw |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +/** |
| 148 | + * @param array<C> $arrayOfC |
| 149 | + */ |
| 150 | +function test3(array $arrayOfC): void |
| 151 | +{ |
| 152 | + try { |
| 153 | + $x = isset($arrayOfC[0][1]); |
| 154 | + } catch (\Exception $e) { |
| 155 | + // offsetExists can throw |
| 156 | + } |
| 157 | + |
| 158 | + try { |
| 159 | + $x = $arrayOfC[0][1]; |
| 160 | + } catch (\Exception $e) { |
| 161 | + // offsetGet can throw |
| 162 | + } |
| 163 | + |
| 164 | + try { |
| 165 | + $arrayOfC[0][1] = 1; |
| 166 | + } catch (\Exception $e) { |
| 167 | + // offsetSet can throw |
| 168 | + } |
| 169 | + |
| 170 | + try { |
| 171 | + unset($arrayOfC[0][1]); |
| 172 | + } catch (\Exception $e) { |
| 173 | + // offsetUnset can throw |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +/** |
| 178 | + * @param array<C> $arrayOfC |
| 179 | + */ |
| 180 | +function test4(array $arrayOfC): void { |
| 181 | + try { |
| 182 | + $x = isset($arrayOfC[0]); |
| 183 | + } catch (\Exception $e) { |
| 184 | + // offsetExists cannot throw |
| 185 | + } |
| 186 | + |
| 187 | + try { |
| 188 | + $x = $arrayOfC[0]; |
| 189 | + } catch (\Exception $e) { |
| 190 | + // offsetGet cannot throw |
| 191 | + } |
| 192 | + |
| 193 | + /** @var C<int, int> $newC */ |
| 194 | + $newC = new C(); |
| 195 | + try { |
| 196 | + $arrayOfC[0] = $newC; |
| 197 | + } catch (\Exception $e) { |
| 198 | + // offsetSet cannot throw |
| 199 | + } |
| 200 | + |
| 201 | + try { |
| 202 | + unset($arrayOfC[0]); |
| 203 | + } catch (\Exception $e) { |
| 204 | + // offsetUnset cannot throw |
| 205 | + } |
| 206 | +} |
0 commit comments