The following should warn about yield from $iter: https://psalm.dev/r/1818600288
I found these snippets:
https://psalm.dev/r/1818600288
<?php
final class MutatingIterator implements IteratorAggregate {
public function getIterator(): ArrayIterator {
print_r('mutating');
return new ArrayIterator(['foo', 'bar']);
}
}
/**
* @psalm-pure
*/
function foo(iterable $iter): iterable {
yield from $iter;
}
$e = foo(new MutatingIterator());
print_r($e);
Psalm output (using commit 6f92846):
No issues!
Another example could be yielding from another generator: https://psalm.dev/r/ad4907e3d4
I found these snippets:
https://psalm.dev/r/ad4907e3d4
<?php
/**
* @psalm-pure
*/
function foo(iterable $iter): iterable {
yield from $iter;
}
/** @return Generator<mixed,int> */
function f() {
yield 123;
yield 234;
}
$_e = foo(f());
Psalm output (using commit 73f6fcd):
No issues!
Would it make sense to introduce a pure-iterator and a regular iterator similar to the callable types introduced in v3.15?
The reason why I ask is for functions like these:
https://psalm.dev/r/1818600288
In this case, map looks like a pure function (at least if you write it in regular arrays), but the iterable part might make it inpure if you mess up the getIterator() function.
This way you can also make distinctions between iterators that have: