ExhaustedGeneratorReused
error native on by default PHP ≥ 5.5Group: Probable bugs
Detect a resolved Generator identity traversed again after a proven complete iterator_to_array call or complete foreach.
Example
php
<?php
function values() { yield 1; } $g = values(); iterator_to_array($g); foreach ($g as $v) {}Reported:
- line 2: Create a fresh generator before traversing again.
Detection
- Report a resolved Generator identity traversed again after a proven complete iterator_to_array call or complete foreach.
- Resolve builtin calls and named arguments before checking the contract. Project-local callees are considered only when their bodies or immutable summaries establish the required facts. The default is enabled, with positive proof required.
- For flow-dependent conditions, require a reachable source-to-use path with no intervening invalidation. Unknown calls, ambiguous aliases or recursion cannot establish definite facts. When the proof is inconclusive, nothing is reported.
- The traversed expression must be a plain local variable in both traversals. A call, property or other expression may hand back a different generator each time it is evaluated, so two textually identical calls are never one identity.
Exceptions (no report)
- Early breaks, potentially exceptional completion, IteratorAggregate values, and creation of a fresh generator invalidate exhaustion proof.
- Unresolved targets, incomplete required facts, suppressed findings and PHP versions lacking the referenced API or language feature produce no report. User declarations shadowing builtin names are not builtin contracts.
- No report after a proven
iterator_to_arraywhen the variable can be rebound by code the local flow facts do not follow: a closure capturing it by reference, aglobalorstaticdeclaration of it in the same scope (for top-level code, aglobaldeclaration of it in any function of the file), anextractorparse_strcall or aninclude/requirein the same scope. - The directly preceding empty
foreachproves exhaustion only when it iterates by value and does not reuse the generator variable as its own key or value target.
Fix
No automatic fix: recreation and materialization have different side effects and memory costs.
PHP versions
Available from PHP 5.5. Builtin behavior and argument contracts follow the configured PHP version. Extension-specific calls require resolved extension API symbols; do not treat same-named application functions as extension calls.
Options
None. Enable or disable this rule through normal rule configuration.
Configure
In custos.json:
json
{
"rules": {
"ExhaustedGeneratorReused": {
"enabled": false
}
}
}Suppress
Before the statement or declaration (or the first statement of the file):
php
// @custos-ignore ExhaustedGeneratorReused