IteratorMaterializationKeyCollision
warning native off by default PHP ≥ 5.5Group: Probable bugs
Detect iterator_to_array with key preservation enabled/default on a resolved generator yielding two certainly equal keys on the same execution path.
Example
php
<?php
function rows() { yield 'k' => 2; yield 'k' => 5; } $a = iterator_to_array(rows());Reported:
- line 2: Preserve all yielded values when materializing the iterator.
Detection
- Report iterator_to_array with key preservation enabled/default on a resolved generator yielding two certainly equal keys on the same execution path.
- 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 disabled; enable only when the stated intent matches the application.
- For flow-dependent conditions, require a reachable source-to-use path with no intervening invalidation. Unknown calls, ambiguous aliases, recursion or analysis-budget exhaustion cannot establish definite facts.
Exceptions (no report)
- Unknown keys, mutually exclusive yields, or preserve_keys false are excluded.
- 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.
Fix
No automatic fix: preserving an associative mapping versus every value is an application decision.
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": {
"IteratorMaterializationKeyCollision": {
"enabled": true
}
}
}Suppress
Before the statement or declaration (or the first statement of the file):
php
// @custos-ignore IteratorMaterializationKeyCollision