|
20 | 20 | namespace PhrozenByte\PHPUnitArrayAsserts\Constraint; |
21 | 21 |
|
22 | 22 | use EmptyIterator; |
| 23 | +use Exception; |
23 | 24 | use Generator; |
24 | 25 | use Iterator; |
25 | 26 | use IteratorAggregate; |
@@ -175,10 +176,13 @@ protected function matches($other): bool |
175 | 176 | * FALSE otherwise. |
176 | 177 | * |
177 | 178 | * Please note that this method will fully traverse a Traversable object. |
178 | | - * If an Iterator is given, this method will try to restore the object's |
179 | | - * pointer to its previous state. This will silently fail for instances of |
180 | | - * NoRewindIterator. Generators will be fully exhausted by this method. The |
181 | | - * behaviour for Iterators with non-unique keys is undefined. |
| 179 | + * It expects Traversables to be rewindable. For NoRewindIterator instances |
| 180 | + * it assumes that the iterator is still in its initial state. Generators |
| 181 | + * will be fully exhausted; if the iterator has begun already, the object |
| 182 | + * is considered invalid. If an Iterator is given, this method will try to |
| 183 | + * restore the object's pointer to its previous state. This will silently |
| 184 | + * fail for NoRewindIterator instances. The behaviour for Iterators with |
| 185 | + * non-unique keys is undefined. |
182 | 186 | * |
183 | 187 | * The second item of the result array holds an integer representing the |
184 | 188 | * number of items given. The integer will be ≥ 0 for any traversable data, |
@@ -227,6 +231,14 @@ protected function inspectData($other): array |
227 | 231 | $other = $other->getIterator(); |
228 | 232 | } |
229 | 233 |
|
| 234 | + if ($other instanceof Generator) { |
| 235 | + try { |
| 236 | + $other->rewind(); |
| 237 | + } catch (Exception $e) { |
| 238 | + return [ false, -1, false ]; |
| 239 | + } |
| 240 | + } |
| 241 | + |
230 | 242 | $restorePointer = null; |
231 | 243 | if ($other instanceof Iterator) { |
232 | 244 | if (!($other instanceof Generator) && !($other instanceof NoRewindIterator)) { |
|
0 commit comments