|
| 1 | +<?php // lint >= 8.0 |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Bug6833; |
| 6 | + |
| 7 | +use PHPStan\TrinaryLogic; |
| 8 | +use function PHPStan\Testing\assertVariableCertainty; |
| 9 | + |
| 10 | +class File |
| 11 | +{ |
| 12 | + public function __construct(private int $id) {} |
| 13 | + public function getId(): int { return $this->id; } |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * @implements \IteratorAggregate<int, File> |
| 18 | + */ |
| 19 | +class FileCollection implements \IteratorAggregate |
| 20 | +{ |
| 21 | + /** @var File[] */ |
| 22 | + private array $files = []; |
| 23 | + |
| 24 | + public function add(File $file): void |
| 25 | + { |
| 26 | + $this->files[] = $file; |
| 27 | + } |
| 28 | + |
| 29 | + /** @throws void */ |
| 30 | + public function getIterator(): \Iterator |
| 31 | + { |
| 32 | + return new \ArrayIterator($this->files); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function testThrowsVoidOnGetIterator(FileCollection $files): void |
| 37 | +{ |
| 38 | + try { |
| 39 | + foreach ($files as $file) { |
| 40 | + echo $file->getId(); |
| 41 | + } |
| 42 | + } catch (\Throwable) { |
| 43 | + assertVariableCertainty(TrinaryLogic::createYes(), $file); |
| 44 | + echo 'Invalid file:' . $file->getId(); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * @implements \IteratorAggregate<int, File> |
| 50 | + */ |
| 51 | +class FileCollectionWithoutThrowsVoid implements \IteratorAggregate |
| 52 | +{ |
| 53 | + /** @var File[] */ |
| 54 | + private array $files = []; |
| 55 | + |
| 56 | + public function getIterator(): \Iterator |
| 57 | + { |
| 58 | + return new \ArrayIterator($this->files); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function testWithoutThrowsVoid(FileCollectionWithoutThrowsVoid $files): void |
| 63 | +{ |
| 64 | + try { |
| 65 | + foreach ($files as $file) { |
| 66 | + echo $file->getId(); |
| 67 | + } |
| 68 | + } catch (\Throwable) { |
| 69 | + assertVariableCertainty(TrinaryLogic::createMaybe(), $file); |
| 70 | + echo $file->getId(); // error - getIterator() could throw |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * @implements \IteratorAggregate<int, File> |
| 76 | + */ |
| 77 | +class FileCollectionExplicitThrows implements \IteratorAggregate |
| 78 | +{ |
| 79 | + /** @var File[] */ |
| 80 | + private array $files = []; |
| 81 | + |
| 82 | + /** @throws \RuntimeException */ |
| 83 | + public function getIterator(): \Iterator |
| 84 | + { |
| 85 | + return new \ArrayIterator($this->files); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +function testExplicitThrowsMatchingCatch(FileCollectionExplicitThrows $files): void |
| 90 | +{ |
| 91 | + try { |
| 92 | + foreach ($files as $file) { |
| 93 | + echo $file->getId(); |
| 94 | + } |
| 95 | + } catch (\Throwable) { |
| 96 | + assertVariableCertainty(TrinaryLogic::createMaybe(), $file); |
| 97 | + echo $file->getId(); // error - getIterator() can throw RuntimeException |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +function testExplicitThrowsNonMatchingCatch(FileCollectionExplicitThrows $files): void |
| 102 | +{ |
| 103 | + try { |
| 104 | + foreach ($files as $file) { |
| 105 | + if ($file->getId() < 0) { |
| 106 | + throw new \LogicException('negative'); |
| 107 | + } |
| 108 | + } |
| 109 | + } catch (\LogicException) { |
| 110 | + echo $file->getId(); // no error - RuntimeException doesn't match LogicException catch |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +/** @param File[] $files */ |
| 115 | +function testArrayForeach(array $files): void |
| 116 | +{ |
| 117 | + try { |
| 118 | + foreach ($files as $file) { |
| 119 | + echo $file->getId(); |
| 120 | + } |
| 121 | + } catch (\Throwable) { |
| 122 | + assertVariableCertainty(TrinaryLogic::createYes(), $file); |
| 123 | + echo $file->getId(); // no error - arrays don't call getIterator() |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +function testThrowsVoidFinallyScope(FileCollection $files): void |
| 128 | +{ |
| 129 | + try { |
| 130 | + foreach ($files as $file) { |
| 131 | + doSomething(); |
| 132 | + } |
| 133 | + } finally { |
| 134 | + assertVariableCertainty(TrinaryLogic::createMaybe(), $file); |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +/** @param File[]|FileCollection $files */ |
| 139 | +function testArrayOrThrowsVoid(array|FileCollection $files): void |
| 140 | +{ |
| 141 | + try { |
| 142 | + foreach ($files as $file) { |
| 143 | + echo $file->getId(); |
| 144 | + } |
| 145 | + } catch (\Throwable) { |
| 146 | + assertVariableCertainty(TrinaryLogic::createYes(), $file); |
| 147 | + echo $file->getId(); // no error - array doesn't throw, getIterator() has @throws void |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +/** @param File[]|FileCollectionExplicitThrows $files */ |
| 152 | +function testArrayOrExplicitThrows(array|FileCollectionExplicitThrows $files): void |
| 153 | +{ |
| 154 | + try { |
| 155 | + foreach ($files as $file) { |
| 156 | + echo $file->getId(); |
| 157 | + } |
| 158 | + } catch (\Throwable) { |
| 159 | + assertVariableCertainty(TrinaryLogic::createMaybe(), $file); |
| 160 | + echo $file->getId(); // error - getIterator() can throw RuntimeException |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +/** @param File[]|FileCollectionWithoutThrowsVoid $files */ |
| 165 | +function testArrayOrNoAnnotation(array|FileCollectionWithoutThrowsVoid $files): void |
| 166 | +{ |
| 167 | + try { |
| 168 | + foreach ($files as $file) { |
| 169 | + echo $file->getId(); |
| 170 | + } |
| 171 | + } catch (\Throwable) { |
| 172 | + assertVariableCertainty(TrinaryLogic::createMaybe(), $file); |
| 173 | + echo $file->getId(); // error - getIterator() could throw |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +function doSomething(): void {} |
| 178 | + |
| 179 | +/** |
| 180 | + * @implements \IteratorAggregate<int, File> |
| 181 | + */ |
| 182 | +class MaybeThrowingCollection implements \IteratorAggregate |
| 183 | +{ |
| 184 | + /** @var File[] */ |
| 185 | + private array $files = []; |
| 186 | + |
| 187 | + public function add(File $file): void |
| 188 | + { |
| 189 | + $this->files[] = $file; |
| 190 | + } |
| 191 | + |
| 192 | + public function getIterator(): \Iterator |
| 193 | + { |
| 194 | + return new \ArrayIterator($this->files); |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +function testUnionThrowsMatchingCatch(FileCollectionExplicitThrows|MaybeThrowingCollection $files): void |
| 199 | +{ |
| 200 | + try { |
| 201 | + foreach ($files as $file) { |
| 202 | + echo $file->getId(); |
| 203 | + } |
| 204 | + } catch (\Throwable) { |
| 205 | + assertVariableCertainty(TrinaryLogic::createMaybe(), $file); |
| 206 | + echo $file->getId(); // error - getIterator() can throw RuntimeException |
| 207 | + } |
| 208 | +} |
0 commit comments