php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77515 A generator is closed by making a call to Generator::valid
Submitted: 2019-01-24 12:57 UTC Modified: -
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: knyjoh at gmail dot com Assigned:
Status: Open Package: *General Issues
PHP Version: 7.3.1 OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2019-01-24 12:57 UTC] knyjoh at gmail dot com
Description:
------------
A generator is closed by making a call to Generator::valid

It is not the expected behavior that a call to valid() will actually change a state. If it is considered a correct behavior it should be documented.
Note that the same problem exists with the current() function.

In example 1 it is clear that when reaching the loop the generator is not closed, because the loop does not fail. Traversing a closed generator is not allowed and would throw an exception, which means that the generator must be open at this stage.

In example 2 however, when reaching the loop an exception is thrown "Cannot traverse an already closed generator". The generator has therefore been closed by the call to valid().

Test script:
---------------
1. <?php
2.
3. function generator() {
4.   if (false) {
5.     yield 1; // Force generator
6.   }
7. }
8.
9. // Example 1
10. $values = generator();
11. foreach ($values as $value) {} // This is ok
12.
13. // Example 2
14. $values = generator();
15. $values->valid();
16. foreach ($values as $value) {} // This is not ok

Actual result:
--------------
Fatal error: Uncaught Exception: Cannot traverse an already closed generator in /in/ab0KK:16
Stack trace:
#0 {main}
  thrown in /in/ab0KK on line 16

Patches

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Jun 20 18:01:27 2019 UTC